jazz-tools 0.7.0-alpha.4 → 0.7.0-alpha.42

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