|
| 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 | +}); |
0 commit comments