jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. package/.eslintrc.cjs +8 -1
  2. package/.turbo/turbo-build.log +79 -9
  3. package/CHANGELOG.md +76 -0
  4. package/dist/coValues/account.js +171 -0
  5. package/dist/coValues/account.js.map +1 -0
  6. package/dist/coValues/coList.js +246 -0
  7. package/dist/coValues/coList.js.map +1 -0
  8. package/dist/coValues/coMap.js +235 -0
  9. package/dist/coValues/coMap.js.map +1 -0
  10. package/dist/coValues/coStream.js +263 -0
  11. package/dist/coValues/coStream.js.map +1 -0
  12. package/dist/coValues/extensions/imageDef.js +14 -11
  13. package/dist/coValues/extensions/imageDef.js.map +1 -1
  14. package/dist/coValues/group.js +71 -0
  15. package/dist/coValues/group.js.map +1 -0
  16. package/dist/coValues/interfaces.js +61 -0
  17. package/dist/coValues/interfaces.js.map +1 -0
  18. package/dist/implementation/errors.js.map +1 -0
  19. package/dist/implementation/inspect.js +2 -0
  20. package/dist/implementation/inspect.js.map +1 -0
  21. package/dist/{refs.js → implementation/refs.js} +16 -15
  22. package/dist/implementation/refs.js.map +1 -0
  23. package/dist/implementation/schema.js +41 -0
  24. package/dist/implementation/schema.js.map +1 -0
  25. package/dist/{subscriptionScope.js → implementation/subscriptionScope.js} +1 -1
  26. package/dist/implementation/subscriptionScope.js.map +1 -0
  27. package/dist/index.js +10 -29
  28. package/dist/index.js.map +1 -1
  29. package/dist/internal.js +13 -0
  30. package/dist/internal.js.map +1 -0
  31. package/dist/tests/coList.test.js +22 -22
  32. package/dist/tests/coList.test.js.map +1 -1
  33. package/dist/tests/coMap.test.js +140 -46
  34. package/dist/tests/coMap.test.js.map +1 -1
  35. package/dist/tests/coStream.test.js +55 -49
  36. package/dist/tests/coStream.test.js.map +1 -1
  37. package/package.json +11 -4
  38. package/src/coValues/account.ts +327 -0
  39. package/src/coValues/coList.ts +391 -0
  40. package/src/coValues/coMap.ts +413 -0
  41. package/src/coValues/coStream.ts +404 -0
  42. package/src/coValues/extensions/imageDef.ts +20 -16
  43. package/src/coValues/group.ts +139 -0
  44. package/src/coValues/interfaces.ts +218 -0
  45. package/src/implementation/inspect.ts +2 -0
  46. package/src/{refs.ts → implementation/refs.ts} +53 -39
  47. package/src/implementation/schema.ts +124 -0
  48. package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
  49. package/src/index.ts +14 -57
  50. package/src/internal.ts +15 -0
  51. package/src/tests/coList.test.ts +23 -25
  52. package/src/tests/coMap.test.ts +140 -53
  53. package/src/tests/coStream.test.ts +76 -68
  54. package/dist/coValueInterfaces.js +0 -8
  55. package/dist/coValueInterfaces.js.map +0 -1
  56. package/dist/coValues/account/account.js +0 -11
  57. package/dist/coValues/account/account.js.map +0 -1
  58. package/dist/coValues/account/accountOf.js +0 -150
  59. package/dist/coValues/account/accountOf.js.map +0 -1
  60. package/dist/coValues/account/migration.js +0 -4
  61. package/dist/coValues/account/migration.js.map +0 -1
  62. package/dist/coValues/coList/coList.js +0 -2
  63. package/dist/coValues/coList/coList.js.map +0 -1
  64. package/dist/coValues/coList/coListOf.js +0 -235
  65. package/dist/coValues/coList/coListOf.js.map +0 -1
  66. package/dist/coValues/coList/internalDocs.js +0 -2
  67. package/dist/coValues/coList/internalDocs.js.map +0 -1
  68. package/dist/coValues/coMap/coMap.js +0 -2
  69. package/dist/coValues/coMap/coMap.js.map +0 -1
  70. package/dist/coValues/coMap/coMapOf.js +0 -262
  71. package/dist/coValues/coMap/coMapOf.js.map +0 -1
  72. package/dist/coValues/coMap/internalDocs.js +0 -2
  73. package/dist/coValues/coMap/internalDocs.js.map +0 -1
  74. package/dist/coValues/coStream/coStream.js +0 -2
  75. package/dist/coValues/coStream/coStream.js.map +0 -1
  76. package/dist/coValues/coStream/coStreamOf.js +0 -244
  77. package/dist/coValues/coStream/coStreamOf.js.map +0 -1
  78. package/dist/coValues/construction.js +0 -34
  79. package/dist/coValues/construction.js.map +0 -1
  80. package/dist/coValues/group/group.js +0 -2
  81. package/dist/coValues/group/group.js.map +0 -1
  82. package/dist/coValues/group/groupOf.js +0 -109
  83. package/dist/coValues/group/groupOf.js.map +0 -1
  84. package/dist/coValues/resolution.js +0 -66
  85. package/dist/coValues/resolution.js.map +0 -1
  86. package/dist/errors.js.map +0 -1
  87. package/dist/refs.js.map +0 -1
  88. package/dist/schemaHelpers.js +0 -14
  89. package/dist/schemaHelpers.js.map +0 -1
  90. package/dist/subscriptionScope.js.map +0 -1
  91. package/dist/tests/types.test.js +0 -33
  92. package/dist/tests/types.test.js.map +0 -1
  93. package/src/coValueInterfaces.ts +0 -105
  94. package/src/coValues/account/account.ts +0 -106
  95. package/src/coValues/account/accountOf.ts +0 -284
  96. package/src/coValues/account/migration.ts +0 -12
  97. package/src/coValues/coList/coList.ts +0 -57
  98. package/src/coValues/coList/coListOf.ts +0 -377
  99. package/src/coValues/coList/internalDocs.ts +0 -1
  100. package/src/coValues/coMap/coMap.ts +0 -110
  101. package/src/coValues/coMap/coMapOf.ts +0 -451
  102. package/src/coValues/coMap/internalDocs.ts +0 -1
  103. package/src/coValues/coStream/coStream.ts +0 -63
  104. package/src/coValues/coStream/coStreamOf.ts +0 -404
  105. package/src/coValues/construction.ts +0 -110
  106. package/src/coValues/group/group.ts +0 -27
  107. package/src/coValues/group/groupOf.ts +0 -183
  108. package/src/coValues/resolution.ts +0 -111
  109. package/src/schemaHelpers.ts +0 -72
  110. package/src/tests/types.test.ts +0 -37
  111. /package/dist/{errors.js → implementation/errors.js} +0 -0
  112. /package/src/{errors.ts → implementation/errors.ts} +0 -0
@@ -4,7 +4,7 @@ import { webcrypto } from "node:crypto";
4
4
  import { connectedPeers } from "cojson/src/streamUtils.js";
5
5
  import { newRandomSessionID } from "cojson/src/coValueCore.js";
6
6
  import { Effect, Queue } from "effect";
7
- import { Co, S, Account, jazzReady } from "..";
7
+ import { Account, CoList, co, jazzReady } from "..";
8
8
 
9
9
  if (!("crypto" in globalThis)) {
10
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -20,7 +20,7 @@ describe("Simple CoList operations", async () => {
20
20
  name: "Hermes Puggington",
21
21
  });
22
22
 
23
- class TestList extends Co.list(S.string).as<TestList>() {}
23
+ class TestList extends CoList.Of(co.string) {}
24
24
 
25
25
  const list = new TestList(["bread", "butter", "onion"], { owner: me });
26
26
 
@@ -97,32 +97,32 @@ describe("Simple CoList operations", async () => {
97
97
  expect(list._raw.asArray()).toEqual(["butter", "onion"]);
98
98
  });
99
99
 
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
- });
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
+ // });
113
113
  });
114
114
  });
115
115
 
116
116
  describe("CoList resolution", async () => {
117
- class TwiceNestedList extends Co.list(S.string).as<TwiceNestedList>() {
117
+ class TwiceNestedList extends CoList.Of(co.string) {
118
118
  joined() {
119
119
  return this.join(",");
120
120
  }
121
121
  }
122
122
 
123
- class NestedList extends Co.list(TwiceNestedList).as<NestedList>() {}
123
+ class NestedList extends CoList.Of(co.ref(TwiceNestedList)) {}
124
124
 
125
- class TestList extends Co.list(NestedList).as<TestList>() {}
125
+ class TestList extends CoList.Of(co.ref(NestedList)) {}
126
126
 
127
127
  const initNodeAndList = async () => {
128
128
  const me = await Account.create({
@@ -173,7 +173,7 @@ describe("CoList resolution", async () => {
173
173
 
174
174
  const loadedList = await TestList.load(list.id, { as: meOnSecondPeer });
175
175
 
176
- expect(loadedList?.[0]).toBe(undefined);
176
+ expect(loadedList?.[0]).toBe(null);
177
177
  expect(loadedList?._refs[0]?.id).toEqual(list[0]!.id);
178
178
 
179
179
  const loadedNestedList = await NestedList.load(list[0]!.id, {
@@ -181,7 +181,7 @@ describe("CoList resolution", async () => {
181
181
  });
182
182
 
183
183
  expect(loadedList?.[0]).toBeDefined();
184
- expect(loadedList?.[0]?.[0]).toBeUndefined();
184
+ expect(loadedList?.[0]?.[0]).toBe(null);
185
185
  expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]![0]!.id);
186
186
  expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
187
187
 
@@ -194,9 +194,7 @@ describe("CoList resolution", async () => {
194
194
  expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
195
195
  expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
196
196
  expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
197
- expect(loadedList?.[0]?._refs[0]?.value).toEqual(
198
- loadedTwiceNestedList
199
- );
197
+ expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
200
198
 
201
199
  const otherNestedList = new NestedList(
202
200
  [new TwiceNestedList(["e", "f"], { owner: meOnSecondPeer })],
@@ -241,11 +239,11 @@ describe("CoList resolution", async () => {
241
239
  );
242
240
 
243
241
  const update1 = yield* $(Queue.take(queue));
244
- expect(update1?.[0]).toEqual(undefined);
242
+ expect(update1?.[0]).toBe(null);
245
243
 
246
244
  const update2 = yield* $(Queue.take(queue));
247
245
  expect(update2?.[0]).toBeDefined();
248
- expect(update2?.[0]?.[0]).toBeUndefined();
246
+ expect(update2?.[0]?.[0]).toBe(null);
249
247
 
250
248
  const update3 = yield* $(Queue.take(queue));
251
249
  expect(update3?.[0]?.[0]).toBeDefined();
@@ -4,7 +4,7 @@ import { webcrypto } from "node:crypto";
4
4
  import { connectedPeers } from "cojson/src/streamUtils.js";
5
5
  import { newRandomSessionID } from "cojson/src/coValueCore.js";
6
6
  import { Effect, Queue } from "effect";
7
- import { Co, S, Account, jazzReady } from "..";
7
+ import { Account, jazzReady, Encoders, CoMap, co } from "..";
8
8
 
9
9
  if (!("crypto" in globalThis)) {
10
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -20,23 +20,25 @@ describe("Simple CoMap operations", async () => {
20
20
  name: "Hermes Puggington",
21
21
  });
22
22
 
23
- class TestMap extends Co.map({
24
- color: S.string,
25
- height: S.number,
26
- birthday: S.Date,
27
- name: S.optional(S.string),
28
- }).as<TestMap>() {
23
+ class TestMap extends CoMap<TestMap> {
24
+ color = co.string;
25
+ _height = co.number;
26
+ birthday = co.encoded(Encoders.Date);
27
+ name? = co.string;
28
+
29
29
  get roughColor() {
30
30
  return this.color + "ish";
31
31
  }
32
32
  }
33
33
 
34
+ console.log("TestMap schema", TestMap.prototype._schema);
35
+
34
36
  const birthday = new Date();
35
37
 
36
38
  const map = new TestMap(
37
39
  {
38
40
  color: "red",
39
- height: 10,
41
+ _height: 10,
40
42
  birthday: birthday,
41
43
  },
42
44
  { owner: me }
@@ -45,9 +47,15 @@ describe("Simple CoMap operations", async () => {
45
47
  test("Construction", () => {
46
48
  expect(map.color).toEqual("red");
47
49
  expect(map.roughColor).toEqual("redish");
48
- expect(map.height).toEqual(10);
50
+ expect(map._height).toEqual(10);
49
51
  expect(map.birthday).toEqual(birthday);
50
52
  expect(map._raw.get("birthday")).toEqual(birthday.toISOString());
53
+ expect(Object.keys(map)).toEqual([
54
+ "color",
55
+ "_height",
56
+ "birthday",
57
+ "name",
58
+ ]);
51
59
  });
52
60
 
53
61
  describe("Mutation", () => {
@@ -58,44 +66,123 @@ describe("Simple CoMap operations", async () => {
58
66
  const newBirthday = new Date();
59
67
  map.birthday = newBirthday;
60
68
  expect(map.birthday).toEqual(newBirthday);
61
- expect(map._raw.get("birthday")).toEqual(
62
- newBirthday.toISOString()
63
- );
69
+ expect(map._raw.get("birthday")).toEqual(newBirthday.toISOString());
64
70
 
65
- Object.assign(map, { color: "green", height: 20 });
71
+ Object.assign(map, { color: "green", _height: 20 });
66
72
  expect(map.color).toEqual("green");
67
73
  expect(map._raw.get("color")).toEqual("green");
68
- expect(map.height).toEqual(20);
69
- expect(map._raw.get("height")).toEqual(20);
74
+ expect(map._height).toEqual(20);
75
+ expect(map._raw.get("_height")).toEqual(20);
70
76
 
71
77
  map.name = "Secret name";
72
78
  expect(map.name).toEqual("Secret name");
73
- delete map.name;
79
+ map.name = undefined;
74
80
  expect(map.name).toEqual(undefined);
75
81
  });
76
82
  });
83
+
84
+ class RecursiveMap extends CoMap<RecursiveMap> {
85
+ name = co.string;
86
+ next: co<RecursiveMap | null> = co.ref(RecursiveMap);
87
+ }
88
+
89
+ const recursiveMap = new RecursiveMap(
90
+ {
91
+ name: "first",
92
+ next: new RecursiveMap(
93
+ {
94
+ name: "second",
95
+ next: new RecursiveMap(
96
+ {
97
+ name: "third",
98
+ },
99
+ { owner: me }
100
+ ),
101
+ },
102
+ { owner: me }
103
+ ),
104
+ },
105
+ { owner: me }
106
+ );
107
+
108
+ describe("Recursive CoMap", () => {
109
+ test("Construction", () => {
110
+ expect(recursiveMap.name).toEqual("first");
111
+ expect(recursiveMap.next?.name).toEqual("second");
112
+ expect(recursiveMap.next?.next?.name).toEqual("third");
113
+ });
114
+ });
115
+
116
+ class MapWithEnumOfMaps extends CoMap<MapWithEnumOfMaps> {
117
+ name = co.string;
118
+ child = co.ref<typeof ChildA | typeof ChildB>((raw) =>
119
+ raw.get("type") === "a" ? ChildA : ChildB
120
+ );
121
+ }
122
+
123
+ class ChildA extends CoMap<ChildA> {
124
+ type = co.literal("a");
125
+ value = co.number;
126
+ }
127
+
128
+ class ChildB extends CoMap<ChildB> {
129
+ type = co.literal("b");
130
+ value = co.string;
131
+ }
132
+
133
+ const mapWithEnum = new MapWithEnumOfMaps(
134
+ {
135
+ name: "enum",
136
+ child: new ChildA(
137
+ {
138
+ type: "a",
139
+ value: 5,
140
+ },
141
+ { owner: me }
142
+ ),
143
+ },
144
+ { owner: me }
145
+ );
146
+
147
+ test("Enum of maps", () => {
148
+ expect(mapWithEnum.name).toEqual("enum");
149
+ expect(mapWithEnum.child?.type).toEqual("a");
150
+ expect(mapWithEnum.child?.value).toEqual(5);
151
+ expect(mapWithEnum.child?.id).toBeDefined();
152
+ });
153
+
154
+ class SuperClassMap extends CoMap<SuperClassMap> {
155
+ name = co.string;
156
+ }
157
+
158
+ class SubClassMap extends SuperClassMap {
159
+ name = co.literal("specificString")
160
+ value = co.number;
161
+ extra = co.ref(TestMap)
162
+ }
163
+ interface SubClassMap extends CoMap<SubClassMap> {}
77
164
  });
78
165
 
79
166
  describe("CoMap resolution", async () => {
80
- class TwiceNestedMap extends Co.map({
81
- taste: S.string,
82
- }).as<TwiceNestedMap>() {}
83
-
84
- class NestedMap extends Co.map({
85
- name: S.string,
86
- twiceNested: TwiceNestedMap,
87
- }).as<NestedMap>() {
88
- get fancyName() {
167
+ class TwiceNestedMap extends CoMap<TwiceNestedMap> {
168
+ taste = co.string;
169
+ }
170
+
171
+ class NestedMap extends CoMap<NestedMap> {
172
+ name = co.string;
173
+ twiceNested = co.ref(TwiceNestedMap);
174
+
175
+ get _fancyName() {
89
176
  return "Sir " + this.name;
90
177
  }
91
178
  }
92
179
 
93
- class TestMap extends Co.map({
94
- color: S.string,
95
- height: S.number,
96
- nested: NestedMap,
97
- }).as<TestMap>() {
98
- get roughColor() {
180
+ class TestMap extends CoMap<TestMap> {
181
+ color = co.string;
182
+ height = co.number;
183
+ nested = co.ref(NestedMap);
184
+
185
+ get _roughColor() {
99
186
  return this.color + "ish";
100
187
  }
101
188
  }
@@ -132,10 +219,10 @@ describe("CoMap resolution", async () => {
132
219
  // const test: Schema.Schema.To<typeof NestedMap>
133
220
 
134
221
  expect(map.color).toEqual("red");
135
- expect(map.roughColor).toEqual("redish");
222
+ expect(map._roughColor).toEqual("redish");
136
223
  expect(map.height).toEqual(10);
137
224
  expect(map.nested?.name).toEqual("nested");
138
- expect(map.nested?.fancyName).toEqual("Sir nested");
225
+ expect(map.nested?._fancyName).toEqual("Sir nested");
139
226
  expect(map.nested?.id).toBeDefined();
140
227
  expect(map.nested?.twiceNested?.taste).toEqual("sour");
141
228
  });
@@ -159,16 +246,16 @@ describe("CoMap resolution", async () => {
159
246
 
160
247
  expect(loadedMap?.color).toEqual("red");
161
248
  expect(loadedMap?.height).toEqual(10);
162
- expect(loadedMap?.nested).toEqual(undefined);
249
+ expect(loadedMap?.nested).toEqual(null);
163
250
  expect(loadedMap?._refs.nested?.id).toEqual(map.nested?.id);
164
- expect(loadedMap?._refs.nested?.value).toEqual(undefined);
251
+ expect(loadedMap?._refs.nested?.value).toEqual(null);
165
252
 
166
253
  const loadedNestedMap = await NestedMap.load(map.nested!.id, {
167
254
  as: meOnSecondPeer,
168
255
  });
169
256
 
170
257
  expect(loadedMap?.nested?.name).toEqual("nested");
171
- expect(loadedMap?.nested.fancyName).toEqual("Sir nested");
258
+ expect(loadedMap?.nested?._fancyName).toEqual("Sir nested");
172
259
  expect(loadedMap?._refs.nested?.value).toEqual(loadedNestedMap);
173
260
  expect(loadedMap?.nested?.twiceNested?.taste).toEqual(undefined);
174
261
 
@@ -236,7 +323,7 @@ describe("CoMap resolution", async () => {
236
323
  );
237
324
 
238
325
  const update1 = yield* $(Queue.take(queue));
239
- expect(update1.nested).toEqual(undefined);
326
+ expect(update1.nested).toEqual(null);
240
327
 
241
328
  const update2 = yield* $(Queue.take(queue));
242
329
  expect(update2.nested?.name).toEqual("nested");
@@ -287,10 +374,10 @@ describe("CoMap resolution", async () => {
287
374
  );
288
375
  });
289
376
 
290
- class TestMapWithOptionalRef extends Co.map({
291
- color: S.string,
292
- nested: S.optional(NestedMap),
293
- }).as<TestMapWithOptionalRef>() {}
377
+ class TestMapWithOptionalRef extends CoMap<TestMapWithOptionalRef> {
378
+ color = co.string;
379
+ nested? = co.ref(NestedMap);
380
+ }
294
381
 
295
382
  test("Construction with optional", async () => {
296
383
  const me = await Account.create({
@@ -326,14 +413,14 @@ describe("CoMap resolution", async () => {
326
413
 
327
414
  expect(mapWith.color).toEqual("red");
328
415
  expect(mapWith.nested?.name).toEqual("wow!");
329
- expect(mapWith.nested?.fancyName).toEqual("Sir wow!");
416
+ expect(mapWith.nested?._fancyName).toEqual("Sir wow!");
330
417
  expect(mapWith.nested?._raw).toBeDefined();
331
418
  });
332
419
 
333
- class TestRecord extends Co.map(
334
- { color: S.string },
335
- { key: S.string, value: S.string }
336
- ).as<TestRecord>() {}
420
+ class TestRecord extends CoMap<TestRecord> {
421
+ [co.items] = co.number;
422
+ }
423
+ interface TestRecord extends Record<string, number> {}
337
424
 
338
425
  test("Construction with index signature", async () => {
339
426
  const me = await Account.create({
@@ -342,16 +429,16 @@ describe("CoMap resolution", async () => {
342
429
 
343
430
  const record = new TestRecord(
344
431
  {
345
- color: "red",
346
- other: "wild",
432
+ height: 5,
433
+ other: 3,
347
434
  },
348
435
  { owner: me }
349
436
  );
350
437
 
351
- expect(record.color).toEqual("red");
352
- expect(record._raw.get("color")).toEqual("red");
353
- expect(record.other).toEqual("wild");
354
- expect(record._raw.get("other")).toEqual("wild");
355
- expect(Object.keys(record)).toEqual(["color", "other"]);
438
+ expect(record.height).toEqual(5);
439
+ expect(record._raw.get("height")).toEqual(5);
440
+ expect(record.other).toEqual(3);
441
+ expect(record._raw.get("other")).toEqual(3);
442
+ expect(Object.keys(record)).toEqual(["height", "other"]);
356
443
  });
357
444
  });