mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
chore: merge blocksuite source code (#9213)
This commit is contained in:
18
blocksuite/framework/sync/src/awareness/engine.ts
Normal file
18
blocksuite/framework/sync/src/awareness/engine.ts
Normal 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());
|
||||
}
|
||||
}
|
||||
73
blocksuite/framework/sync/src/awareness/impl/broadcast.ts
Normal file
73
blocksuite/framework/sync/src/awareness/impl/broadcast.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
1
blocksuite/framework/sync/src/awareness/impl/index.ts
Normal file
1
blocksuite/framework/sync/src/awareness/impl/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './broadcast.js';
|
||||
3
blocksuite/framework/sync/src/awareness/index.ts
Normal file
3
blocksuite/framework/sync/src/awareness/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './engine.js';
|
||||
export * from './impl/index.js';
|
||||
export * from './source.js';
|
||||
6
blocksuite/framework/sync/src/awareness/source.ts
Normal file
6
blocksuite/framework/sync/src/awareness/source.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { Awareness } from 'y-protocols/awareness';
|
||||
|
||||
export interface AwarenessSource {
|
||||
connect(awareness: Awareness): void;
|
||||
disconnect(): void;
|
||||
}
|
||||
Reference in New Issue
Block a user