jazz-tools 0.7.0-alpha.8 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. package/.eslintrc.cjs +3 -10
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +3 -19
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +140 -0
  6. package/CHANGELOG.md +304 -0
  7. package/README.md +10 -2
  8. package/dist/coValues/account.js +59 -41
  9. package/dist/coValues/account.js.map +1 -1
  10. package/dist/coValues/coList.js +49 -46
  11. package/dist/coValues/coList.js.map +1 -1
  12. package/dist/coValues/coMap.js +143 -44
  13. package/dist/coValues/coMap.js.map +1 -1
  14. package/dist/coValues/coStream.js +144 -35
  15. package/dist/coValues/coStream.js.map +1 -1
  16. package/dist/coValues/deepLoading.js +60 -0
  17. package/dist/coValues/deepLoading.js.map +1 -0
  18. package/dist/coValues/extensions/imageDef.js +10 -7
  19. package/dist/coValues/extensions/imageDef.js.map +1 -1
  20. package/dist/coValues/group.js +49 -13
  21. package/dist/coValues/group.js.map +1 -1
  22. package/dist/coValues/interfaces.js +70 -31
  23. package/dist/coValues/interfaces.js.map +1 -1
  24. package/dist/implementation/devtoolsFormatters.js +114 -0
  25. package/dist/implementation/devtoolsFormatters.js.map +1 -0
  26. package/dist/implementation/refs.js +58 -18
  27. package/dist/implementation/refs.js.map +1 -1
  28. package/dist/implementation/schema.js +58 -0
  29. package/dist/implementation/schema.js.map +1 -0
  30. package/dist/implementation/subscriptionScope.js +19 -1
  31. package/dist/implementation/subscriptionScope.js.map +1 -1
  32. package/dist/implementation/symbols.js +5 -0
  33. package/dist/implementation/symbols.js.map +1 -0
  34. package/dist/index.js +3 -5
  35. package/dist/index.js.map +1 -1
  36. package/dist/internal.js +5 -2
  37. package/dist/internal.js.map +1 -1
  38. package/dist/tests/coList.test.js +51 -48
  39. package/dist/tests/coList.test.js.map +1 -1
  40. package/dist/tests/coMap.test.js +131 -73
  41. package/dist/tests/coMap.test.js.map +1 -1
  42. package/dist/tests/coStream.test.js +56 -41
  43. package/dist/tests/coStream.test.js.map +1 -1
  44. package/dist/tests/deepLoading.test.js +188 -0
  45. package/dist/tests/deepLoading.test.js.map +1 -0
  46. package/dist/tests/groupsAndAccounts.test.js +83 -0
  47. package/dist/tests/groupsAndAccounts.test.js.map +1 -0
  48. package/package.json +17 -9
  49. package/src/coValues/account.ts +113 -125
  50. package/src/coValues/coList.ts +87 -103
  51. package/src/coValues/coMap.ts +200 -147
  52. package/src/coValues/coStream.ts +264 -80
  53. package/src/coValues/deepLoading.ts +229 -0
  54. package/src/coValues/extensions/imageDef.ts +17 -13
  55. package/src/coValues/group.ts +92 -58
  56. package/src/coValues/interfaces.ts +215 -115
  57. package/src/implementation/devtoolsFormatters.ts +110 -0
  58. package/src/implementation/inspect.ts +1 -1
  59. package/src/implementation/refs.ts +80 -28
  60. package/src/implementation/schema.ts +138 -0
  61. package/src/implementation/subscriptionScope.ts +48 -12
  62. package/src/implementation/symbols.ts +11 -0
  63. package/src/index.ts +12 -8
  64. package/src/internal.ts +7 -3
  65. package/src/tests/coList.test.ts +77 -62
  66. package/src/tests/coMap.test.ts +201 -113
  67. package/src/tests/coStream.test.ts +113 -84
  68. package/src/tests/deepLoading.test.ts +301 -0
  69. package/src/tests/groupsAndAccounts.test.ts +91 -0
  70. package/dist/implementation/encoding.js +0 -26
  71. package/dist/implementation/encoding.js.map +0 -1
  72. package/src/implementation/encoding.ts +0 -105
@@ -1,28 +1,26 @@
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, CoList, val, jazzReady } 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 {
6
+ Account,
7
+ CoList,
8
+ WasmCrypto,
9
+ co,
10
+ isControlledAccount,
11
+ } from "../index.js";
13
12
 
14
- beforeEach(async () => {
15
- await jazzReady;
16
- });
13
+ const Crypto = await WasmCrypto.create();
17
14
 
18
15
  describe("Simple CoList operations", async () => {
19
16
  const me = await Account.create({
20
- name: "Hermes Puggington",
17
+ creationProps: { name: "Hermes Puggington" },
18
+ crypto: Crypto,
21
19
  });
22
20
 
23
- class TestList extends CoList.Of(val.string) {}
21
+ class TestList extends CoList.Of(co.string) {}
24
22
 
25
- const list = new TestList(["bread", "butter", "onion"], { owner: me });
23
+ const list = TestList.create(["bread", "butter", "onion"], { owner: me });
26
24
 
27
25
  test("Construction", () => {
28
26
  expect(list[0]).toBe("bread");
@@ -39,7 +37,7 @@ describe("Simple CoList operations", async () => {
39
37
 
40
38
  describe("Mutation", () => {
41
39
  test("assignment", () => {
42
- const list = new TestList(["bread", "butter", "onion"], {
40
+ const list = TestList.create(["bread", "butter", "onion"], {
43
41
  owner: me,
44
42
  });
45
43
  list[1] = "margarine";
@@ -52,7 +50,7 @@ describe("Simple CoList operations", async () => {
52
50
  });
53
51
 
54
52
  test("push", () => {
55
- const list = new TestList(["bread", "butter", "onion"], {
53
+ const list = TestList.create(["bread", "butter", "onion"], {
56
54
  owner: me,
57
55
  });
58
56
  list.push("cheese");
@@ -66,7 +64,7 @@ describe("Simple CoList operations", async () => {
66
64
  });
67
65
 
68
66
  test("unshift", () => {
69
- const list = new TestList(["bread", "butter", "onion"], {
67
+ const list = TestList.create(["bread", "butter", "onion"], {
70
68
  owner: me,
71
69
  });
72
70
  list.unshift("lettuce");
@@ -80,7 +78,7 @@ describe("Simple CoList operations", async () => {
80
78
  });
81
79
 
82
80
  test("pop", () => {
83
- const list = new TestList(["bread", "butter", "onion"], {
81
+ const list = TestList.create(["bread", "butter", "onion"], {
84
82
  owner: me,
85
83
  });
86
84
  expect(list.pop()).toBe("onion");
@@ -89,7 +87,7 @@ describe("Simple CoList operations", async () => {
89
87
  });
90
88
 
91
89
  test("shift", () => {
92
- const list = new TestList(["bread", "butter", "onion"], {
90
+ const list = TestList.create(["bread", "butter", "onion"], {
93
91
  owner: me,
94
92
  });
95
93
  expect(list.shift()).toBe("bread");
@@ -97,50 +95,51 @@ describe("Simple CoList operations", async () => {
97
95
  expect(list._raw.asArray()).toEqual(["butter", "onion"]);
98
96
  });
99
97
 
100
- // test("splice", () => {
101
- // const list = new TestList(["bread", "butter", "onion"], {
102
- // owner: me,
103
- // });
104
- // list.splice(1, 1, "salt", "pepper");
105
- // expect(list.length).toBe(4);
106
- // expect(list._raw.asArray()).toEqual([
107
- // "bread",
108
- // "salt",
109
- // "pepper",
110
- // "onion",
111
- // ]);
112
- // });
98
+ test("splice", () => {
99
+ const list = TestList.create(["bread", "butter", "onion"], {
100
+ owner: me,
101
+ });
102
+ list.splice(1, 1, "salt", "pepper");
103
+ expect(list.length).toBe(4);
104
+ expect(list._raw.asArray()).toEqual([
105
+ "bread",
106
+ "salt",
107
+ "pepper",
108
+ "onion",
109
+ ]);
110
+ });
113
111
  });
114
112
  });
115
113
 
116
114
  describe("CoList resolution", async () => {
117
- class TwiceNestedList extends CoList.Of(val.string) {
115
+ class TwiceNestedList extends CoList.Of(co.string) {
118
116
  joined() {
119
117
  return this.join(",");
120
118
  }
121
119
  }
122
120
 
123
- class NestedList extends CoList.Of(val.ref(() => TwiceNestedList)) {}
121
+ class NestedList extends CoList.Of(co.ref(TwiceNestedList)) {}
124
122
 
125
- class TestList extends CoList.Of(val.ref(() => NestedList)) {}
123
+ class TestList extends CoList.Of(co.ref(NestedList)) {}
126
124
 
127
125
  const initNodeAndList = async () => {
128
126
  const me = await Account.create({
129
- name: "Hermes Puggington",
127
+ creationProps: { name: "Hermes Puggington" },
128
+ crypto: Crypto,
130
129
  });
131
130
 
132
- const list = new TestList(
131
+ const list = TestList.create(
133
132
  [
134
- new NestedList(
135
- [new TwiceNestedList(["a", "b"], { owner: me })],
136
- { owner: me }
133
+ NestedList.create(
134
+ [TwiceNestedList.create(["a", "b"], { owner: me })],
135
+ { owner: me },
137
136
  ),
138
- new NestedList(
139
- [new TwiceNestedList(["c", "d"], { owner: me })],
140
- { owner: me }
137
+ NestedList.create(
138
+ [TwiceNestedList.create(["c", "d"], { owner: me })],
139
+ { owner: me },
141
140
  ),
142
141
  ],
143
- { owner: me }
142
+ { owner: me },
144
143
  );
145
144
 
146
145
  return { me, list };
@@ -161,24 +160,31 @@ describe("CoList resolution", async () => {
161
160
  const [initialAsPeer, secondPeer] = connectedPeers(
162
161
  "initial",
163
162
  "second",
164
- { peer1role: "server", peer2role: "client" }
163
+ { peer1role: "server", peer2role: "client" },
165
164
  );
165
+ if (!isControlledAccount(me)) {
166
+ throw "me is not a controlled account";
167
+ }
166
168
  me._raw.core.node.syncManager.addPeer(secondPeer);
167
169
  const meOnSecondPeer = await Account.become({
168
170
  accountID: me.id,
169
171
  accountSecret: me._raw.agentSecret,
170
172
  peersToLoadFrom: [initialAsPeer],
173
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
171
174
  sessionID: newRandomSessionID(me.id as any),
175
+ crypto: Crypto,
172
176
  });
173
177
 
174
- const loadedList = await TestList.load(list.id, { as: meOnSecondPeer });
178
+ const loadedList = await TestList.load(list.id, meOnSecondPeer, []);
175
179
 
176
180
  expect(loadedList?.[0]).toBe(null);
177
181
  expect(loadedList?._refs[0]?.id).toEqual(list[0]!.id);
178
182
 
179
- const loadedNestedList = await NestedList.load(list[0]!.id, {
180
- as: meOnSecondPeer,
181
- });
183
+ const loadedNestedList = await NestedList.load(
184
+ list[0]!.id,
185
+ meOnSecondPeer,
186
+ [],
187
+ );
182
188
 
183
189
  expect(loadedList?.[0]).toBeDefined();
184
190
  expect(loadedList?.[0]?.[0]).toBe(null);
@@ -187,7 +193,8 @@ describe("CoList resolution", async () => {
187
193
 
188
194
  const loadedTwiceNestedList = await TwiceNestedList.load(
189
195
  list[0]![0]!.id,
190
- { as: meOnSecondPeer }
196
+ meOnSecondPeer,
197
+ [],
191
198
  );
192
199
 
193
200
  expect(loadedList?.[0]?.[0]).toBeDefined();
@@ -196,9 +203,9 @@ describe("CoList resolution", async () => {
196
203
  expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
197
204
  expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
198
205
 
199
- const otherNestedList = new NestedList(
200
- [new TwiceNestedList(["e", "f"], { owner: meOnSecondPeer })],
201
- { owner: meOnSecondPeer }
206
+ const otherNestedList = NestedList.create(
207
+ [TwiceNestedList.create(["e", "f"], { owner: meOnSecondPeer })],
208
+ { owner: meOnSecondPeer },
202
209
  );
203
210
 
204
211
  loadedList![0] = otherNestedList;
@@ -212,14 +219,19 @@ describe("CoList resolution", async () => {
212
219
  const [initialAsPeer, secondPeer] = connectedPeers(
213
220
  "initial",
214
221
  "second",
215
- { peer1role: "server", peer2role: "client" }
222
+ { peer1role: "server", peer2role: "client" },
216
223
  );
224
+ if (!isControlledAccount(me)) {
225
+ throw "me is not a controlled account";
226
+ }
217
227
  me._raw.core.node.syncManager.addPeer(secondPeer);
218
228
  const meOnSecondPeer = await Account.become({
219
229
  accountID: me.id,
220
230
  accountSecret: me._raw.agentSecret,
221
231
  peersToLoadFrom: [initialAsPeer],
232
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
222
233
  sessionID: newRandomSessionID(me.id as any),
234
+ crypto: Crypto,
223
235
  });
224
236
 
225
237
  await Effect.runPromise(
@@ -228,14 +240,17 @@ describe("CoList resolution", async () => {
228
240
 
229
241
  TestList.subscribe(
230
242
  list.id,
231
- { as: meOnSecondPeer },
243
+ meOnSecondPeer,
244
+ [],
232
245
  (subscribedList) => {
233
246
  console.log(
234
247
  "subscribedList?.[0]?.[0]?.[0]",
235
- subscribedList?.[0]?.[0]?.[0]
248
+ subscribedList?.[0]?.[0]?.[0],
249
+ );
250
+ void Effect.runPromise(
251
+ Queue.offer(queue, subscribedList),
236
252
  );
237
- Effect.runPromise(Queue.offer(queue, subscribedList));
238
- }
253
+ },
239
254
  );
240
255
 
241
256
  const update1 = yield* $(Queue.take(queue));
@@ -257,11 +272,11 @@ describe("CoList resolution", async () => {
257
272
 
258
273
  // When assigning a new nested value, we get an update
259
274
 
260
- const newTwiceNestedList = new TwiceNestedList(["y", "z"], {
275
+ const newTwiceNestedList = TwiceNestedList.create(["y", "z"], {
261
276
  owner: meOnSecondPeer,
262
277
  });
263
278
 
264
- const newNestedList = new NestedList([newTwiceNestedList], {
279
+ const newNestedList = NestedList.create([newTwiceNestedList], {
265
280
  owner: meOnSecondPeer,
266
281
  });
267
282
 
@@ -275,7 +290,7 @@ describe("CoList resolution", async () => {
275
290
  newTwiceNestedList[0] = "w";
276
291
  const update6 = yield* $(Queue.take(queue));
277
292
  expect(update6?.[0]?.[0]?.[0]).toBe("w");
278
- })
293
+ }),
279
294
  );
280
295
  });
281
296
  });