jazz-tools 0.7.0-alpha.36 → 0.7.0-alpha.38

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. package/.eslintrc.cjs +3 -10
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +3 -91
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +140 -0
  6. package/CHANGELOG.md +19 -0
  7. package/README.md +10 -2
  8. package/dist/coValues/account.js +22 -12
  9. package/dist/coValues/account.js.map +1 -1
  10. package/dist/coValues/coList.js +7 -9
  11. package/dist/coValues/coList.js.map +1 -1
  12. package/dist/coValues/coMap.js +11 -3
  13. package/dist/coValues/coMap.js.map +1 -1
  14. package/dist/coValues/coStream.js +15 -8
  15. package/dist/coValues/coStream.js.map +1 -1
  16. package/dist/coValues/deepLoading.js +57 -0
  17. package/dist/coValues/deepLoading.js.map +1 -0
  18. package/dist/coValues/extensions/imageDef.js +1 -1
  19. package/dist/coValues/extensions/imageDef.js.map +1 -1
  20. package/dist/coValues/group.js +1 -0
  21. package/dist/coValues/group.js.map +1 -1
  22. package/dist/coValues/interfaces.js +34 -28
  23. package/dist/coValues/interfaces.js.map +1 -1
  24. package/dist/implementation/devtoolsFormatters.js +1 -0
  25. package/dist/implementation/devtoolsFormatters.js.map +1 -1
  26. package/dist/implementation/refs.js +4 -2
  27. package/dist/implementation/refs.js.map +1 -1
  28. package/dist/implementation/schema.js +5 -1
  29. package/dist/implementation/schema.js.map +1 -1
  30. package/dist/implementation/subscriptionScope.js +9 -4
  31. package/dist/implementation/subscriptionScope.js.map +1 -1
  32. package/dist/implementation/symbols.js.map +1 -1
  33. package/dist/index.js +2 -4
  34. package/dist/index.js.map +1 -1
  35. package/dist/internal.js +2 -1
  36. package/dist/internal.js.map +1 -1
  37. package/dist/tests/coList.test.js +20 -17
  38. package/dist/tests/coList.test.js.map +1 -1
  39. package/dist/tests/coMap.test.js +27 -17
  40. package/dist/tests/coMap.test.js.map +1 -1
  41. package/dist/tests/coStream.test.js +39 -24
  42. package/dist/tests/coStream.test.js.map +1 -1
  43. package/dist/tests/deepLoading.test.js +188 -0
  44. package/dist/tests/deepLoading.test.js.map +1 -0
  45. package/dist/tests/groupsAndAccounts.test.js +6 -11
  46. package/dist/tests/groupsAndAccounts.test.js.map +1 -1
  47. package/package.json +12 -6
  48. package/src/coValues/account.ts +60 -58
  49. package/src/coValues/coList.ts +31 -51
  50. package/src/coValues/coMap.ts +27 -21
  51. package/src/coValues/coStream.ts +44 -32
  52. package/src/coValues/deepLoading.ts +215 -0
  53. package/src/coValues/extensions/imageDef.ts +3 -3
  54. package/src/coValues/group.ts +11 -13
  55. package/src/coValues/interfaces.ts +151 -93
  56. package/src/implementation/devtoolsFormatters.ts +1 -0
  57. package/src/implementation/inspect.ts +1 -1
  58. package/src/implementation/refs.ts +29 -15
  59. package/src/implementation/schema.ts +21 -6
  60. package/src/implementation/subscriptionScope.ts +33 -17
  61. package/src/implementation/symbols.ts +0 -1
  62. package/src/index.ts +5 -3
  63. package/src/internal.ts +3 -2
  64. package/src/tests/coList.test.ts +33 -28
  65. package/src/tests/coMap.test.ts +63 -55
  66. package/src/tests/coStream.test.ts +82 -67
  67. package/src/tests/deepLoading.test.ts +301 -0
  68. package/src/tests/groupsAndAccounts.test.ts +9 -18
@@ -1,23 +1,15 @@
1
- import { expect, describe, test, beforeEach } from "vitest";
2
-
3
- import { webcrypto } from "node:crypto";
1
+ import { expect, describe, test } from "vitest";
4
2
  import { connectedPeers } from "cojson/src/streamUtils.js";
5
3
  import { newRandomSessionID } from "cojson/src/coValueCore.js";
6
4
  import { Effect, Queue } from "effect";
7
- import { Account, jazzReady, Encoders, CoMap, co } from "..";
8
-
9
- if (!("crypto" in globalThis)) {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- (globalThis as any).crypto = webcrypto;
12
- }
5
+ import { Account, Encoders, CoMap, co, WasmCrypto, isControlledAccount } from "../index.js";
13
6
 
14
- beforeEach(async () => {
15
- await jazzReady;
16
- });
7
+ const Crypto = await WasmCrypto.create();
17
8
 
18
9
  describe("Simple CoMap operations", async () => {
19
10
  const me = await Account.create({
20
11
  creationProps: { name: "Hermes Puggington" },
12
+ crypto: Crypto,
21
13
  });
22
14
 
23
15
  class TestMap extends CoMap {
@@ -41,7 +33,7 @@ describe("Simple CoMap operations", async () => {
41
33
  _height: 10,
42
34
  birthday: birthday,
43
35
  },
44
- { owner: me }
36
+ { owner: me },
45
37
  );
46
38
 
47
39
  test("Construction", () => {
@@ -82,7 +74,7 @@ describe("Simple CoMap operations", async () => {
82
74
 
83
75
  class RecursiveMap extends CoMap {
84
76
  name = co.string;
85
- next: co<RecursiveMap | null> = co.ref(RecursiveMap);
77
+ next?: co<RecursiveMap | null> = co.ref(RecursiveMap);
86
78
  }
87
79
 
88
80
  const recursiveMap = RecursiveMap.create(
@@ -95,13 +87,13 @@ describe("Simple CoMap operations", async () => {
95
87
  {
96
88
  name: "third",
97
89
  },
98
- { owner: me }
90
+ { owner: me },
99
91
  ),
100
92
  },
101
- { owner: me }
93
+ { owner: me },
102
94
  ),
103
95
  },
104
- { owner: me }
96
+ { owner: me },
105
97
  );
106
98
 
107
99
  describe("Recursive CoMap", () => {
@@ -115,7 +107,7 @@ describe("Simple CoMap operations", async () => {
115
107
  class MapWithEnumOfMaps extends CoMap {
116
108
  name = co.string;
117
109
  child = co.ref<typeof ChildA | typeof ChildB>((raw) =>
118
- raw.get("type") === "a" ? ChildA : ChildB
110
+ raw.get("type") === "a" ? ChildA : ChildB,
119
111
  );
120
112
  }
121
113
 
@@ -137,10 +129,10 @@ describe("Simple CoMap operations", async () => {
137
129
  type: "a",
138
130
  value: 5,
139
131
  },
140
- { owner: me }
132
+ { owner: me },
141
133
  ),
142
134
  },
143
- { owner: me }
135
+ { owner: me },
144
136
  );
145
137
 
146
138
  test("Enum of maps", () => {
@@ -154,19 +146,18 @@ describe("Simple CoMap operations", async () => {
154
146
  name = co.string;
155
147
  }
156
148
 
149
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
157
150
  class SubClassMap extends SuperClassMap {
158
151
  name = co.literal("specificString");
159
152
  value = co.number;
160
153
  extra = co.ref(TestMap);
161
154
  }
162
- interface SubClassMap extends CoMap {}
163
155
 
164
- class GenericMapWithLoose<
165
- out T extends string = string,
166
- > extends CoMap {
156
+ class GenericMapWithLoose<out T extends string = string> extends CoMap {
167
157
  name = co.json<T>();
168
158
  }
169
159
 
160
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
170
161
  const loose: GenericMapWithLoose<string> = {} as GenericMapWithLoose<
171
162
  "a" | "b"
172
163
  >;
@@ -199,6 +190,7 @@ describe("CoMap resolution", async () => {
199
190
  const initNodeAndMap = async () => {
200
191
  const me = await Account.create({
201
192
  creationProps: { name: "Hermes Puggington" },
193
+ crypto: Crypto,
202
194
  });
203
195
 
204
196
  const map = TestMap.create(
@@ -210,13 +202,13 @@ describe("CoMap resolution", async () => {
210
202
  name: "nested",
211
203
  twiceNested: TwiceNestedMap.create(
212
204
  { taste: "sour" },
213
- { owner: me }
205
+ { owner: me },
214
206
  ),
215
207
  },
216
- { owner: me }
208
+ { owner: me },
217
209
  ),
218
210
  },
219
- { owner: me }
211
+ { owner: me },
220
212
  );
221
213
 
222
214
  return { me, map };
@@ -241,17 +233,20 @@ describe("CoMap resolution", async () => {
241
233
  const [initialAsPeer, secondPeer] = connectedPeers(
242
234
  "initial",
243
235
  "second",
244
- { peer1role: "server", peer2role: "client" }
236
+ { peer1role: "server", peer2role: "client" },
245
237
  );
238
+ if (!isControlledAccount(me)) { throw("me is not a controlled account") }
246
239
  me._raw.core.node.syncManager.addPeer(secondPeer);
247
240
  const meOnSecondPeer = await Account.become({
248
241
  accountID: me.id,
249
242
  accountSecret: me._raw.agentSecret,
250
243
  peersToLoadFrom: [initialAsPeer],
244
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
251
245
  sessionID: newRandomSessionID(me.id as any),
246
+ crypto: Crypto,
252
247
  });
253
248
 
254
- const loadedMap = await TestMap.load(map.id, { as: meOnSecondPeer });
249
+ const loadedMap = await TestMap.load(map.id, meOnSecondPeer, {});
255
250
 
256
251
  expect(loadedMap?.color).toEqual("red");
257
252
  expect(loadedMap?.height).toEqual(10);
@@ -259,9 +254,11 @@ describe("CoMap resolution", async () => {
259
254
  expect(loadedMap?._refs.nested?.id).toEqual(map.nested?.id);
260
255
  expect(loadedMap?._refs.nested?.value).toEqual(null);
261
256
 
262
- const loadedNestedMap = await NestedMap.load(map.nested!.id, {
263
- as: meOnSecondPeer,
264
- });
257
+ const loadedNestedMap = await NestedMap.load(
258
+ map.nested!.id,
259
+ meOnSecondPeer,
260
+ {},
261
+ );
265
262
 
266
263
  expect(loadedMap?.nested?.name).toEqual("nested");
267
264
  expect(loadedMap?.nested?._fancyName).toEqual("Sir nested");
@@ -270,12 +267,13 @@ describe("CoMap resolution", async () => {
270
267
 
271
268
  const loadedTwiceNestedMap = await TwiceNestedMap.load(
272
269
  map.nested!.twiceNested!.id,
273
- { as: meOnSecondPeer }
270
+ meOnSecondPeer,
271
+ {},
274
272
  );
275
273
 
276
274
  expect(loadedMap?.nested?.twiceNested?.taste).toEqual("sour");
277
275
  expect(loadedMap?.nested?._refs.twiceNested?.value).toEqual(
278
- loadedTwiceNestedMap
276
+ loadedTwiceNestedMap,
279
277
  );
280
278
 
281
279
  const otherNestedMap = NestedMap.create(
@@ -283,10 +281,10 @@ describe("CoMap resolution", async () => {
283
281
  name: "otherNested",
284
282
  twiceNested: TwiceNestedMap.create(
285
283
  { taste: "sweet" },
286
- { owner: meOnSecondPeer }
284
+ { owner: meOnSecondPeer },
287
285
  ),
288
286
  },
289
- { owner: meOnSecondPeer }
287
+ { owner: meOnSecondPeer },
290
288
  );
291
289
 
292
290
  loadedMap!.nested = otherNestedMap;
@@ -303,16 +301,17 @@ describe("CoMap resolution", async () => {
303
301
  const [initialAsPeer, secondAsPeer] = connectedPeers(
304
302
  "initial",
305
303
  "second",
306
- { peer1role: "server", peer2role: "client" }
304
+ { peer1role: "server", peer2role: "client" },
307
305
  );
308
-
306
+ if (!isControlledAccount(me)) { throw("me is not a controlled account") }
309
307
  me._raw.core.node.syncManager.addPeer(secondAsPeer);
310
-
311
308
  const meOnSecondPeer = await Account.become({
312
309
  accountID: me.id,
313
310
  accountSecret: me._raw.agentSecret,
314
311
  peersToLoadFrom: [initialAsPeer],
312
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
315
313
  sessionID: newRandomSessionID(me.id as any),
314
+ crypto: Crypto,
316
315
  });
317
316
 
318
317
  await Effect.runPromise(
@@ -321,14 +320,17 @@ describe("CoMap resolution", async () => {
321
320
 
322
321
  TestMap.subscribe(
323
322
  map.id,
324
- { as: meOnSecondPeer },
323
+ meOnSecondPeer,
324
+ {},
325
325
  (subscribedMap) => {
326
326
  console.log(
327
327
  "subscribedMap.nested?.twiceNested?.taste",
328
- subscribedMap.nested?.twiceNested?.taste
328
+ subscribedMap.nested?.twiceNested?.taste,
329
329
  );
330
- Effect.runPromise(Queue.offer(queue, subscribedMap));
331
- }
330
+ void Effect.runPromise(
331
+ Queue.offer(queue, subscribedMap),
332
+ );
333
+ },
332
334
  );
333
335
 
334
336
  const update1 = yield* $(Queue.take(queue));
@@ -351,7 +353,7 @@ describe("CoMap resolution", async () => {
351
353
  {
352
354
  taste: "sweet",
353
355
  },
354
- { owner: meOnSecondPeer }
356
+ { owner: meOnSecondPeer },
355
357
  );
356
358
 
357
359
  const newNested = NestedMap.create(
@@ -359,7 +361,7 @@ describe("CoMap resolution", async () => {
359
361
  name: "newNested",
360
362
  twiceNested: newTwiceNested,
361
363
  },
362
- { owner: meOnSecondPeer }
364
+ { owner: meOnSecondPeer },
363
365
  );
364
366
 
365
367
  update3.nested = newNested;
@@ -379,7 +381,7 @@ describe("CoMap resolution", async () => {
379
381
  newTwiceNested.taste = "umami";
380
382
  const update6 = yield* $(Queue.take(queue));
381
383
  expect(update6.nested?.twiceNested?.taste).toEqual("umami");
382
- })
384
+ }),
383
385
  );
384
386
  });
385
387
 
@@ -391,13 +393,14 @@ describe("CoMap resolution", async () => {
391
393
  test("Construction with optional", async () => {
392
394
  const me = await Account.create({
393
395
  creationProps: { name: "Hermes Puggington" },
396
+ crypto: Crypto,
394
397
  });
395
398
 
396
399
  const mapWithout = TestMapWithOptionalRef.create(
397
400
  {
398
401
  color: "red",
399
402
  },
400
- { owner: me }
403
+ { owner: me },
401
404
  );
402
405
 
403
406
  expect(mapWithout.color).toEqual("red");
@@ -411,13 +414,13 @@ describe("CoMap resolution", async () => {
411
414
  name: "wow!",
412
415
  twiceNested: TwiceNestedMap.create(
413
416
  { taste: "sour" },
414
- { owner: me }
417
+ { owner: me },
415
418
  ),
416
419
  },
417
- { owner: me }
420
+ { owner: me },
418
421
  ),
419
422
  },
420
- { owner: me }
423
+ { owner: me },
421
424
  );
422
425
 
423
426
  expect(mapWith.color).toEqual("red");
@@ -426,14 +429,17 @@ describe("CoMap resolution", async () => {
426
429
  expect(mapWith.nested?._raw).toBeDefined();
427
430
  });
428
431
 
432
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
429
433
  class TestRecord extends CoMap {
430
434
  [co.items] = co.number;
431
435
  }
436
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
432
437
  interface TestRecord extends Record<string, number> {}
433
438
 
434
439
  test("Construction with index signature", async () => {
435
440
  const me = await Account.create({
436
441
  creationProps: { name: "Hermes Puggington" },
442
+ crypto: Crypto,
437
443
  });
438
444
 
439
445
  const record = TestRecord.create(
@@ -441,7 +447,7 @@ describe("CoMap resolution", async () => {
441
447
  height: 5,
442
448
  other: 3,
443
449
  },
444
- { owner: me }
450
+ { owner: me },
445
451
  );
446
452
 
447
453
  expect(record.height).toEqual(5);
@@ -462,6 +468,7 @@ describe("CoMap resolution", async () => {
462
468
  test("Construction with index signature (shorthand)", async () => {
463
469
  const me = await Account.create({
464
470
  creationProps: { name: "Hermes Puggington" },
471
+ crypto: Crypto,
465
472
  });
466
473
 
467
474
  const record = TestRecord2.create(
@@ -469,7 +476,7 @@ describe("CoMap resolution", async () => {
469
476
  height: 5,
470
477
  other: 3,
471
478
  },
472
- { owner: me }
479
+ { owner: me },
473
480
  );
474
481
 
475
482
  expect(record.height).toEqual(5);
@@ -484,20 +491,21 @@ describe("CoMap resolution", async () => {
484
491
  test("Construction with index signature ref", async () => {
485
492
  const me = await Account.create({
486
493
  creationProps: { name: "Hermes Puggington" },
494
+ crypto: Crypto,
487
495
  });
488
496
 
489
497
  const record = TestRecordRef.create(
490
498
  {
491
499
  firstNested: TwiceNestedMap.create(
492
500
  { taste: "sour" },
493
- { owner: me }
501
+ { owner: me },
494
502
  ),
495
503
  secondNested: TwiceNestedMap.create(
496
504
  { taste: "sweet" },
497
- { owner: me }
505
+ { owner: me },
498
506
  ),
499
507
  },
500
- { owner: me }
508
+ { owner: me },
501
509
  );
502
510
 
503
511
  expect(record.firstNested?.taste).toEqual("sour");