|
1 | 1 | import { CollaborationProvider, HistoryItem } from '@/types'; |
2 | 2 | import * as Y from 'yjs'; |
3 | | -// import { eventEmitter } from '@/util'; |
| 3 | +import { collaborationLog, generateUUID } from '@/util'; |
| 4 | +// import { IndexeddbPersistence } from 'y-indexeddb'; |
4 | 5 |
|
5 | 6 | export class LocalProvider implements CollaborationProvider { |
6 | 7 | private readonly broadcastChannel: BroadcastChannel; |
| 8 | + // private readonly db: IndexeddbPersistence; |
| 9 | + private readonly peerId = generateUUID(); |
7 | 10 | constructor(doc: Y.Doc) { |
8 | | - const docId = doc.guid; |
9 | | - this.broadcastChannel = new BroadcastChannel(docId); |
| 11 | + this.broadcastChannel = new BroadcastChannel(doc.guid); |
10 | 12 | this.broadcastChannel.onmessage = ( |
11 | | - event: MessageEvent<{ docId: string; update: Uint8Array }>, |
| 13 | + event: MessageEvent<{ |
| 14 | + docId: string; |
| 15 | + update: Uint8Array; |
| 16 | + peerId: string; |
| 17 | + }>, |
12 | 18 | ) => { |
13 | | - const { update } = event.data; |
14 | | - console.log(docId, event); |
15 | | - Y.applyUpdate(doc, update); |
| 19 | + const { update, peerId } = event.data; |
| 20 | + collaborationLog('onmessage', event, this.peerId); |
| 21 | + if (peerId === this.peerId) { |
| 22 | + return; |
| 23 | + } |
| 24 | + Y.applyUpdate(doc, update, peerId); |
16 | 25 | // eventEmitter.emit('modelChange', { changeSet: new Set(['rangeMap']) }); |
17 | 26 | }; |
| 27 | + // this.db = new IndexeddbPersistence('excel', doc); |
18 | 28 | } |
19 | 29 | addHistory = async (docId: string, update: Uint8Array) => { |
20 | | - this.broadcastChannel.postMessage({ docId, update }); |
| 30 | + this.broadcastChannel.postMessage({ docId, update, peerId: this.peerId }); |
21 | 31 | }; |
22 | | - retrieveHistory = async (docId: string): Promise<HistoryItem[]> => { |
23 | | - console.log(docId); |
| 32 | + retrieveHistory = async (_docId: string): Promise<HistoryItem[]> => { |
| 33 | + // await this.db.whenSynced; |
| 34 | + // console.log('load db success') |
24 | 35 | return []; |
25 | 36 | }; |
26 | 37 | } |
0 commit comments