cojson 0.8.5 → 0.8.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/native/PeerKnownStates.js +63 -0
  3. package/dist/native/PeerKnownStates.js.map +1 -0
  4. package/dist/native/PeerState.js +2 -1
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/coValueState.js +74 -0
  7. package/dist/native/coValueState.js.map +1 -0
  8. package/dist/native/exports.js +39 -0
  9. package/dist/native/exports.js.map +1 -0
  10. package/dist/native/index.native.js +2 -39
  11. package/dist/native/index.native.js.map +1 -1
  12. package/dist/native/localNode.js +28 -50
  13. package/dist/native/localNode.js.map +1 -1
  14. package/dist/native/storage/index.js +1 -1
  15. package/dist/native/storage/index.js.map +1 -1
  16. package/dist/native/sync.js +85 -105
  17. package/dist/native/sync.js.map +1 -1
  18. package/dist/web/PeerKnownStates.js +63 -0
  19. package/dist/web/PeerKnownStates.js.map +1 -0
  20. package/dist/web/PeerState.js +2 -1
  21. package/dist/web/PeerState.js.map +1 -1
  22. package/dist/web/coValueState.js +74 -0
  23. package/dist/web/coValueState.js.map +1 -0
  24. package/dist/web/exports.js +39 -0
  25. package/dist/web/exports.js.map +1 -0
  26. package/dist/web/index.web.js +2 -39
  27. package/dist/web/index.web.js.map +1 -1
  28. package/dist/web/localNode.js +28 -50
  29. package/dist/web/localNode.js.map +1 -1
  30. package/dist/web/storage/index.js +1 -1
  31. package/dist/web/storage/index.js.map +1 -1
  32. package/dist/web/sync.js +85 -105
  33. package/dist/web/sync.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/PeerKnownStates.ts +108 -0
  36. package/src/PeerState.ts +4 -2
  37. package/src/coValueState.ts +107 -0
  38. package/src/exports.ts +149 -0
  39. package/src/index.native.ts +2 -152
  40. package/src/index.web.ts +2 -152
  41. package/src/localNode.ts +42 -89
  42. package/src/storage/index.ts +1 -1
  43. package/src/sync.ts +90 -143
  44. package/src/tests/PeerKnownStates.test.ts +100 -0
  45. package/src/tests/PeerState.test.ts +0 -11
  46. package/src/tests/sync.test.ts +2 -2
  47. package/.turbo/turbo-build.log +0 -16
  48. package/.turbo/turbo-lint.log +0 -4
  49. package/.turbo/turbo-test.log +0 -1001
@@ -0,0 +1,100 @@
1
+ import { PeerKnownStates } from '../PeerKnownStates.js';
2
+ import { CoValueKnownState, emptyKnownState } from '../sync.js';
3
+ import { RawCoID, SessionID } from '../ids.js';
4
+ import { vi, describe, test, expect } from 'vitest';
5
+
6
+
7
+ describe('PeerKnownStates', () => {
8
+ test('should set and get a known state', () => {
9
+ const peerKnownStates = new PeerKnownStates();
10
+ const id = 'test-id' as RawCoID;
11
+ const knownState: CoValueKnownState = emptyKnownState(id);
12
+
13
+ peerKnownStates.dispatch({ type: 'SET', id, value: knownState });
14
+
15
+ expect(peerKnownStates.get(id)).toEqual(knownState);
16
+ expect(peerKnownStates.has(id)).toBe(true);
17
+ });
18
+
19
+ test('should update header', () => {
20
+ const peerKnownStates = new PeerKnownStates();
21
+ const id = 'test-id' as RawCoID;
22
+
23
+ peerKnownStates.dispatch({ type: 'UPDATE_HEADER', id, header: true });
24
+
25
+ const result = peerKnownStates.get(id);
26
+ expect(result?.header).toBe(true);
27
+ });
28
+
29
+ test('should update session counter', () => {
30
+ const peerKnownStates = new PeerKnownStates();
31
+ const id = 'test-id' as RawCoID;
32
+ const sessionId = 'session-1' as SessionID;
33
+
34
+ peerKnownStates.dispatch({ type: 'UPDATE_SESSION_COUNTER', id, sessionId, value: 5 });
35
+
36
+ const result = peerKnownStates.get(id);
37
+ expect(result?.sessions[sessionId]).toBe(5);
38
+ });
39
+
40
+ test('should combine with existing state', () => {
41
+ const peerKnownStates = new PeerKnownStates();
42
+ const id = 'test-id' as RawCoID;
43
+ const session1 = 'session-1' as SessionID;
44
+ const session2 = 'session-2' as SessionID;
45
+ const initialState: CoValueKnownState = {
46
+ ...emptyKnownState(id),
47
+ sessions: { [session1]: 5 },
48
+ };
49
+ const combineState: CoValueKnownState = {
50
+ ...emptyKnownState(id),
51
+ sessions: { [session2]: 10 },
52
+ };
53
+
54
+ peerKnownStates.dispatch({ type: 'SET', id, value: initialState });
55
+ peerKnownStates.dispatch({ type: 'COMBINE_WITH', id, value: combineState });
56
+
57
+ const result = peerKnownStates.get(id);
58
+ expect(result?.sessions).toEqual({ [session1]: 5, [session2]: 10 });
59
+ });
60
+
61
+ test('should set as empty', () => {
62
+ const peerKnownStates = new PeerKnownStates();
63
+ const id = 'test-id' as RawCoID;
64
+ const sessionId = 'session-1' as SessionID;
65
+ const initialState: CoValueKnownState = {
66
+ ...emptyKnownState(id),
67
+ sessions: { [sessionId]: 5 },
68
+ };
69
+
70
+ peerKnownStates.dispatch({ type: 'SET', id, value: initialState });
71
+ peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
72
+
73
+ const result = peerKnownStates.get(id);
74
+ expect(result).toEqual(emptyKnownState(id));
75
+ });
76
+
77
+ test('should trigger listeners on dispatch', () => {
78
+ const peerKnownStates = new PeerKnownStates();
79
+ const id = 'test-id' as RawCoID;
80
+ const listener = vi.fn();
81
+
82
+ peerKnownStates.subscribe(listener);
83
+ peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
84
+
85
+ expect(listener).toHaveBeenCalledWith(id, emptyKnownState(id));
86
+ });
87
+
88
+ test('should unsubscribe listener', () => {
89
+ const peerKnownStates = new PeerKnownStates();
90
+ const id = 'test-id' as RawCoID;
91
+ const listener = vi.fn();
92
+
93
+ const unsubscribe = peerKnownStates.subscribe(listener);
94
+ unsubscribe();
95
+
96
+ peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
97
+
98
+ expect(listener).not.toHaveBeenCalled();
99
+ });
100
+ });
@@ -20,17 +20,6 @@ function setup() {
20
20
  }
21
21
 
22
22
  describe("PeerState", () => {
23
- test("should initialize with correct properties", () => {
24
- const { peerState } = setup();
25
- expect(peerState.id).toBe("test-peer");
26
- expect(peerState.role).toBe("peer");
27
- expect(peerState.priority).toBe(1);
28
- expect(peerState.crashOnClose).toBe(false);
29
- expect(peerState.closed).toBe(false);
30
- expect(peerState.optimisticKnownStates).toEqual({});
31
- expect(peerState.toldKnownState).toEqual(new Set());
32
- });
33
-
34
23
  test("should push outgoing message to peer", async () => {
35
24
  const { mockPeer, peerState } = setup();
36
25
  const message: SyncMessage = { action: "load", id: "co_ztest-id", header: false, sessions: {} };
@@ -761,7 +761,7 @@ test.skip("When replaying creation and transactions of a coValue as new content,
761
761
  expect(groupTellKnownStateMsg).toMatchObject(groupStateEx(group));
762
762
 
763
763
  expect(
764
- node2.syncManager.peers["test1"]!.optimisticKnownStates[group.core.id],
764
+ node2.syncManager.peers["test1"]!.optimisticKnownStates.has(group.core.id),
765
765
  ).toBeDefined();
766
766
 
767
767
  // await inTx1.push(adminTellKnownStateMsg);
@@ -1086,7 +1086,7 @@ test("If we start loading a coValue before connecting to a peer that has it, it
1086
1086
 
1087
1087
  const mapOnNode2Promise = node2.loadCoValueCore(map.core.id);
1088
1088
 
1089
- expect(node2.coValues[map.core.id]?.state).toEqual("loading");
1089
+ expect(node2.coValues[map.core.id]?.state.type).toEqual("unknown");
1090
1090
 
1091
1091
  node2.syncManager.addPeer(node1asPeer);
1092
1092
 
@@ -1,16 +0,0 @@
1
-
2
- > cojson@0.8.3 build /Users/anselm/jazz/jazz/packages/cojson
3
- > npm run lint && rm -rf ./dist && pnpm run build:native && pnpm run build:web
4
-
5
-
6
- > cojson@0.8.3 lint
7
- > eslint . --ext ts,tsx
8
-
9
-
10
- > cojson@0.8.3 build:native /Users/anselm/jazz/jazz/packages/cojson
11
- > tsc --sourceMap --outDir dist/native -p tsconfig.native.json
12
-
13
-
14
- > cojson@0.8.3 build:web /Users/anselm/jazz/jazz/packages/cojson
15
- > tsc --sourceMap --outDir dist/web -p tsconfig.web.json
16
-
@@ -1,4 +0,0 @@
1
-
2
- > cojson@0.7.35-guest-auth.5 lint /Users/anselm/jazz/jazz/packages/cojson
3
- > eslint . --ext ts,tsx
4
-