Skip to content

Commit 00af492

Browse files
committed
fix: 🐛 filter events correctly
1 parent f973552 commit 00af492

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"abstract-level": "^2.0.0",
7878
"browser-level": "^1.0.1",
7979
"fs-zoo": "^1.1.0",
80-
"json-joy": "^16.23.2",
80+
"json-joy": "^16.24.0",
8181
"memfs": "^4.11.0",
8282
"memory-level": "^1.0.0",
8383
"sonic-forest": "^1.0.3",
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {Model, s} from 'json-joy/lib/json-crdt';
2+
import {setup} from './setup';
3+
import {BehaviorSubject} from 'rxjs';
4+
import {until} from 'thingies';
5+
import {LocalRepoRebaseEvent} from '../../types';
6+
7+
describe('events', () => {
8+
test('can emit to another tab', async () => {
9+
const kit = await setup({local: {connected$: new BehaviorSubject(false)}});
10+
const local2 = await kit.createLocal();
11+
const bus1 = kit.pubsub;
12+
const bus2 = local2.pubsub;
13+
const msgs: any[] = [];
14+
bus1.bus$.subscribe((msg) => {
15+
msgs.push(msg);
16+
});
17+
bus2.bus$.subscribe((msg) => {
18+
msgs.push(msg);
19+
});
20+
(bus1 as any).pub({foo: 'bar', bin: new Uint8Array([1, 2, 3])});
21+
await until(() => msgs.length === 2);
22+
expect(msgs).toEqual([{foo: 'bar', bin: new Uint8Array([1, 2, 3])}, {foo: 'bar', bin: new Uint8Array([1, 2, 3])}]);
23+
await bus1.end();
24+
await bus2.end();
25+
});
26+
27+
test('emits "rebase" event (in same tab)', async () => {
28+
const kit = await setup({local: {connected$: new BehaviorSubject(false)}});
29+
const model = Model.create(s.obj({foo: s.con(1)}), kit.sid);
30+
const patch = model.api.flush();
31+
const res1 = await kit.local.sync({
32+
id: kit.blockId,
33+
patches: [patch],
34+
});
35+
const events: LocalRepoRebaseEvent[] = [];
36+
const sub = kit.local.change$(kit.blockId).subscribe((event) => {
37+
if ((event as LocalRepoRebaseEvent).rebase) events.push(event as LocalRepoRebaseEvent);
38+
});
39+
const model2 = model.clone();
40+
model.api.obj([]).set({foo: 2});
41+
const patch2 = model.api.flush();
42+
const res2 = await kit.local.sync({
43+
id: kit.blockId,
44+
patches: [patch2],
45+
cursor: res1.cursor,
46+
});
47+
const get1 = await kit.local.get({id: kit.blockId});
48+
expect(get1.model.view()).toEqual({foo: 2});
49+
await until(() => events.length === 1);
50+
const event = events[0];
51+
expect(model2.view()).toEqual({foo: 1});
52+
for (const patch of event.rebase) {
53+
model2.applyPatch(patch);
54+
}
55+
expect(model2.view()).toEqual({foo: 2});
56+
sub.unsubscribe();
57+
await kit.stop();
58+
});
59+
60+
test('emits "rebase" event (across tabs)', async () => {
61+
const kit = await setup({local: {connected$: new BehaviorSubject(false)}});
62+
const local2 = await kit.createLocal();
63+
const model = Model.create(s.obj({foo: s.con(1)}), kit.sid);
64+
const patch = model.api.flush();
65+
const res1 = await kit.local.sync({
66+
id: kit.blockId,
67+
patches: [patch],
68+
});
69+
const events: LocalRepoRebaseEvent[] = [];
70+
const sub = local2.local.change$(kit.blockId).subscribe((event) => {
71+
if ((event as LocalRepoRebaseEvent).rebase) events.push(event as LocalRepoRebaseEvent);
72+
});
73+
const model2 = model.clone();
74+
model.api.obj([]).set({foo: 2});
75+
const patch2 = model.api.flush();
76+
const res2 = await kit.local.sync({
77+
id: kit.blockId,
78+
patches: [patch2],
79+
cursor: res1.cursor,
80+
});
81+
const get1 = await kit.local.get({id: kit.blockId});
82+
expect(get1.model.view()).toEqual({foo: 2});
83+
await until(() => events.length === 1);
84+
const event = events[0];
85+
expect(model2.view()).toEqual({foo: 1});
86+
for (const patch of event.rebase) {
87+
model2.applyPatch(patch);
88+
}
89+
expect(model2.view()).toEqual({foo: 2});
90+
sub.unsubscribe();
91+
await kit.stop();
92+
});
93+
});

src/json-crdt-repo/local/level/__tests__/setup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const setup = async (
2525
}) as unknown as BinStrLevel;
2626
const blockId = [...col, id];
2727
const createLocal = (sid: number = 12345678) => {
28-
const pubsub = createPubsub('test-' + blockId.join('/')) as LevelLocalRepoPubSub;
28+
const busName = 'test-' + id;
29+
const pubsub = createPubsub(busName) as LevelLocalRepoPubSub;
2930
const local = new LevelLocalRepo({
3031
kv,
3132
locks,

src/json-crdt-repo/pubsub/__tests__/index.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ test('can emit to another instance', async () => {
66
const bus1 = pubsub(id);
77
const bus2 = pubsub(id);
88
const msgs: any[] = [];
9+
bus1.bus$.subscribe((msg) => {
10+
msgs.push(msg);
11+
});
912
bus2.bus$.subscribe((msg) => {
1013
msgs.push(msg);
1114
});
1215
bus1.pub('hello');
13-
await until(() => msgs.length === 1);
14-
expect(msgs).toEqual(['hello']);
16+
await until(() => msgs.length === 2);
17+
expect(msgs).toEqual(['hello', 'hello']);
1518
await bus1.end();
1619
await bus2.end();
1720
});

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,10 +2020,10 @@ jsesc@^2.5.1:
20202020
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
20212021
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==
20222022

2023-
json-joy@^16.23.2:
2024-
version "16.23.2"
2025-
resolved "https://registry.yarnpkg.com/json-joy/-/json-joy-16.23.2.tgz#e7132edd50aa7338f2d7d335e5c08e85f306d57a"
2026-
integrity sha512-DgCJYEpU91IijGXWXBTtZJICBDBiSIV0c1Lvu5CYm4WjcT42Kz4SXxRC6aS8FJKNj+JR2VsjPoi0V0YA9hhdEA==
2023+
json-joy@^16.24.0:
2024+
version "16.24.0"
2025+
resolved "https://registry.yarnpkg.com/json-joy/-/json-joy-16.24.0.tgz#ae68c10524d9cde99a5363baf46fe8e3f6aca4c4"
2026+
integrity sha512-Y9yvAOCLl8ar/tHgstxWi7snXj3Vl/6wWtSb2YH1QNRxRHYJSC2dmJm+xolXYkxoD41lQA898n7TLJgiQceToQ==
20272027
dependencies:
20282028
arg "^5.0.2"
20292029
hyperdyperid "^1.2.0"

0 commit comments

Comments
 (0)