jazz-tools 0.8.14 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/CHANGELOG.md +281 -268
  2. package/dist/native/coValues/account.js +3 -6
  3. package/dist/native/coValues/account.js.map +1 -1
  4. package/dist/native/coValues/coList.js +3 -7
  5. package/dist/native/coValues/coList.js.map +1 -1
  6. package/dist/native/coValues/coMap.js +8 -14
  7. package/dist/native/coValues/coMap.js.map +1 -1
  8. package/dist/native/coValues/coStream.js +7 -12
  9. package/dist/native/coValues/coStream.js.map +1 -1
  10. package/dist/native/coValues/deepLoading.js +6 -3
  11. package/dist/native/coValues/deepLoading.js.map +1 -1
  12. package/dist/native/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/native/coValues/group.js +3 -6
  14. package/dist/native/coValues/group.js.map +1 -1
  15. package/dist/native/coValues/interfaces.js +4 -3
  16. package/dist/native/coValues/interfaces.js.map +1 -1
  17. package/dist/native/exports.js +3 -9
  18. package/dist/native/exports.js.map +1 -1
  19. package/dist/native/implementation/createContext.js +7 -0
  20. package/dist/native/implementation/createContext.js.map +1 -1
  21. package/dist/native/implementation/devtoolsFormatters.js +5 -25
  22. package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
  23. package/dist/native/implementation/refs.js +1 -2
  24. package/dist/native/implementation/refs.js.map +1 -1
  25. package/dist/native/implementation/schema.js +1 -1
  26. package/dist/native/implementation/schema.js.map +1 -1
  27. package/dist/native/implementation/subscriptionScope.js +2 -4
  28. package/dist/native/implementation/subscriptionScope.js.map +1 -1
  29. package/dist/native/index.native.js +1 -1
  30. package/dist/native/index.native.js.map +1 -1
  31. package/dist/native/lib/cache.js.map +1 -1
  32. package/dist/native/lib/cache.test.js +1 -1
  33. package/dist/native/lib/cache.test.js.map +1 -1
  34. package/dist/web/coValues/account.js +3 -6
  35. package/dist/web/coValues/account.js.map +1 -1
  36. package/dist/web/coValues/coList.js +3 -7
  37. package/dist/web/coValues/coList.js.map +1 -1
  38. package/dist/web/coValues/coMap.js +8 -14
  39. package/dist/web/coValues/coMap.js.map +1 -1
  40. package/dist/web/coValues/coStream.js +7 -12
  41. package/dist/web/coValues/coStream.js.map +1 -1
  42. package/dist/web/coValues/deepLoading.js +6 -3
  43. package/dist/web/coValues/deepLoading.js.map +1 -1
  44. package/dist/web/coValues/extensions/imageDef.js.map +1 -1
  45. package/dist/web/coValues/group.js +3 -6
  46. package/dist/web/coValues/group.js.map +1 -1
  47. package/dist/web/coValues/interfaces.js +4 -3
  48. package/dist/web/coValues/interfaces.js.map +1 -1
  49. package/dist/web/exports.js +3 -9
  50. package/dist/web/exports.js.map +1 -1
  51. package/dist/web/implementation/createContext.js +7 -0
  52. package/dist/web/implementation/createContext.js.map +1 -1
  53. package/dist/web/implementation/devtoolsFormatters.js +5 -25
  54. package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
  55. package/dist/web/implementation/refs.js +1 -2
  56. package/dist/web/implementation/refs.js.map +1 -1
  57. package/dist/web/implementation/schema.js +1 -1
  58. package/dist/web/implementation/schema.js.map +1 -1
  59. package/dist/web/implementation/subscriptionScope.js +2 -4
  60. package/dist/web/implementation/subscriptionScope.js.map +1 -1
  61. package/dist/web/lib/cache.js.map +1 -1
  62. package/dist/web/lib/cache.test.js +1 -1
  63. package/dist/web/lib/cache.test.js.map +1 -1
  64. package/package.json +5 -9
  65. package/src/coValues/account.ts +330 -339
  66. package/src/coValues/coList.ts +474 -495
  67. package/src/coValues/coMap.ts +584 -604
  68. package/src/coValues/coStream.ts +624 -650
  69. package/src/coValues/deepLoading.ts +184 -200
  70. package/src/coValues/extensions/imageDef.ts +44 -44
  71. package/src/coValues/group.ts +196 -210
  72. package/src/coValues/interfaces.ts +197 -199
  73. package/src/exports.ts +38 -25
  74. package/src/implementation/createContext.ts +210 -204
  75. package/src/implementation/devtoolsFormatters.ts +80 -100
  76. package/src/implementation/refs.ts +127 -139
  77. package/src/implementation/schema.ts +124 -128
  78. package/src/implementation/subscriptionScope.ts +111 -121
  79. package/src/index.native.ts +3 -3
  80. package/src/lib/cache.test.ts +48 -48
  81. package/src/lib/cache.ts +9 -9
  82. package/src/tests/coList.test.ts +264 -283
  83. package/src/tests/coMap.test.ts +741 -761
  84. package/src/tests/coStream.test.ts +405 -438
  85. package/src/tests/deepLoading.test.ts +251 -256
  86. package/src/tests/groupsAndAccounts.test.ts +70 -74
  87. package/src/tests/schema.test.ts +198 -198
  88. package/src/tests/subscribe.test.ts +312 -299
  89. package/tsconfig.json +2 -4
  90. package/tsconfig.native.json +4 -10
  91. package/tsconfig.web.json +4 -10
  92. package/.eslintrc.cjs +0 -24
  93. package/.prettierrc.js +0 -9
@@ -1,148 +1,138 @@
1
1
  import type { RawCoValue } from "cojson";
2
2
  import type {
3
- Account,
4
- CoValue,
5
- ID,
6
- CoValueClass,
7
- CoValueFromRaw,
8
- AnonymousJazzAgent,
3
+ Account,
4
+ AnonymousJazzAgent,
5
+ CoValue,
6
+ CoValueClass,
7
+ CoValueFromRaw,
8
+ ID,
9
9
  } from "../internal.js";
10
10
 
11
11
  export const subscriptionsScopes = new WeakMap<
12
- CoValue,
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- SubscriptionScope<any>
12
+ CoValue,
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ SubscriptionScope<any>
15
15
  >();
16
16
 
17
17
  const TRACE_INVALIDATIONS = false;
18
18
 
19
19
  export class SubscriptionScope<Root extends CoValue> {
20
- scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
21
- subscriber: Account | AnonymousJazzAgent;
22
- entries = new Map<
23
- ID<CoValue>,
24
- | { state: "loading"; immediatelyUnsub?: boolean }
25
- | { state: "loaded"; rawUnsub: () => void }
26
- >();
27
- rootEntry: {
28
- state: "loaded";
29
- value: RawCoValue;
30
- rawUnsub: () => void;
20
+ scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
21
+ subscriber: Account | AnonymousJazzAgent;
22
+ entries = new Map<
23
+ ID<CoValue>,
24
+ | { state: "loading"; immediatelyUnsub?: boolean }
25
+ | { state: "loaded"; rawUnsub: () => void }
26
+ >();
27
+ rootEntry: {
28
+ state: "loaded";
29
+ value: RawCoValue;
30
+ rawUnsub: () => void;
31
+ };
32
+ scheduleUpdate: () => void;
33
+ scheduledUpdate: boolean = false;
34
+ cachedValues: { [id: ID<CoValue>]: CoValue } = {};
35
+ parents: { [id: ID<CoValue>]: Set<ID<CoValue>> } = {};
36
+
37
+ constructor(
38
+ root: Root,
39
+ rootSchema: CoValueClass<Root> & CoValueFromRaw<Root>,
40
+ onUpdate: (newRoot: Root) => void,
41
+ ) {
42
+ this.rootEntry = {
43
+ state: "loaded" as const,
44
+ value: root._raw,
45
+ rawUnsub: () => {}, // placeholder
31
46
  };
32
- scheduleUpdate: () => void;
33
- scheduledUpdate: boolean = false;
34
- cachedValues: { [id: ID<CoValue>]: CoValue } = {};
35
- parents: { [id: ID<CoValue>]: Set<ID<CoValue>> } = {};
47
+ this.entries.set(root.id, this.rootEntry);
36
48
 
37
- constructor(
38
- root: Root,
39
- rootSchema: CoValueClass<Root> & CoValueFromRaw<Root>,
40
- onUpdate: (newRoot: Root) => void,
41
- ) {
42
- this.rootEntry = {
43
- state: "loaded" as const,
44
- value: root._raw,
45
- rawUnsub: () => {}, // placeholder
46
- };
47
- this.entries.set(root.id, this.rootEntry);
49
+ subscriptionsScopes.set(root, this);
48
50
 
49
- subscriptionsScopes.set(root, this);
51
+ this.subscriber = root._loadedAs;
52
+ this.scheduleUpdate = () => {
53
+ const value = rootSchema.fromRaw(this.rootEntry.value) as Root;
54
+ subscriptionsScopes.set(value, this);
55
+ onUpdate(value);
56
+ };
50
57
 
51
- this.subscriber = root._loadedAs;
52
- this.scheduleUpdate = () => {
53
- const value = rootSchema.fromRaw(this.rootEntry.value) as Root;
54
- subscriptionsScopes.set(value, this);
55
- onUpdate(value);
56
- };
58
+ this.rootEntry.rawUnsub = root._raw.core.subscribe(
59
+ (rawUpdate: RawCoValue | undefined) => {
60
+ if (!rawUpdate) return;
61
+ this.rootEntry.value = rawUpdate;
62
+ this.scheduleUpdate();
63
+ },
64
+ );
65
+ }
57
66
 
58
- this.rootEntry.rawUnsub = root._raw.core.subscribe(
59
- (rawUpdate: RawCoValue | undefined) => {
60
- if (!rawUpdate) return;
61
- this.rootEntry.value = rawUpdate;
62
- this.scheduleUpdate();
63
- },
64
- );
67
+ onRefAccessedOrSet(
68
+ fromId: ID<CoValue>,
69
+ accessedOrSetId: ID<CoValue> | undefined,
70
+ ) {
71
+ // console.log("onRefAccessedOrSet", this.scopeID, accessedOrSetId);
72
+ if (!accessedOrSetId) {
73
+ return;
65
74
  }
66
75
 
67
- onRefAccessedOrSet(
68
- fromId: ID<CoValue>,
69
- accessedOrSetId: ID<CoValue> | undefined,
70
- ) {
71
- // console.log("onRefAccessedOrSet", this.scopeID, accessedOrSetId);
72
- if (!accessedOrSetId) {
73
- return;
74
- }
75
-
76
- this.parents[accessedOrSetId] =
77
- this.parents[accessedOrSetId] || new Set();
78
- this.parents[accessedOrSetId]!.add(fromId);
76
+ this.parents[accessedOrSetId] = this.parents[accessedOrSetId] || new Set();
77
+ this.parents[accessedOrSetId]!.add(fromId);
79
78
 
80
- if (!this.entries.has(accessedOrSetId)) {
81
- const loadingEntry = {
82
- state: "loading",
83
- immediatelyUnsub: false,
84
- } as const;
85
- this.entries.set(accessedOrSetId, loadingEntry);
86
- const node =
87
- this.subscriber._type === "Account"
88
- ? this.subscriber._raw.core.node
89
- : this.subscriber.node;
90
- void node.loadCoValueCore(accessedOrSetId).then((core) => {
91
- if (
92
- loadingEntry.state === "loading" &&
93
- loadingEntry.immediatelyUnsub
94
- ) {
95
- return;
96
- }
97
- if (core !== "unavailable") {
98
- const entry = {
99
- state: "loaded" as const,
100
- rawUnsub: () => {}, // placeholder
101
- };
102
- this.entries.set(accessedOrSetId, entry);
79
+ if (!this.entries.has(accessedOrSetId)) {
80
+ const loadingEntry = {
81
+ state: "loading",
82
+ immediatelyUnsub: false,
83
+ } as const;
84
+ this.entries.set(accessedOrSetId, loadingEntry);
85
+ const node =
86
+ this.subscriber._type === "Account"
87
+ ? this.subscriber._raw.core.node
88
+ : this.subscriber.node;
89
+ void node.loadCoValueCore(accessedOrSetId).then((core) => {
90
+ if (loadingEntry.state === "loading" && loadingEntry.immediatelyUnsub) {
91
+ return;
92
+ }
93
+ if (core !== "unavailable") {
94
+ const entry = {
95
+ state: "loaded" as const,
96
+ rawUnsub: () => {}, // placeholder
97
+ };
98
+ this.entries.set(accessedOrSetId, entry);
103
99
 
104
- const rawUnsub = core.subscribe((rawUpdate) => {
105
- // console.log("ref update", this.scopeID, accessedOrSetId, JSON.stringify(rawUpdate))
106
- if (!rawUpdate) return;
107
- this.invalidate(accessedOrSetId);
108
- this.scheduleUpdate();
109
- });
100
+ const rawUnsub = core.subscribe((rawUpdate) => {
101
+ // console.log("ref update", this.scopeID, accessedOrSetId, JSON.stringify(rawUpdate))
102
+ if (!rawUpdate) return;
103
+ this.invalidate(accessedOrSetId);
104
+ this.scheduleUpdate();
105
+ });
110
106
 
111
- entry.rawUnsub = rawUnsub;
112
- }
113
- });
107
+ entry.rawUnsub = rawUnsub;
114
108
  }
109
+ });
115
110
  }
111
+ }
116
112
 
117
- invalidate(
118
- id: ID<CoValue>,
119
- fromChild?: ID<CoValue>,
120
- seen: Set<ID<CoValue>> = new Set(),
121
- ) {
122
- if (seen.has(id)) return;
123
- TRACE_INVALIDATIONS &&
124
- console.log(
125
- "invalidating",
126
- fromChild,
127
- "->",
128
- id,
129
- this.cachedValues[id],
130
- );
131
- delete this.cachedValues[id];
132
- seen.add(id);
133
- for (const parent of this.parents[id] || []) {
134
- this.invalidate(parent, id, seen);
135
- }
113
+ invalidate(
114
+ id: ID<CoValue>,
115
+ fromChild?: ID<CoValue>,
116
+ seen: Set<ID<CoValue>> = new Set(),
117
+ ) {
118
+ if (seen.has(id)) return;
119
+ TRACE_INVALIDATIONS &&
120
+ console.log("invalidating", fromChild, "->", id, this.cachedValues[id]);
121
+ delete this.cachedValues[id];
122
+ seen.add(id);
123
+ for (const parent of this.parents[id] || []) {
124
+ this.invalidate(parent, id, seen);
136
125
  }
126
+ }
137
127
 
138
- unsubscribeAll() {
139
- for (const entry of this.entries.values()) {
140
- if (entry.state === "loaded") {
141
- entry.rawUnsub();
142
- } else {
143
- entry.immediatelyUnsub = true;
144
- }
145
- }
146
- this.entries.clear();
128
+ unsubscribeAll() {
129
+ for (const entry of this.entries.values()) {
130
+ if (entry.state === "loaded") {
131
+ entry.rawUnsub();
132
+ } else {
133
+ entry.immediatelyUnsub = true;
134
+ }
147
135
  }
136
+ this.entries.clear();
137
+ }
148
138
  }
@@ -1,7 +1,7 @@
1
1
  export * from "./exports.js";
2
2
 
3
3
  export {
4
- cojsonInternals,
5
- MAX_RECOMMENDED_TX_SIZE,
6
- PureJSCrypto,
4
+ MAX_RECOMMENDED_TX_SIZE,
5
+ PureJSCrypto,
6
+ cojsonInternals,
7
7
  } from "cojson/native";
@@ -1,64 +1,64 @@
1
- import { expect, describe, test } from "vitest";
2
- import { coValuesCache } from "./cache.js";
3
1
  import { RawCoValue } from "cojson";
2
+ import { describe, expect, test } from "vitest";
4
3
  import { CoValue } from "../internal.js";
4
+ import { coValuesCache } from "./cache.js";
5
5
 
6
6
  describe("coValuesCache", () => {
7
- test("should return computed value when not cached", () => {
8
- const mockRawValue = { type: "comap" } as RawCoValue;
9
- const mockCoValue = { id: "test" } as unknown as CoValue;
10
- let computeCalls = 0;
11
-
12
- const result = coValuesCache.get(mockRawValue, () => {
13
- computeCalls++;
14
- return mockCoValue;
15
- });
7
+ test("should return computed value when not cached", () => {
8
+ const mockRawValue = { type: "comap" } as RawCoValue;
9
+ const mockCoValue = { id: "test" } as unknown as CoValue;
10
+ let computeCalls = 0;
16
11
 
17
- expect(result).toBe(mockCoValue);
18
- expect(computeCalls).toBe(1);
12
+ const result = coValuesCache.get(mockRawValue, () => {
13
+ computeCalls++;
14
+ return mockCoValue;
19
15
  });
20
16
 
21
- test("should return cached value on subsequent calls", () => {
22
- const mockRawValue = { type: "comap" } as RawCoValue;
23
- const mockCoValue = { id: "test" } as unknown as CoValue;
24
- let computeCalls = 0;
17
+ expect(result).toBe(mockCoValue);
18
+ expect(computeCalls).toBe(1);
19
+ });
25
20
 
26
- // First call
27
- const result1 = coValuesCache.get(mockRawValue, () => {
28
- computeCalls++;
29
- return mockCoValue;
30
- });
21
+ test("should return cached value on subsequent calls", () => {
22
+ const mockRawValue = { type: "comap" } as RawCoValue;
23
+ const mockCoValue = { id: "test" } as unknown as CoValue;
24
+ let computeCalls = 0;
31
25
 
32
- // Second call with same raw value
33
- const result2 = coValuesCache.get(mockRawValue, () => {
34
- computeCalls++;
35
- return mockCoValue;
36
- });
26
+ // First call
27
+ const result1 = coValuesCache.get(mockRawValue, () => {
28
+ computeCalls++;
29
+ return mockCoValue;
30
+ });
37
31
 
38
- expect(result1).toBe(mockCoValue);
39
- expect(result2).toBe(mockCoValue);
40
- expect(computeCalls).toBe(1); // Compute should only be called once
32
+ // Second call with same raw value
33
+ const result2 = coValuesCache.get(mockRawValue, () => {
34
+ computeCalls++;
35
+ return mockCoValue;
41
36
  });
42
37
 
43
- test("should cache different values for different raw values", () => {
44
- const mockRawValue1 = { type: "comap" } as RawCoValue;
45
- const mockRawValue2 = { type: "colist" } as RawCoValue;
46
- const mockCoValue1 = { id: "test1" } as unknown as CoValue;
47
- const mockCoValue2 = { id: "test2" } as unknown as CoValue;
48
- let computeCalls = 0;
38
+ expect(result1).toBe(mockCoValue);
39
+ expect(result2).toBe(mockCoValue);
40
+ expect(computeCalls).toBe(1); // Compute should only be called once
41
+ });
49
42
 
50
- const result1 = coValuesCache.get(mockRawValue1, () => {
51
- computeCalls++;
52
- return mockCoValue1;
53
- });
43
+ test("should cache different values for different raw values", () => {
44
+ const mockRawValue1 = { type: "comap" } as RawCoValue;
45
+ const mockRawValue2 = { type: "colist" } as RawCoValue;
46
+ const mockCoValue1 = { id: "test1" } as unknown as CoValue;
47
+ const mockCoValue2 = { id: "test2" } as unknown as CoValue;
48
+ let computeCalls = 0;
54
49
 
55
- const result2 = coValuesCache.get(mockRawValue2, () => {
56
- computeCalls++;
57
- return mockCoValue2;
58
- });
50
+ const result1 = coValuesCache.get(mockRawValue1, () => {
51
+ computeCalls++;
52
+ return mockCoValue1;
53
+ });
59
54
 
60
- expect(result1).toBe(mockCoValue1);
61
- expect(result2).toBe(mockCoValue2);
62
- expect(computeCalls).toBe(2); // Should compute once for each unique raw value
55
+ const result2 = coValuesCache.get(mockRawValue2, () => {
56
+ computeCalls++;
57
+ return mockCoValue2;
63
58
  });
64
- });
59
+
60
+ expect(result1).toBe(mockCoValue1);
61
+ expect(result2).toBe(mockCoValue2);
62
+ expect(computeCalls).toBe(2); // Should compute once for each unique raw value
63
+ });
64
+ });
package/src/lib/cache.ts CHANGED
@@ -4,13 +4,13 @@ import { CoValue } from "../internal.js";
4
4
  const weakMap = new WeakMap<RawCoValue, CoValue>();
5
5
 
6
6
  export const coValuesCache = {
7
- get: <V extends CoValue>(raw: RawCoValue, compute: () => V) => {
8
- const cached = weakMap.get(raw);
9
- if (cached) {
10
- return cached as V;
11
- }
12
- const computed = compute();
13
- weakMap.set(raw, computed);
14
- return computed;
15
- },
7
+ get: <V extends CoValue>(raw: RawCoValue, compute: () => V) => {
8
+ const cached = weakMap.get(raw);
9
+ if (cached) {
10
+ return cached as V;
11
+ }
12
+ const computed = compute();
13
+ weakMap.set(raw, computed);
14
+ return computed;
15
+ },
16
16
  };