Skip to content

Commit c24b451

Browse files
authored
Merge pull request #669 from streamich/sync-store-updates
Make JSON Patch JSON CRDT store events synchronous
2 parents ccfd498 + c43305e commit c24b451

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/json-crdt/json-patch/JsonPatchStore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class JsonPatchStore<N extends JsonNode = JsonNode<any>> implements SyncS
2222
this.node = api.find(path) as N;
2323
this.api = api.wrap(this.node) as unknown as JsonNodeApi<N>;
2424
this.patcher = new JsonPatch(model, path);
25+
this.subscribe = (listener) => api.onChange.listen(listener);
2526
}
2627

2728
public readonly update = (change: Operation | Operation[]): void => {
@@ -35,6 +36,6 @@ export class JsonPatchStore<N extends JsonNode = JsonNode<any>> implements SyncS
3536

3637
// ---------------------------------------------------------------- SyncStore
3738

38-
public readonly subscribe = (callback: () => void) => this.api.events.onChanges.listen(() => callback());
39+
public readonly subscribe: SyncStore<any>['subscribe'];
3940
public readonly getSnapshot = () => this.node.view() as Readonly<JsonNodeView<N>>;
4041
}

src/json-crdt/json-patch/__tests__/JsonPatchStore.spec.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('can make updates', () => {
2828
});
2929
});
3030

31-
test('can subscribe and unsubscribe to changes', async () => {
31+
test('can subscribe and unsubscribe to changes', () => {
3232
const model = Model.create(
3333
s.obj({
3434
ui: s.obj({
@@ -44,25 +44,21 @@ test('can subscribe and unsubscribe to changes', async () => {
4444
const unsubscribe = store.subscribe(() => {
4545
cnt++;
4646
});
47-
await tick(1);
4847
expect(cnt).toBe(0);
4948
store.update({op: 'str_ins', path: '/text', pos: 1, str: 'x'});
50-
await tick(1);
5149
expect(cnt).toBe(1);
5250
store.update({op: 'str_ins', path: '/text', pos: 1, str: 'x'});
53-
await tick(1);
5451
expect(cnt).toBe(2);
5552
unsubscribe();
5653
store.update({op: 'str_ins', path: '/text', pos: 1, str: 'x'});
57-
await tick(1);
5854
expect(cnt).toBe(2);
5955
expect(store.getSnapshot()).toEqual({
6056
text: 'axxxbc',
6157
counter: 123,
6258
});
6359
});
6460

65-
test('can bind to a sub-path', async () => {
61+
test('can bind to a sub-path', () => {
6662
const model = Model.create(
6763
s.obj({
6864
ui: s.obj({
@@ -86,7 +82,7 @@ test('can bind to a sub-path', async () => {
8682
});
8783
});
8884

89-
test('can bind store to a "str" node', async () => {
85+
test('can bind store to a "str" node', () => {
9086
const model = Model.create(
9187
s.obj({
9288
ui: s.obj({
@@ -104,7 +100,7 @@ test('can bind store to a "str" node', async () => {
104100
expect(store2.getSnapshot()).toEqual('abcx');
105101
});
106102

107-
test('can execute mutations inside a transaction', async () => {
103+
test('can execute mutations inside a transaction', () => {
108104
const model = Model.create(
109105
s.obj({
110106
ui: s.obj({
@@ -126,7 +122,6 @@ test('can execute mutations inside a transaction', async () => {
126122
store2.update({op: 'str_ins', path: '', pos: 3, str: 'x'});
127123
});
128124
expect(cnt).toBe(1);
129-
await tick(1);
130125
expect(cnt).toBe(1);
131126
expect(store2.getSnapshot()).toEqual('abcx');
132127
});

0 commit comments

Comments
 (0)