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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/coValues/account.js +62 -29
  3. package/dist/coValues/account.js.map +1 -1
  4. package/dist/coValues/coList.js +139 -89
  5. package/dist/coValues/coList.js.map +1 -1
  6. package/dist/coValues/coMap.js +135 -151
  7. package/dist/coValues/coMap.js.map +1 -1
  8. package/dist/coValues/coStream.js +131 -57
  9. package/dist/coValues/coStream.js.map +1 -1
  10. package/dist/coValues/extensions/imageDef.js +10 -7
  11. package/dist/coValues/extensions/imageDef.js.map +1 -1
  12. package/dist/coValues/group.js +8 -30
  13. package/dist/coValues/group.js.map +1 -1
  14. package/dist/coValues/interfaces.js +7 -3
  15. package/dist/coValues/interfaces.js.map +1 -1
  16. package/dist/implementation/encoding.js +26 -0
  17. package/dist/implementation/encoding.js.map +1 -0
  18. package/dist/implementation/refs.js +11 -10
  19. package/dist/implementation/refs.js.map +1 -1
  20. package/dist/index.js +4 -3
  21. package/dist/index.js.map +1 -1
  22. package/dist/internal.js +1 -1
  23. package/dist/internal.js.map +1 -1
  24. package/dist/tests/coList.test.js +5 -9
  25. package/dist/tests/coList.test.js.map +1 -1
  26. package/dist/tests/coMap.test.js +87 -37
  27. package/dist/tests/coMap.test.js.map +1 -1
  28. package/dist/tests/coStream.test.js +46 -51
  29. package/dist/tests/coStream.test.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/coValues/account.ts +90 -60
  32. package/src/coValues/coList.ts +177 -114
  33. package/src/coValues/coMap.ts +191 -240
  34. package/src/coValues/coStream.ts +175 -97
  35. package/src/coValues/extensions/imageDef.ts +7 -12
  36. package/src/coValues/group.ts +24 -71
  37. package/src/coValues/interfaces.ts +10 -9
  38. package/src/implementation/encoding.ts +105 -0
  39. package/src/implementation/refs.ts +21 -20
  40. package/src/index.ts +3 -3
  41. package/src/internal.ts +1 -1
  42. package/src/tests/coList.test.ts +5 -9
  43. package/src/tests/coMap.test.ts +68 -52
  44. package/src/tests/coStream.test.ts +61 -66
  45. package/dist/implementation/schema.js +0 -6
  46. package/dist/implementation/schema.js.map +0 -1
  47. package/src/implementation/schema.ts +0 -69
@@ -1,10 +1,10 @@
1
- import { expect, describe, test, beforeEach, Test } from "vitest";
1
+ import { expect, describe, test, beforeEach } from "vitest";
2
2
 
3
3
  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 { BinaryCoStream, ID, Account, jazzReady, CoStream } from "..";
7
+ import { BinaryCoStream, ID, Account, jazzReady, CoStream, val } from "..";
8
8
  import { Simplify } from "effect/Types";
9
9
 
10
10
  if (!("crypto" in globalThis)) {
@@ -21,42 +21,38 @@ describe("Simple CoStream operations", async () => {
21
21
  name: "Hermes Puggington",
22
22
  });
23
23
 
24
- class TestStream extends CoStream<string> {}
25
- TestStream.encoding({ _item: "json" });
24
+ class TestStream extends CoStream.Of(val.string) {}
26
25
 
27
26
  const stream = new TestStream(["milk"], { owner: me });
28
27
 
29
28
  test("Construction", () => {
30
- expect(stream.by[me.id]?.value).toEqual("milk");
31
- expect(stream.in[me.sessionID]?.value).toEqual("milk");
29
+ expect(stream[me.id]?.value).toEqual("milk");
30
+ expect(stream.perSession[me.sessionID]?.value).toEqual("milk");
32
31
  });
33
32
 
34
33
  describe("Mutation", () => {
35
34
  test("pushing", () => {
36
35
  stream.push("bread");
37
- expect(stream.by[me.id]?.value).toEqual("bread");
38
- expect(stream.in[me.sessionID]?.value).toEqual("bread");
36
+ expect(stream[me.id]?.value).toEqual("bread");
37
+ expect(stream.perSession[me.sessionID]?.value).toEqual("bread");
39
38
 
40
39
  stream.push("butter");
41
- expect(stream.by[me.id]?.value).toEqual("butter");
42
- expect(stream.in[me.sessionID]?.value).toEqual("butter");
40
+ expect(stream[me.id]?.value).toEqual("butter");
41
+ expect(stream.perSession[me.sessionID]?.value).toEqual("butter");
43
42
  });
44
43
  });
45
44
  });
46
45
 
47
46
  describe("CoStream resolution", async () => {
48
- class TwiceNestedStream extends CoStream<string> {
47
+ class TwiceNestedStream extends CoStream.Of(val.string) {
49
48
  fancyValueOf(account: ID<Account>) {
50
- return "Sir " + this.by[account]?.value;
49
+ return "Sir " + this[account]?.value;
51
50
  }
52
51
  }
53
- TwiceNestedStream.encoding({ _item: "json" });
54
52
 
55
- class NestedStream extends CoStream<TwiceNestedStream | null>{}
56
- NestedStream.encoding({ _item: {ref: () => TwiceNestedStream} });
53
+ class NestedStream extends CoStream.Of(val.ref(() => TwiceNestedStream)) {}
57
54
 
58
- class TestStream extends CoStream<NestedStream | null> {}
59
- TestStream.encoding({ _item: {ref: () => NestedStream} });
55
+ class TestStream extends CoStream.Of(val.ref(() => NestedStream)) {}
60
56
 
61
57
  const initNodeAndStream = async () => {
62
58
  const me = await Account.create({
@@ -78,9 +74,9 @@ describe("CoStream resolution", async () => {
78
74
 
79
75
  test("Construction", async () => {
80
76
  const { me, stream } = await initNodeAndStream();
81
- expect(
82
- stream.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value
83
- ).toEqual("milk");
77
+ expect(stream[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toEqual(
78
+ "milk"
79
+ );
84
80
  });
85
81
 
86
82
  test("Loading and availability", async () => {
@@ -102,48 +98,48 @@ describe("CoStream resolution", async () => {
102
98
  as: meOnSecondPeer,
103
99
  });
104
100
 
105
- expect(loadedStream?.by[me.id]?.value).toEqual(null);
106
- expect(loadedStream?.by[me.id]?.ref?.id).toEqual(
107
- stream.by[me.id]?.value?.id
101
+ expect(loadedStream?.[me.id]?.value).toEqual(null);
102
+ expect(loadedStream?.[me.id]?.ref?.id).toEqual(
103
+ stream[me.id]?.value?.id
108
104
  );
109
105
 
110
106
  const loadedNestedStream = await NestedStream.load(
111
- stream.by[me.id]!.value!.id,
107
+ stream[me.id]!.value!.id,
112
108
  { as: meOnSecondPeer }
113
109
  );
114
110
 
115
- // expect(loadedStream?.by[me.id]?.value).toEqual(loadedNestedStream);
116
- expect(loadedStream?.by[me.id]?.value?.id).toEqual(
111
+ // expect(loadedStream?.[me.id]?.value).toEqual(loadedNestedStream);
112
+ expect(loadedStream?.[me.id]?.value?.id).toEqual(
117
113
  loadedNestedStream?.id
118
114
  );
119
- expect(loadedStream?.by[me.id]?.value?.by[me.id]?.value).toEqual(null);
120
- // expect(loadedStream?.by[me.id]?.ref?.value).toEqual(loadedNestedStream);
121
- expect(loadedStream?.by[me.id]?.ref?.value?.id).toEqual(
115
+ expect(loadedStream?.[me.id]?.value?.[me.id]?.value).toEqual(null);
116
+ // expect(loadedStream?.[me.id]?.ref?.value).toEqual(loadedNestedStream);
117
+ expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
122
118
  loadedNestedStream?.id
123
119
  );
124
- expect(loadedStream?.by[me.id]?.value?.by[me.id]?.ref?.id).toEqual(
125
- stream.by[me.id]?.value?.by[me.id]?.value?.id
120
+ expect(loadedStream?.[me.id]?.value?.[me.id]?.ref?.id).toEqual(
121
+ stream[me.id]?.value?.[me.id]?.value?.id
126
122
  );
127
123
 
128
124
  const loadedTwiceNestedStream = await TwiceNestedStream.load(
129
- stream.by[me.id]!.value!.by[me.id]!.value!.id,
125
+ stream[me.id]!.value![me.id]!.value!.id,
130
126
  { as: meOnSecondPeer }
131
127
  );
132
128
 
133
- // expect(loadedStream?.by[me.id]?.value?.by[me.id]?.value).toEqual(
129
+ // expect(loadedStream?.[me.id]?.value?.[me.id]?.value).toEqual(
134
130
  // loadedTwiceNestedStream
135
131
  // );
136
- expect(loadedStream?.by[me.id]?.value?.by[me.id]?.value?.id).toEqual(
132
+ expect(loadedStream?.[me.id]?.value?.[me.id]?.value?.id).toEqual(
137
133
  loadedTwiceNestedStream?.id
138
134
  );
139
135
  expect(
140
- loadedStream?.by[me.id]?.value?.by[me.id]?.value?.fancyValueOf(
141
- me.id
142
- )
136
+ loadedStream?.[me.id]?.value?.[me.id]?.value?.fancyValueOf(me.id)
143
137
  ).toEqual("Sir milk");
144
- // expect(loadedStream?.by[me.id]?.ref?.value).toEqual(loadedNestedStream);
145
- expect(loadedStream?.by[me.id]?.ref?.value?.id).toEqual(loadedNestedStream?.id);
146
- expect(loadedStream?.by[me.id]?.value?.by[me.id]?.ref?.value?.id).toEqual(
138
+ // expect(loadedStream?.[me.id]?.ref?.value).toEqual(loadedNestedStream);
139
+ expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
140
+ loadedNestedStream?.id
141
+ );
142
+ expect(loadedStream?.[me.id]?.value?.[me.id]?.ref?.value?.id).toEqual(
147
143
  loadedTwiceNestedStream?.id
148
144
  );
149
145
 
@@ -152,16 +148,16 @@ describe("CoStream resolution", async () => {
152
148
  { owner: meOnSecondPeer }
153
149
  );
154
150
  loadedStream?.push(otherNestedStream);
155
- // expect(loadedStream?.by[me.id]?.value).toEqual(otherNestedStream);
156
- expect(loadedStream?.by[me.id]?.value?.id).toEqual(otherNestedStream?.id);
157
- expect(loadedStream?.by[me.id]?.ref?.value?.id).toEqual(otherNestedStream?.id);
158
- expect(loadedStream?.by[me.id]?.value?.by[me.id]?.value?.id).toEqual(
159
- otherNestedStream.by[me.id]?.value?.id
151
+ // expect(loadedStream?.[me.id]?.value).toEqual(otherNestedStream);
152
+ expect(loadedStream?.[me.id]?.value?.id).toEqual(otherNestedStream?.id);
153
+ expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
154
+ otherNestedStream?.id
155
+ );
156
+ expect(loadedStream?.[me.id]?.value?.[me.id]?.value?.id).toEqual(
157
+ otherNestedStream[me.id]?.value?.id
160
158
  );
161
159
  expect(
162
- loadedStream?.by[me.id]?.value?.by[me.id]?.value?.fancyValueOf(
163
- me.id
164
- )
160
+ loadedStream?.[me.id]?.value?.[me.id]?.value?.fancyValueOf(me.id)
165
161
  ).toEqual("Sir butter");
166
162
  });
167
163
 
@@ -192,17 +188,18 @@ describe("CoStream resolution", async () => {
192
188
  { as: meOnSecondPeer },
193
189
  (subscribedStream) => {
194
190
  console.log(
195
- "subscribedStream.by[me.id]",
196
- subscribedStream.by[me.id]
191
+ "subscribedStream[me.id]",
192
+ subscribedStream[me.id]
197
193
  );
198
194
  console.log(
199
- "subscribedStream.by[me.id]?.value?.by[me.id]?.value",
200
- subscribedStream.by[me.id]?.value?.by[me.id]?.value
195
+ "subscribedStream[me.id]?.value?.[me.id]?.value",
196
+ subscribedStream[me.id]?.value?.[me.id]?.value
201
197
  );
202
198
  console.log(
203
- "subscribedStream.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value",
204
- subscribedStream.by[me.id]?.value?.by[me.id]?.value
205
- ?.by[me.id]?.value
199
+ "subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value",
200
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[
201
+ me.id
202
+ ]?.value
206
203
  );
207
204
  Effect.runPromise(Queue.offer(queue, subscribedStream));
208
205
  }
@@ -212,25 +209,23 @@ describe("CoStream resolution", async () => {
212
209
  const te: T = stream;
213
210
 
214
211
  const update1 = yield* $(Queue.take(queue));
215
- expect(update1.by[me.id]?.value).toEqual(null);
212
+ expect(update1[me.id]?.value).toEqual(null);
216
213
 
217
214
  const update2 = yield* $(Queue.take(queue));
218
- expect(update2.by[me.id]?.value).toBeDefined();
219
- expect(update2.by[me.id]?.value?.by[me.id]?.value).toBe(null);
215
+ expect(update2[me.id]?.value).toBeDefined();
216
+ expect(update2[me.id]?.value?.[me.id]?.value).toBe(null);
220
217
 
221
218
  const update3 = yield* $(Queue.take(queue));
219
+ expect(update3[me.id]?.value?.[me.id]?.value).toBeDefined();
222
220
  expect(
223
- update3.by[me.id]?.value?.by[me.id]?.value
224
- ).toBeDefined();
225
- expect(
226
- update3.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value
221
+ update3[me.id]?.value?.[me.id]?.value?.[me.id]?.value
227
222
  ).toBe("milk");
228
223
 
229
- update3.by[me.id]!.value!.by[me.id]!.value!.push("bread");
224
+ update3[me.id]!.value![me.id]!.value!.push("bread");
230
225
 
231
226
  const update4 = yield* $(Queue.take(queue));
232
227
  expect(
233
- update4.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value
228
+ update4[me.id]?.value?.[me.id]?.value?.[me.id]?.value
234
229
  ).toBe("bread");
235
230
 
236
231
  // When assigning a new nested stream, we get an update
@@ -246,14 +241,14 @@ describe("CoStream resolution", async () => {
246
241
 
247
242
  const update5 = yield* $(Queue.take(queue));
248
243
  expect(
249
- update5.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value
244
+ update5[me.id]?.value?.[me.id]?.value?.[me.id]?.value
250
245
  ).toBe("butter");
251
246
 
252
247
  // we get updates when the new nested stream changes
253
248
  newTwiceNested.push("jam");
254
249
  const update6 = yield* $(Queue.take(queue));
255
250
  expect(
256
- update6.by[me.id]?.value?.by[me.id]?.value?.by[me.id]?.value
251
+ update6[me.id]?.value?.[me.id]?.value?.[me.id]?.value
257
252
  ).toBe("jam");
258
253
  })
259
254
  );
@@ -1,6 +0,0 @@
1
- import { Date } from "@effect/schema/Schema";
2
- export const Encoders = {
3
- Date,
4
- };
5
- export const indexSignature = Symbol.for("indexSignature");
6
- //# sourceMappingURL=schema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/implementation/schema.ts"],"names":[],"mappings":"AAkCA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAE7C,MAAM,CAAC,MAAM,QAAQ,GAAG;IACpB,IAAI;CACP,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC"}
@@ -1,69 +0,0 @@
1
- import type { JsonValue } from "cojson";
2
- import type { CoValue, CoValueClass } from "../internal.js";
3
- import type { Schema, TypeId } from "@effect/schema/Schema";
4
-
5
- export type PrimitiveField = "json";
6
- export type EncodedField<V> = { encoded: Encoder<V> };
7
- export type RefField<V extends CoValue> = {
8
- ref: () => CoValueClass<V>;
9
- };
10
-
11
- export type FieldDescriptor =
12
- | PrimitiveField
13
- | RefField<CoValue>
14
- | EncodedField<any>;
15
-
16
- export type FieldDescriptorFor<Field> = NonNullable<Field> extends CoValue
17
- ? RefField<NonNullable<Field>>
18
- : NonNullable<Field> extends JsonValue
19
- ? PrimitiveField
20
- : EncodedField<NonNullable<Field>>;
21
-
22
- export type EffectSchemaWithInputAndOutput<A, I = A> = Schema<
23
- any,
24
- any,
25
- never
26
- > & {
27
- [TypeId]: {
28
- _A: (_: any) => A;
29
- _I: (_: any) => I;
30
- };
31
- };
32
-
33
- export type Encoder<V> = EffectSchemaWithInputAndOutput<V, JsonValue>;
34
-
35
- import { Date } from "@effect/schema/Schema";
36
-
37
- export const Encoders = {
38
- Date,
39
- };
40
-
41
- export const indexSignature = Symbol.for("indexSignature");
42
- export type indexSignature = typeof indexSignature;
43
-
44
- export type EnsureCoValueNullable<
45
- V,
46
- Key extends string | indexSignature,
47
- > = NonNullable<V> extends CoValue
48
- ? null extends V
49
- ? V
50
- : Key extends string
51
- ? [
52
- `👋 CoMap fields that are CoValue references should be nullable, declare ${Key} as:`,
53
- V | null,
54
- ]
55
- : [
56
- `👋 CoMap fields that are CoValue references should be nullable, declare [indexSignature] as:`,
57
- V | null,
58
- ]
59
- : V;
60
-
61
- export type EnsureItemNullable<Item, ContainerType extends string> =
62
- NonNullable<Item> extends CoValue
63
- ? null extends Item
64
- ? any
65
- : [
66
- `👋 CoList items that are CoValue references should be nullable, make sure the Item generic parameter of ${ContainerType} is:`,
67
- Item | null
68
- ]
69
- : any;