jazz-tools 0.13.17 → 0.13.19

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 (82) hide show
  1. package/.turbo/turbo-build.log +7 -7
  2. package/CHANGELOG.md +17 -0
  3. package/dist/{chunk-PYBQOYML.js → chunk-4HBHY4I7.js} +735 -595
  4. package/dist/chunk-4HBHY4I7.js.map +1 -0
  5. package/dist/coValues/account.d.ts +5 -4
  6. package/dist/coValues/account.d.ts.map +1 -1
  7. package/dist/coValues/coFeed.d.ts +1 -0
  8. package/dist/coValues/coFeed.d.ts.map +1 -1
  9. package/dist/coValues/coList.d.ts +1 -0
  10. package/dist/coValues/coList.d.ts.map +1 -1
  11. package/dist/coValues/coMap.d.ts +4 -2
  12. package/dist/coValues/coMap.d.ts.map +1 -1
  13. package/dist/coValues/coPlainText.d.ts +2 -2
  14. package/dist/coValues/coPlainText.d.ts.map +1 -1
  15. package/dist/coValues/deepLoading.d.ts +1 -9
  16. package/dist/coValues/deepLoading.d.ts.map +1 -1
  17. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  18. package/dist/coValues/group.d.ts.map +1 -1
  19. package/dist/coValues/inbox.d.ts.map +1 -1
  20. package/dist/coValues/interfaces.d.ts +4 -1
  21. package/dist/coValues/interfaces.d.ts.map +1 -1
  22. package/dist/implementation/createContext.d.ts.map +1 -1
  23. package/dist/implementation/refs.d.ts +5 -10
  24. package/dist/implementation/refs.d.ts.map +1 -1
  25. package/dist/index.js +1 -1
  26. package/dist/internal.d.ts +1 -1
  27. package/dist/internal.d.ts.map +1 -1
  28. package/dist/subscribe/CoValueCoreSubscription.d.ts +14 -0
  29. package/dist/subscribe/CoValueCoreSubscription.d.ts.map +1 -0
  30. package/dist/subscribe/JazzError.d.ts +16 -0
  31. package/dist/subscribe/JazzError.d.ts.map +1 -0
  32. package/dist/subscribe/SubscriptionScope.d.ts +43 -0
  33. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -0
  34. package/dist/subscribe/index.d.ts +21 -0
  35. package/dist/subscribe/index.d.ts.map +1 -0
  36. package/dist/subscribe/types.d.ts +12 -0
  37. package/dist/subscribe/types.d.ts.map +1 -0
  38. package/dist/subscribe/utils.d.ts +10 -0
  39. package/dist/subscribe/utils.d.ts.map +1 -0
  40. package/dist/testing.js +2 -2
  41. package/dist/testing.js.map +1 -1
  42. package/dist/tests/coMap.record.test.d.ts +2 -0
  43. package/dist/tests/coMap.record.test.d.ts.map +1 -0
  44. package/dist/tests/utils.d.ts +2 -2
  45. package/dist/tests/utils.d.ts.map +1 -1
  46. package/package.json +2 -2
  47. package/src/coValues/account.ts +43 -31
  48. package/src/coValues/coFeed.ts +11 -7
  49. package/src/coValues/coList.ts +13 -17
  50. package/src/coValues/coMap.ts +72 -80
  51. package/src/coValues/coPlainText.ts +13 -2
  52. package/src/coValues/deepLoading.ts +4 -277
  53. package/src/coValues/extensions/imageDef.ts +1 -7
  54. package/src/coValues/group.ts +7 -6
  55. package/src/coValues/inbox.ts +4 -11
  56. package/src/coValues/interfaces.ts +54 -111
  57. package/src/implementation/createContext.ts +3 -4
  58. package/src/implementation/invites.ts +2 -2
  59. package/src/implementation/refs.ts +30 -121
  60. package/src/internal.ts +1 -2
  61. package/src/subscribe/CoValueCoreSubscription.ts +71 -0
  62. package/src/subscribe/JazzError.ts +48 -0
  63. package/src/subscribe/SubscriptionScope.ts +536 -0
  64. package/src/subscribe/index.ts +82 -0
  65. package/src/subscribe/types.ts +7 -0
  66. package/src/subscribe/utils.ts +36 -0
  67. package/src/testing.ts +1 -1
  68. package/src/tests/ContextManager.test.ts +13 -9
  69. package/src/tests/coFeed.test.ts +6 -6
  70. package/src/tests/coList.test.ts +304 -115
  71. package/src/tests/coMap.record.test.ts +325 -0
  72. package/src/tests/coMap.test.ts +718 -645
  73. package/src/tests/coPlainText.test.ts +2 -2
  74. package/src/tests/createContext.test.ts +8 -8
  75. package/src/tests/deepLoading.test.ts +8 -34
  76. package/src/tests/groupsAndAccounts.test.ts +6 -4
  77. package/src/tests/subscribe.test.ts +614 -42
  78. package/src/tests/utils.ts +8 -6
  79. package/dist/chunk-PYBQOYML.js.map +0 -1
  80. package/dist/implementation/subscriptionScope.d.ts +0 -34
  81. package/dist/implementation/subscriptionScope.d.ts.map +0 -1
  82. package/src/implementation/subscriptionScope.ts +0 -165
@@ -1,34 +0,0 @@
1
- import type { RawCoValue } from "cojson";
2
- import { type Account } from "../coValues/account.js";
3
- import type { AnonymousJazzAgent, CoValue, CoValueClass, CoValueFromRaw, ID } from "../internal.js";
4
- export declare const subscriptionsScopes: WeakMap<CoValue, SubscriptionScope<any>>;
5
- export declare class SubscriptionScope<Root extends CoValue> {
6
- scopeID: string;
7
- subscriber: Account | AnonymousJazzAgent;
8
- entries: Map<ID<CoValue>, {
9
- state: "loading";
10
- immediatelyUnsub?: boolean;
11
- } | {
12
- state: "loaded";
13
- rawUnsub: () => void;
14
- }>;
15
- rootEntry: {
16
- state: "loaded";
17
- value: RawCoValue;
18
- rawUnsub: () => void;
19
- };
20
- scheduleUpdate: () => void;
21
- scheduledUpdate: boolean;
22
- cachedValues: {
23
- [id: ID<CoValue>]: CoValue;
24
- };
25
- parents: {
26
- [id: ID<CoValue>]: Set<ID<CoValue>>;
27
- };
28
- syncResolution: boolean;
29
- constructor(root: Root, rootSchema: CoValueClass<Root> & CoValueFromRaw<Root>, onUpdate: (newRoot: Root, scope: SubscriptionScope<Root>) => void);
30
- onRefAccessedOrSet(fromId: ID<CoValue>, accessedOrSetId: ID<CoValue> | undefined): void;
31
- invalidate(id: ID<CoValue>, seen?: Set<ID<CoValue>>): void;
32
- unsubscribeAll: () => void;
33
- }
34
- //# sourceMappingURL=subscriptionScope.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"subscriptionScope.d.ts","sourceRoot":"","sources":["../../src/implementation/subscriptionScope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA0B,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC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AE,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,CAAa;IAU9D,cAAc,aASZ;CACH"}
@@ -1,165 +0,0 @@
1
- import type { CoValueCore, LocalNode, RawCoValue } from "cojson";
2
- import { type Account } from "../coValues/account.js";
3
- import type {
4
- AnonymousJazzAgent,
5
- CoValue,
6
- CoValueClass,
7
- CoValueFromRaw,
8
- ID,
9
- } from "../internal.js";
10
-
11
- export const subscriptionsScopes = new WeakMap<
12
- CoValue,
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- SubscriptionScope<any>
15
- >();
16
-
17
- export class SubscriptionScope<Root extends CoValue> {
18
- scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
19
- subscriber: Account | AnonymousJazzAgent;
20
- entries = new Map<
21
- ID<CoValue>,
22
- | { state: "loading"; immediatelyUnsub?: boolean }
23
- | { state: "loaded"; rawUnsub: () => void }
24
- >();
25
- rootEntry: {
26
- state: "loaded";
27
- value: RawCoValue;
28
- rawUnsub: () => void;
29
- };
30
- scheduleUpdate: () => void;
31
- scheduledUpdate: boolean = false;
32
- cachedValues: { [id: ID<CoValue>]: CoValue } = {};
33
- parents: { [id: ID<CoValue>]: Set<ID<CoValue>> } = {};
34
- syncResolution: boolean = false;
35
-
36
- constructor(
37
- root: Root,
38
- rootSchema: CoValueClass<Root> & CoValueFromRaw<Root>,
39
- onUpdate: (newRoot: Root, scope: SubscriptionScope<Root>) => void,
40
- ) {
41
- this.rootEntry = {
42
- state: "loaded" as const,
43
- value: root._raw,
44
- rawUnsub: () => {}, // placeholder
45
- };
46
- this.entries.set(root.id, this.rootEntry);
47
-
48
- subscriptionsScopes.set(root, this);
49
-
50
- this.subscriber = root._loadedAs;
51
-
52
- this.scheduleUpdate = () => {
53
- const value = rootSchema.fromRaw(this.rootEntry.value) as Root;
54
- subscriptionsScopes.set(value, this);
55
- onUpdate(value, this);
56
- };
57
-
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
- }
66
-
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] = this.parents[accessedOrSetId] || new Set();
77
- this.parents[accessedOrSetId]!.add(fromId);
78
-
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
-
90
- loadCoValue(
91
- node,
92
- accessedOrSetId,
93
- (core) => {
94
- if (
95
- loadingEntry.state === "loading" &&
96
- loadingEntry.immediatelyUnsub
97
- ) {
98
- return;
99
- }
100
- if (core !== "unavailable") {
101
- const entry = {
102
- state: "loaded" as const,
103
- rawUnsub: () => {}, // placeholder
104
- };
105
- this.entries.set(accessedOrSetId, entry);
106
-
107
- const rawUnsub = core.subscribe((rawUpdate) => {
108
- if (!rawUpdate) return;
109
-
110
- this.invalidate(accessedOrSetId);
111
- this.scheduleUpdate();
112
- });
113
-
114
- entry.rawUnsub = rawUnsub;
115
- }
116
- },
117
- this.syncResolution,
118
- );
119
- }
120
- }
121
-
122
- invalidate(id: ID<CoValue>, seen: Set<ID<CoValue>> = new Set()) {
123
- if (seen.has(id)) return;
124
-
125
- delete this.cachedValues[id];
126
- seen.add(id);
127
- for (const parent of this.parents[id] || []) {
128
- this.invalidate(parent, seen);
129
- }
130
- }
131
-
132
- unsubscribeAll = () => {
133
- for (const entry of this.entries.values()) {
134
- if (entry.state === "loaded") {
135
- entry.rawUnsub();
136
- } else {
137
- entry.immediatelyUnsub = true;
138
- }
139
- }
140
- this.entries.clear();
141
- };
142
- }
143
-
144
- /**
145
- * Loads a CoValue from the node and calls the callback with the result.
146
-
147
- * If the CoValue is already loaded, the callback is called synchronously.
148
- * If the CoValue is not loaded, the callback is called asynchronously.
149
- */
150
- function loadCoValue(
151
- node: LocalNode,
152
- id: ID<CoValue>,
153
- callback: (value: CoValueCore | "unavailable") => void,
154
- syncResolution: boolean,
155
- ) {
156
- const entry = node.coValuesStore.get(id);
157
-
158
- if (entry.isAvailable() && syncResolution) {
159
- callback(entry.core);
160
- } else {
161
- void node.loadCoValueCore(id).then((core) => {
162
- callback(core);
163
- });
164
- }
165
- }