jazz-tools 0.8.15 → 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 +276 -269
  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 +1 -2
  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 +1 -2
  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 -26
  74. package/src/implementation/createContext.ts +206 -213
  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
@@ -2,60 +2,60 @@ import type { CojsonInternalTypes, RawCoValue } from "cojson";
2
2
  import { RawAccount } from "cojson";
3
3
  import type { DeeplyLoaded, DepthsIn } from "../internal.js";
4
4
  import {
5
- Account,
6
- Group,
7
- SubscriptionScope,
8
- Ref,
9
- inspect,
10
- subscriptionsScopes,
11
- AnonymousJazzAgent,
5
+ Account,
6
+ AnonymousJazzAgent,
7
+ Group,
8
+ Ref,
9
+ SubscriptionScope,
10
+ inspect,
11
+ subscriptionsScopes,
12
12
  } from "../internal.js";
13
- import { fulfillsDepth } from "./deepLoading.js";
14
13
  import { coValuesCache } from "../lib/cache.js";
14
+ import { fulfillsDepth } from "./deepLoading.js";
15
15
 
16
16
  /** @category Abstract interfaces */
17
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
18
  export interface CoValueClass<Value extends CoValue = CoValue> {
19
- /** @ignore */
20
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- new (...args: any[]): Value;
19
+ /** @ignore */
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ new (...args: any[]): Value;
22
22
  }
23
23
 
24
24
  export interface CoValueFromRaw<V extends CoValue> {
25
- fromRaw(raw: V["_raw"]): V;
25
+ fromRaw(raw: V["_raw"]): V;
26
26
  }
27
27
 
28
28
  /** @category Abstract interfaces */
29
29
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
30
  export interface CoValue {
31
- /** @category Content */
32
- readonly id: ID<this>;
33
- /** @category Type Helpers */
34
- _type: string;
35
- /** @category Collaboration */
36
- _owner: Account | Group;
37
- /** @category Internals */
38
- _raw: RawCoValue;
39
- /** @internal */
40
- readonly _loadedAs: Account | AnonymousJazzAgent;
41
- /** @category Stringifying & Inspection */
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- toJSON(key?: string, seenAbove?: ID<CoValue>[]): any[] | object | string;
44
- /** @category Stringifying & Inspection */
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- [inspect](): any;
31
+ /** @category Content */
32
+ readonly id: ID<this>;
33
+ /** @category Type Helpers */
34
+ _type: string;
35
+ /** @category Collaboration */
36
+ _owner: Account | Group;
37
+ /** @category Internals */
38
+ _raw: RawCoValue;
39
+ /** @internal */
40
+ readonly _loadedAs: Account | AnonymousJazzAgent;
41
+ /** @category Stringifying & Inspection */
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ toJSON(key?: string, seenAbove?: ID<CoValue>[]): any[] | object | string;
44
+ /** @category Stringifying & Inspection */
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ [inspect](): any;
47
47
  }
48
48
 
49
49
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
50
  export function isCoValue(value: any): value is CoValue {
51
- return value && value._type !== undefined;
51
+ return value && value._type !== undefined;
52
52
  }
53
53
 
54
54
  export function isCoValueClass<V extends CoValue>(
55
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- value: any,
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ value: any,
57
57
  ): value is CoValueClass<V> & CoValueFromRaw<V> {
58
- return typeof value === "function" && value.fromRaw !== undefined;
58
+ return typeof value === "function" && value.fromRaw !== undefined;
59
59
  }
60
60
 
61
61
  /** @category CoValues */
@@ -65,202 +65,200 @@ type IDMarker<out T> = { __type(_: never): T };
65
65
 
66
66
  /** @internal */
67
67
  export class CoValueBase implements CoValue {
68
- declare id: ID<this>;
69
- declare _type: string;
70
- declare _raw: RawCoValue;
71
- /** @category Internals */
72
- declare _instanceID: string;
73
-
74
- get _owner(): Account | Group {
75
- const owner =
76
- this._raw.group instanceof RawAccount
77
- ? Account.fromRaw(this._raw.group)
78
- : Group.fromRaw(this._raw.group);
79
-
80
- const subScope = subscriptionsScopes.get(this);
81
- if (subScope) {
82
- subScope.onRefAccessedOrSet(this.id, owner.id);
83
- subscriptionsScopes.set(owner, subScope);
84
- }
85
-
86
- return owner;
68
+ declare id: ID<this>;
69
+ declare _type: string;
70
+ declare _raw: RawCoValue;
71
+ /** @category Internals */
72
+ declare _instanceID: string;
73
+
74
+ get _owner(): Account | Group {
75
+ const owner =
76
+ this._raw.group instanceof RawAccount
77
+ ? Account.fromRaw(this._raw.group)
78
+ : Group.fromRaw(this._raw.group);
79
+
80
+ const subScope = subscriptionsScopes.get(this);
81
+ if (subScope) {
82
+ subScope.onRefAccessedOrSet(this.id, owner.id);
83
+ subscriptionsScopes.set(owner, subScope);
87
84
  }
88
85
 
89
- /** @private */
90
- get _loadedAs() {
91
- const rawAccount = this._raw.core.node.account;
86
+ return owner;
87
+ }
92
88
 
93
- if (rawAccount instanceof RawAccount) {
94
- return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount)) ;
95
- }
89
+ /** @private */
90
+ get _loadedAs() {
91
+ const rawAccount = this._raw.core.node.account;
96
92
 
97
- return new AnonymousJazzAgent(this._raw.core.node);
93
+ if (rawAccount instanceof RawAccount) {
94
+ return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount));
98
95
  }
99
96
 
100
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- constructor(..._args: any) {
102
- Object.defineProperty(this, "_instanceID", {
103
- value: `instance-${Math.random().toString(36).slice(2)}`,
104
- enumerable: false,
105
- });
106
- }
107
-
108
- /** @category Internals */
109
- static fromRaw<V extends CoValue>(
110
- this: CoValueClass<V>,
111
- raw: RawCoValue,
112
- ): V {
113
- return new this({ fromRaw: raw });
114
- }
115
-
116
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
- toJSON(): object | any[] | string {
118
- return {
119
- id: this.id,
120
- type: this._type,
121
- error: "unknown CoValue class",
122
- };
123
- }
124
-
125
- [inspect]() {
126
- return this.toJSON();
127
- }
97
+ return new AnonymousJazzAgent(this._raw.core.node);
98
+ }
128
99
 
129
- /** @category Type Helpers */
130
- castAs<Cl extends CoValueClass & CoValueFromRaw<CoValue>>(
131
- cl: Cl,
132
- ): InstanceType<Cl> {
133
- const casted = cl.fromRaw(this._raw) as InstanceType<Cl>;
134
- const subscriptionScope = subscriptionsScopes.get(this);
135
- if (subscriptionScope) {
136
- subscriptionsScopes.set(casted, subscriptionScope);
137
- }
138
- return casted;
100
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
+ constructor(..._args: any) {
102
+ Object.defineProperty(this, "_instanceID", {
103
+ value: `instance-${Math.random().toString(36).slice(2)}`,
104
+ enumerable: false,
105
+ });
106
+ }
107
+
108
+ /** @category Internals */
109
+ static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: RawCoValue): V {
110
+ return new this({ fromRaw: raw });
111
+ }
112
+
113
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
114
+ toJSON(): object | any[] | string {
115
+ return {
116
+ id: this.id,
117
+ type: this._type,
118
+ error: "unknown CoValue class",
119
+ };
120
+ }
121
+
122
+ [inspect]() {
123
+ return this.toJSON();
124
+ }
125
+
126
+ /** @category Type Helpers */
127
+ castAs<Cl extends CoValueClass & CoValueFromRaw<CoValue>>(
128
+ cl: Cl,
129
+ ): InstanceType<Cl> {
130
+ const casted = cl.fromRaw(this._raw) as InstanceType<Cl>;
131
+ const subscriptionScope = subscriptionsScopes.get(this);
132
+ if (subscriptionScope) {
133
+ subscriptionsScopes.set(casted, subscriptionScope);
139
134
  }
135
+ return casted;
136
+ }
140
137
  }
141
138
 
142
139
  export function loadCoValue<V extends CoValue, Depth>(
143
- cls: CoValueClass<V>,
144
- id: ID<V>,
145
- as: Account | AnonymousJazzAgent,
146
- depth: Depth & DepthsIn<V>,
140
+ cls: CoValueClass<V>,
141
+ id: ID<V>,
142
+ as: Account | AnonymousJazzAgent,
143
+ depth: Depth & DepthsIn<V>,
147
144
  ): Promise<DeeplyLoaded<V, Depth> | undefined> {
148
- return new Promise((resolve) => {
149
- const unsubscribe = subscribeToCoValue(
150
- cls,
151
- id,
152
- as,
153
- depth,
154
- (value) => {
155
- resolve(value);
156
- unsubscribe();
157
- },
158
- () => {
159
- resolve(undefined);
160
- unsubscribe();
161
- },
162
- );
163
- });
145
+ return new Promise((resolve) => {
146
+ const unsubscribe = subscribeToCoValue(
147
+ cls,
148
+ id,
149
+ as,
150
+ depth,
151
+ (value) => {
152
+ resolve(value);
153
+ unsubscribe();
154
+ },
155
+ () => {
156
+ resolve(undefined);
157
+ unsubscribe();
158
+ },
159
+ );
160
+ });
164
161
  }
165
162
 
166
163
  export function ensureCoValueLoaded<V extends CoValue, Depth>(
167
- existing: V,
168
- depth: Depth & DepthsIn<V>,
164
+ existing: V,
165
+ depth: Depth & DepthsIn<V>,
169
166
  ): Promise<DeeplyLoaded<V, Depth> | undefined> {
170
- return loadCoValue(
171
- existing.constructor as CoValueClass<V>,
172
- existing.id,
173
- existing._loadedAs,
174
- depth,
175
- );
167
+ return loadCoValue(
168
+ existing.constructor as CoValueClass<V>,
169
+ existing.id,
170
+ existing._loadedAs,
171
+ depth,
172
+ );
176
173
  }
177
174
 
178
175
  export function subscribeToCoValue<V extends CoValue, Depth>(
176
+ cls: CoValueClass<V>,
177
+ id: ID<V>,
178
+ as: Account | AnonymousJazzAgent,
179
+ depth: Depth & DepthsIn<V>,
180
+ listener: (value: DeeplyLoaded<V, Depth>) => void,
181
+ onUnavailable?: () => void,
182
+ ): () => void {
183
+ const ref = new Ref(id, as, { ref: cls, optional: false });
184
+
185
+ let unsubscribed = false;
186
+ let unsubscribe: (() => void) | undefined;
187
+
188
+ ref
189
+ .load()
190
+ .then((value) => {
191
+ if (!value) {
192
+ onUnavailable && onUnavailable();
193
+ return;
194
+ }
195
+ if (unsubscribed) return;
196
+ const subscription = new SubscriptionScope(
197
+ value,
198
+ cls as CoValueClass<V> & CoValueFromRaw<V>,
199
+ (update) => {
200
+ if (fulfillsDepth(depth, update)) {
201
+ listener(update as DeeplyLoaded<V, Depth>);
202
+ }
203
+ },
204
+ );
205
+
206
+ unsubscribe = () => subscription.unsubscribeAll();
207
+ })
208
+ .catch((e) => {
209
+ console.error("Failed to load / subscribe to CoValue", e);
210
+ });
211
+
212
+ return function unsubscribeAtAnyPoint() {
213
+ unsubscribed = true;
214
+ unsubscribe && unsubscribe();
215
+ };
216
+ }
217
+
218
+ export function createCoValueObservable<V extends CoValue, Depth>() {
219
+ let currentValue: DeeplyLoaded<V, Depth> | undefined = undefined;
220
+
221
+ function subscribe(
179
222
  cls: CoValueClass<V>,
180
223
  id: ID<V>,
181
224
  as: Account | AnonymousJazzAgent,
182
225
  depth: Depth & DepthsIn<V>,
183
- listener: (value: DeeplyLoaded<V, Depth>) => void,
226
+ listener: () => void,
184
227
  onUnavailable?: () => void,
185
- ): () => void {
186
- const ref = new Ref(id, as, { ref: cls, optional: false });
187
-
188
- let unsubscribed = false;
189
- let unsubscribe: (() => void) | undefined;
190
-
191
- ref.load()
192
- .then((value) => {
193
- if (!value) {
194
- onUnavailable && onUnavailable();
195
- return;
196
- }
197
- if (unsubscribed) return;
198
- const subscription = new SubscriptionScope(
199
- value,
200
- cls as CoValueClass<V> & CoValueFromRaw<V>,
201
- (update) => {
202
- if (fulfillsDepth(depth, update)) {
203
- listener(update as DeeplyLoaded<V, Depth>);
204
- }
205
- },
206
- );
207
-
208
- unsubscribe = () => subscription.unsubscribeAll();
209
- })
210
- .catch((e) => {
211
- console.error("Failed to load / subscribe to CoValue", e);
212
- });
213
-
214
- return function unsubscribeAtAnyPoint() {
215
- unsubscribed = true;
216
- unsubscribe && unsubscribe();
217
- };
218
- }
228
+ ) {
229
+ const unsubscribe = subscribeToCoValue(
230
+ cls,
231
+ id,
232
+ as,
233
+ depth,
234
+ (value) => {
235
+ currentValue = value;
236
+ listener();
237
+ },
238
+ onUnavailable,
239
+ );
219
240
 
220
- export function createCoValueObservable<V extends CoValue, Depth>() {
221
- let currentValue: DeeplyLoaded<V, Depth> | undefined = undefined;
222
-
223
- function subscribe(
224
- cls: CoValueClass<V>,
225
- id: ID<V>,
226
- as: Account | AnonymousJazzAgent,
227
- depth: Depth & DepthsIn<V>,
228
- listener: () => void,
229
- onUnavailable?: () => void,
230
- ) {
231
- const unsubscribe = subscribeToCoValue(
232
- cls,
233
- id,
234
- as,
235
- depth,
236
- (value) => {
237
- currentValue = value;
238
- listener();
239
- },
240
- onUnavailable,
241
- );
242
-
243
- return unsubscribe
244
- }
241
+ return unsubscribe;
242
+ }
245
243
 
246
- const observable = {
247
- getCurrentValue: () => currentValue,
248
- subscribe,
249
- };
244
+ const observable = {
245
+ getCurrentValue: () => currentValue,
246
+ subscribe,
247
+ };
250
248
 
251
- return observable;
249
+ return observable;
252
250
  }
253
251
 
254
252
  export function subscribeToExistingCoValue<V extends CoValue, Depth>(
255
- existing: V,
256
- depth: Depth & DepthsIn<V>,
257
- listener: (value: DeeplyLoaded<V, Depth>) => void,
253
+ existing: V,
254
+ depth: Depth & DepthsIn<V>,
255
+ listener: (value: DeeplyLoaded<V, Depth>) => void,
258
256
  ): () => void {
259
- return subscribeToCoValue(
260
- existing.constructor as CoValueClass<V>,
261
- existing.id,
262
- existing._loadedAs,
263
- depth,
264
- listener,
265
- );
257
+ return subscribeToCoValue(
258
+ existing.constructor as CoValueClass<V>,
259
+ existing.id,
260
+ existing._loadedAs,
261
+ depth,
262
+ listener,
263
+ );
266
264
  }
package/src/exports.ts CHANGED
@@ -1,36 +1,48 @@
1
1
  export type {
2
- InviteSecret,
3
- Peer,
4
- SessionID,
5
- AgentID,
6
- SyncMessage,
7
- CryptoProvider,
8
- CoValueUniqueness,
2
+ AgentID,
3
+ CoValueUniqueness,
4
+ CryptoProvider,
5
+ InviteSecret,
6
+ Peer,
7
+ SessionID,
8
+ SyncMessage,
9
9
  } from "cojson";
10
10
 
11
- export type { ID, CoValue } from "./internal.js";
11
+ export type { CoValue, ID } from "./internal.js";
12
12
 
13
13
  export { Encoders, co } from "./internal.js";
14
14
 
15
- export { CoMap, type CoMapInit } from "./internal.js";
16
- export { CoList } from "./internal.js";
17
- export { CoStream, BinaryCoStream } from "./internal.js";
18
- export { Group, Profile } from "./internal.js";
19
- export { Account, isControlledAccount, type AccountClass } from "./internal.js";
20
- export { ImageDefinition } from "./internal.js";
21
- export { CoValueBase, type CoValueClass } from "./internal.js";
22
- export type { DepthsIn, DeeplyLoaded } from "./internal.js";
15
+ export {
16
+ Account,
17
+ BinaryCoStream,
18
+ CoList,
19
+ CoMap,
20
+ CoStream,
21
+ CoValueBase,
22
+ Group,
23
+ ImageDefinition,
24
+ Profile,
25
+ isControlledAccount,
26
+ type AccountClass,
27
+ type CoMapInit,
28
+ type CoValueClass,
29
+ } from "./internal.js";
30
+ export type { DeeplyLoaded, DepthsIn } from "./internal.js";
23
31
 
24
- export { loadCoValue, subscribeToCoValue, createCoValueObservable } from "./internal.js";
32
+ export {
33
+ createCoValueObservable,
34
+ loadCoValue,
35
+ subscribeToCoValue,
36
+ } from "./internal.js";
25
37
 
26
38
  export {
27
- type AuthMethod,
28
- type AuthResult,
29
- type Credentials,
30
- createJazzContext,
31
- fixedCredentialsAuth,
32
- ephemeralCredentialsAuth,
33
- AnonymousJazzAgent,
34
- createAnonymousJazzContext,
35
- randomSessionProvider,
39
+ AnonymousJazzAgent,
40
+ createAnonymousJazzContext,
41
+ createJazzContext,
42
+ ephemeralCredentialsAuth,
43
+ fixedCredentialsAuth,
44
+ randomSessionProvider,
45
+ type AuthMethod,
46
+ type AuthResult,
47
+ type Credentials,
36
48
  } from "./internal.js";