-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nbstore): add doc sync frontend
- Loading branch information
Showing
8 changed files
with
499 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'fake-indexeddb/auto'; | ||
|
||
import { test, vitest } from 'vitest'; | ||
import { Doc as YDoc } from 'yjs'; | ||
|
||
import { DocFrontend } from '../frontend/doc'; | ||
import { IndexedDBDocStorage } from '../impls/idb'; | ||
import { expectYjsEqual } from './utils'; | ||
|
||
test('doc', async () => { | ||
const doc1 = new YDoc({ | ||
guid: 'test-doc', | ||
}); | ||
doc1.getMap('test').set('hello', 'world'); | ||
|
||
const docStorage = new IndexedDBDocStorage({ | ||
id: 'ws1', | ||
peer: 'a', | ||
type: 'workspace', | ||
}); | ||
|
||
await docStorage.connect(); | ||
|
||
const frontend1 = new DocFrontend(docStorage, null); | ||
frontend1.start(); | ||
frontend1.addDoc(doc1); | ||
await vitest.waitFor(async () => { | ||
const doc = await docStorage.getDoc('test-doc'); | ||
expectYjsEqual(doc!.bin, { | ||
test: { | ||
hello: 'world', | ||
}, | ||
}); | ||
}); | ||
|
||
const doc2 = new YDoc({ | ||
guid: 'test-doc', | ||
}); | ||
const frontend2 = new DocFrontend(docStorage, null); | ||
frontend2.start(); | ||
frontend2.addDoc(doc2); | ||
|
||
await vitest.waitFor(async () => { | ||
expectYjsEqual(doc2, { | ||
test: { | ||
hello: 'world', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect } from 'vitest'; | ||
import { applyUpdate, Doc as YDoc } from 'yjs'; | ||
|
||
export function expectYjsEqual( | ||
doc: Uint8Array | YDoc, | ||
match: Record<string, any> | ||
) { | ||
let ydoc: YDoc; | ||
if (doc instanceof Uint8Array) { | ||
ydoc = new YDoc(); | ||
applyUpdate(ydoc, doc); | ||
} else { | ||
ydoc = doc; | ||
} | ||
|
||
for (const key in match) { | ||
const value = match[key]; | ||
if (Array.isArray(value)) { | ||
const actual = ydoc.getArray(key).toJSON(); | ||
expect(actual).toEqual(value); | ||
} else if (typeof value === 'string') { | ||
const actual = ydoc.getText(key).toJSON(); | ||
expect(actual).toEqual(value); | ||
} else { | ||
const actual = ydoc.getMap(key).toJSON(); | ||
expect(actual).toEqual(value); | ||
} | ||
} | ||
return doc; | ||
} |
Oops, something went wrong.