mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 14:28:51 +08:00
feat(nbstore): add awareness storage&sync&frontend (#9016)
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
import { test, vitest } from 'vitest';
|
||||
import { expect, test, vitest } from 'vitest';
|
||||
import { Awareness } from 'y-protocols/awareness.js';
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
|
||||
import { AwarenessFrontend } from '../frontend/awareness';
|
||||
import { DocFrontend } from '../frontend/doc';
|
||||
import { BroadcastChannelAwarenessStorage } from '../impls/broadcast-channel/awareness';
|
||||
import { IndexedDBDocStorage } from '../impls/idb';
|
||||
import { AwarenessSync } from '../sync/awareness';
|
||||
import { expectYjsEqual } from './utils';
|
||||
|
||||
test('doc', async () => {
|
||||
@@ -48,3 +52,86 @@ test('doc', async () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('awareness', async () => {
|
||||
const storage1 = new BroadcastChannelAwarenessStorage({
|
||||
id: 'ws1',
|
||||
peer: 'a',
|
||||
type: 'workspace',
|
||||
});
|
||||
|
||||
const storage2 = new BroadcastChannelAwarenessStorage({
|
||||
id: 'ws1',
|
||||
peer: 'b',
|
||||
type: 'workspace',
|
||||
});
|
||||
|
||||
await storage1.connect();
|
||||
await storage2.connect();
|
||||
|
||||
// peer a
|
||||
const docA = new YDoc({ guid: 'test-doc' });
|
||||
docA.clientID = 1;
|
||||
const awarenessA = new Awareness(docA);
|
||||
|
||||
// peer b
|
||||
const docB = new YDoc({ guid: 'test-doc' });
|
||||
docB.clientID = 2;
|
||||
const awarenessB = new Awareness(docB);
|
||||
|
||||
// peer c
|
||||
const docC = new YDoc({ guid: 'test-doc' });
|
||||
docC.clientID = 3;
|
||||
const awarenessC = new Awareness(docC);
|
||||
|
||||
{
|
||||
const sync = new AwarenessSync(storage1, [storage2]);
|
||||
const frontend = new AwarenessFrontend(sync);
|
||||
frontend.connect(awarenessA);
|
||||
frontend.connect(awarenessB);
|
||||
}
|
||||
{
|
||||
const sync = new AwarenessSync(storage2, [storage1]);
|
||||
const frontend = new AwarenessFrontend(sync);
|
||||
frontend.connect(awarenessC);
|
||||
}
|
||||
|
||||
awarenessA.setLocalState({
|
||||
hello: 'world',
|
||||
});
|
||||
|
||||
await vitest.waitFor(() => {
|
||||
expect(awarenessB.getStates().get(1)).toEqual({
|
||||
hello: 'world',
|
||||
});
|
||||
expect(awarenessC.getStates().get(1)).toEqual({
|
||||
hello: 'world',
|
||||
});
|
||||
});
|
||||
|
||||
awarenessB.setLocalState({
|
||||
foo: 'bar',
|
||||
});
|
||||
|
||||
await vitest.waitFor(() => {
|
||||
expect(awarenessA.getStates().get(2)).toEqual({
|
||||
foo: 'bar',
|
||||
});
|
||||
expect(awarenessC.getStates().get(2)).toEqual({
|
||||
foo: 'bar',
|
||||
});
|
||||
});
|
||||
|
||||
awarenessC.setLocalState({
|
||||
baz: 'qux',
|
||||
});
|
||||
|
||||
await vitest.waitFor(() => {
|
||||
expect(awarenessA.getStates().get(3)).toEqual({
|
||||
baz: 'qux',
|
||||
});
|
||||
expect(awarenessB.getStates().get(3)).toEqual({
|
||||
baz: 'qux',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
IndexedDBSyncStorage,
|
||||
} from '../impls/idb';
|
||||
import { SpaceStorage } from '../storage';
|
||||
import { SyncEngine } from '../sync';
|
||||
import { Sync } from '../sync';
|
||||
import { expectYjsEqual } from './utils';
|
||||
|
||||
test('doc', async () => {
|
||||
@@ -53,7 +53,7 @@ test('doc', async () => {
|
||||
bin: update,
|
||||
});
|
||||
|
||||
const sync = new SyncEngine(peerA, [peerB, peerC]);
|
||||
const sync = new Sync(peerA, [peerB, peerC]);
|
||||
sync.start();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
@@ -143,7 +143,7 @@ test('blob', async () => {
|
||||
await peerB.connect();
|
||||
await peerC.connect();
|
||||
|
||||
const sync = new SyncEngine(peerA, [peerB, peerC]);
|
||||
const sync = new Sync(peerA, [peerB, peerC]);
|
||||
sync.start();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
Reference in New Issue
Block a user