chore: merge blocksuite source code (#9213)

This commit is contained in:
Mirone
2024-12-20 15:38:06 +08:00
committed by GitHub
parent 2c9ef916f4
commit 30200ff86d
2031 changed files with 238888 additions and 229 deletions

View File

@@ -0,0 +1,18 @@
import type { Awareness } from 'y-protocols/awareness';
import type { AwarenessSource } from './source.js';
export class AwarenessEngine {
constructor(
readonly awareness: Awareness,
readonly sources: AwarenessSource[]
) {}
connect() {
this.sources.forEach(source => source.connect(this.awareness));
}
disconnect() {
this.sources.forEach(source => source.disconnect());
}
}

View File

@@ -0,0 +1,73 @@
import type { Awareness } from 'y-protocols/awareness';
import {
applyAwarenessUpdate,
encodeAwarenessUpdate,
} from 'y-protocols/awareness';
import type { AwarenessSource } from '../source.js';
type AwarenessChanges = Record<'added' | 'updated' | 'removed', number[]>;
type ChannelMessage =
| { type: 'connect' }
| { type: 'update'; update: Uint8Array };
export class BroadcastChannelAwarenessSource implements AwarenessSource {
awareness: Awareness | null = null;
channel: BroadcastChannel | null = null;
handleAwarenessUpdate = (changes: AwarenessChanges, origin: unknown) => {
if (origin === 'remote') {
return;
}
const changedClients = Object.values(changes).reduce((res, cur) =>
res.concat(cur)
);
const update = encodeAwarenessUpdate(this.awareness!, changedClients);
this.channel?.postMessage({
type: 'update',
update: update,
} satisfies ChannelMessage);
};
constructor(readonly channelName: string) {}
connect(awareness: Awareness): void {
this.channel = new BroadcastChannel(this.channelName);
this.channel.postMessage({
type: 'connect',
} satisfies ChannelMessage);
this.awareness = awareness;
awareness.on('update', this.handleAwarenessUpdate);
this.channel.addEventListener(
'message',
(event: MessageEvent<ChannelMessage>) => {
this.handleChannelMessage(event);
}
);
}
disconnect(): void {
this.awareness?.off('update', this.handleAwarenessUpdate);
this.channel?.close();
this.channel = null;
}
handleChannelMessage(event: MessageEvent<ChannelMessage>) {
if (event.data.type === 'update') {
const update = event.data.update;
applyAwarenessUpdate(this.awareness!, update, 'remote');
}
if (event.data.type === 'connect') {
this.channel?.postMessage({
type: 'update',
update: encodeAwarenessUpdate(this.awareness!, [
this.awareness!.clientID,
]),
} satisfies ChannelMessage);
}
}
}

View File

@@ -0,0 +1 @@
export * from './broadcast.js';

View File

@@ -0,0 +1,3 @@
export * from './engine.js';
export * from './impl/index.js';
export * from './source.js';

View File

@@ -0,0 +1,6 @@
import type { Awareness } from 'y-protocols/awareness';
export interface AwarenessSource {
connect(awareness: Awareness): void;
disconnect(): void;
}