jazz-tools 0.7.1 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/.turbo/turbo-lint.log +1 -1
  3. package/.turbo/turbo-test.log +33 -33
  4. package/CHANGELOG.md +177 -165
  5. package/LICENSE.txt +1 -1
  6. package/dist/coValues/account.js +41 -4
  7. package/dist/coValues/account.js.map +1 -1
  8. package/dist/coValues/coList.js +179 -8
  9. package/dist/coValues/coList.js.map +1 -1
  10. package/dist/coValues/coMap.js +141 -5
  11. package/dist/coValues/coMap.js.map +1 -1
  12. package/dist/coValues/coStream.js +49 -1
  13. package/dist/coValues/coStream.js.map +1 -1
  14. package/dist/coValues/group.js +26 -2
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/coValues/interfaces.js +32 -48
  17. package/dist/coValues/interfaces.js.map +1 -1
  18. package/dist/implementation/schema.js.map +1 -1
  19. package/dist/implementation/subscriptionScope.js.map +1 -1
  20. package/dist/index.js +1 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/tests/deepLoading.test.js +2 -2
  23. package/dist/tests/deepLoading.test.js.map +1 -1
  24. package/dist/tests/groupsAndAccounts.test.js +3 -3
  25. package/dist/tests/groupsAndAccounts.test.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/coValues/account.ts +106 -13
  28. package/src/coValues/coList.ts +236 -23
  29. package/src/coValues/coMap.ts +194 -11
  30. package/src/coValues/coStream.ts +128 -11
  31. package/src/coValues/group.ts +78 -5
  32. package/src/coValues/interfaces.ts +141 -211
  33. package/src/implementation/schema.ts +7 -4
  34. package/src/implementation/subscriptionScope.ts +3 -3
  35. package/src/index.ts +7 -0
  36. package/src/tests/deepLoading.test.ts +6 -3
  37. package/src/tests/groupsAndAccounts.test.ts +4 -4
@@ -16,8 +16,12 @@ import type {
16
16
  Group,
17
17
  ID,
18
18
  IfCo,
19
- ClassOf,
20
19
  UnCo,
20
+ AccountCtx,
21
+ CoValueClass,
22
+ DeeplyLoaded,
23
+ DepthsIn,
24
+ UnavailableError,
21
25
  } from "../internal.js";
22
26
  import {
23
27
  ItemsSym,
@@ -29,8 +33,15 @@ import {
29
33
  InitValues,
30
34
  SchemaInit,
31
35
  isRefEncoded,
36
+ loadCoValue,
37
+ loadCoValueEf,
38
+ subscribeToCoValue,
39
+ subscribeToCoValueEf,
40
+ ensureCoValueLoaded,
41
+ subscribeToExistingCoValue,
32
42
  } from "../internal.js";
33
43
  import { encodeSync, decodeSync } from "@effect/schema/Schema";
44
+ import { Effect, Stream } from "effect";
34
45
 
35
46
  export type CoStreamEntry<Item> = SingleCoStreamEntry<Item> & {
36
47
  all: IterableIterator<SingleCoStreamEntry<Item>>;
@@ -46,10 +57,7 @@ export type SingleCoStreamEntry<Item> = {
46
57
 
47
58
  /** @category CoValues */
48
59
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
- export class CoStream<Item = any>
50
- extends CoValueBase
51
- implements CoValue<"CoStream", RawCoStream>
52
- {
60
+ export class CoStream<Item = any> extends CoValueBase implements CoValue {
53
61
  static Of<Item>(item: IfCo<Item, Item>): typeof CoStream<Item> {
54
62
  return class CoStreamOf extends CoStream<Item> {
55
63
  [co.items] = item;
@@ -114,7 +122,7 @@ export class CoStream<Item = any>
114
122
  }
115
123
 
116
124
  static create<S extends CoStream>(
117
- this: ClassOf<S>,
125
+ this: CoValueClass<S>,
118
126
  init: S extends CoStream<infer Item> ? UnCo<Item>[] : never,
119
127
  options: { owner: Account | Group },
120
128
  ) {
@@ -178,6 +186,62 @@ export class CoStream<Item = any>
178
186
  this._schema ||= {};
179
187
  Object.assign(this._schema, def);
180
188
  }
189
+
190
+ /** @category Subscription & Loading */
191
+ static load<S extends CoStream, Depth>(
192
+ this: CoValueClass<S>,
193
+ id: ID<S>,
194
+ as: Account,
195
+ depth: Depth & DepthsIn<S>,
196
+ ): Promise<DeeplyLoaded<S, Depth> | undefined> {
197
+ return loadCoValue(this, id, as, depth);
198
+ }
199
+
200
+ /** @category Subscription & Loading */
201
+ static loadEf<S extends CoStream, Depth>(
202
+ this: CoValueClass<S>,
203
+ id: ID<S>,
204
+ depth: Depth & DepthsIn<S>,
205
+ ): Effect.Effect<DeeplyLoaded<S, Depth>, UnavailableError, AccountCtx> {
206
+ return loadCoValueEf<S, Depth>(this, id, depth);
207
+ }
208
+
209
+ /** @category Subscription & Loading */
210
+ static subscribe<S extends CoStream, Depth>(
211
+ this: CoValueClass<S>,
212
+ id: ID<S>,
213
+ as: Account,
214
+ depth: Depth & DepthsIn<S>,
215
+ listener: (value: DeeplyLoaded<S, Depth>) => void,
216
+ ): () => void {
217
+ return subscribeToCoValue<S, Depth>(this, id, as, depth, listener);
218
+ }
219
+
220
+ /** @category Subscription & Loading */
221
+ static subscribeEf<S extends CoStream, Depth>(
222
+ this: CoValueClass<S>,
223
+ id: ID<S>,
224
+ depth: Depth & DepthsIn<S>,
225
+ ): Stream.Stream<DeeplyLoaded<S, Depth>, UnavailableError, AccountCtx> {
226
+ return subscribeToCoValueEf<S, Depth>(this, id, depth);
227
+ }
228
+
229
+ /** @category Subscription & Loading */
230
+ ensureLoaded<S extends CoStream, Depth>(
231
+ this: S,
232
+ depth: Depth & DepthsIn<S>,
233
+ ): Promise<DeeplyLoaded<S, Depth> | undefined> {
234
+ return ensureCoValueLoaded(this, depth);
235
+ }
236
+
237
+ /** @category Subscription & Loading */
238
+ subscribe<S extends CoStream, Depth>(
239
+ this: S,
240
+ depth: Depth & DepthsIn<S>,
241
+ listener: (value: DeeplyLoaded<S, Depth>) => void,
242
+ ): () => void {
243
+ return subscribeToExistingCoValue(this, depth, listener);
244
+ }
181
245
  }
182
246
 
183
247
  function entryFromRawEntry<Item>(
@@ -437,10 +501,7 @@ const CoStreamPerSessionProxyHandler = (
437
501
  });
438
502
 
439
503
  /** @category CoValues */
440
- export class BinaryCoStream
441
- extends CoValueBase
442
- implements CoValue<"BinaryCoStream", RawBinaryCoStream>
443
- {
504
+ export class BinaryCoStream extends CoValueBase implements CoValue {
444
505
  declare id: ID<this>;
445
506
  declare _type: "BinaryCoStream";
446
507
  declare _raw: RawBinaryCoStream;
@@ -476,7 +537,7 @@ export class BinaryCoStream
476
537
  }
477
538
 
478
539
  static create<S extends BinaryCoStream>(
479
- this: ClassOf<S>,
540
+ this: CoValueClass<S>,
480
541
  options: { owner: Account | Group },
481
542
  ) {
482
543
  return new this(options);
@@ -584,4 +645,60 @@ export class BinaryCoStream
584
645
  [inspect]() {
585
646
  return this.toJSON();
586
647
  }
648
+
649
+ /** @category Subscription & Loading */
650
+ static load<B extends BinaryCoStream, Depth>(
651
+ this: CoValueClass<B>,
652
+ id: ID<B>,
653
+ as: Account,
654
+ depth: Depth & DepthsIn<B>,
655
+ ): Promise<DeeplyLoaded<B, Depth> | undefined> {
656
+ return loadCoValue(this, id, as, depth);
657
+ }
658
+
659
+ /** @category Subscription & Loading */
660
+ static loadEf<B extends BinaryCoStream, Depth>(
661
+ this: CoValueClass<B>,
662
+ id: ID<B>,
663
+ depth: Depth & DepthsIn<B>,
664
+ ): Effect.Effect<DeeplyLoaded<B, Depth>, UnavailableError, AccountCtx> {
665
+ return loadCoValueEf<B, Depth>(this, id, depth);
666
+ }
667
+
668
+ /** @category Subscription & Loading */
669
+ static subscribe<B extends BinaryCoStream, Depth>(
670
+ this: CoValueClass<B>,
671
+ id: ID<B>,
672
+ as: Account,
673
+ depth: Depth & DepthsIn<B>,
674
+ listener: (value: DeeplyLoaded<B, Depth>) => void,
675
+ ): () => void {
676
+ return subscribeToCoValue<B, Depth>(this, id, as, depth, listener);
677
+ }
678
+
679
+ /** @category Subscription & Loading */
680
+ static subscribeEf<B extends BinaryCoStream, Depth>(
681
+ this: CoValueClass<B>,
682
+ id: ID<B>,
683
+ depth: Depth & DepthsIn<B>,
684
+ ): Stream.Stream<DeeplyLoaded<B, Depth>, UnavailableError, AccountCtx> {
685
+ return subscribeToCoValueEf<B, Depth>(this, id, depth);
686
+ }
687
+
688
+ /** @category Subscription & Loading */
689
+ ensureLoaded<B extends BinaryCoStream, Depth>(
690
+ this: B,
691
+ depth: Depth & DepthsIn<B>,
692
+ ): Promise<DeeplyLoaded<B, Depth> | undefined> {
693
+ return ensureCoValueLoaded(this, depth);
694
+ }
695
+
696
+ /** @category Subscription & Loading */
697
+ subscribe<B extends BinaryCoStream, Depth>(
698
+ this: B,
699
+ depth: Depth & DepthsIn<B>,
700
+ listener: (value: DeeplyLoaded<B, Depth>) => void,
701
+ ): () => void {
702
+ return subscribeToExistingCoValue(this, depth, listener);
703
+ }
587
704
  }
@@ -1,5 +1,15 @@
1
1
  import type { AccountID, Everyone, RawGroup, Role } from "cojson";
2
- import type { CoValue, ID, RefEncoded, Schema, ClassOf } from "../internal.js";
2
+ import type {
3
+ CoValue,
4
+ ID,
5
+ RefEncoded,
6
+ Schema,
7
+ AccountCtx,
8
+ CoValueClass,
9
+ DeeplyLoaded,
10
+ DepthsIn,
11
+ UnavailableError,
12
+ } from "../internal.js";
3
13
  import {
4
14
  Account,
5
15
  CoMap,
@@ -9,7 +19,14 @@ import {
9
19
  isControlledAccount,
10
20
  AccountAndGroupProxyHandler,
11
21
  MembersSym,
22
+ loadCoValue,
23
+ loadCoValueEf,
24
+ subscribeToCoValue,
25
+ subscribeToCoValueEf,
26
+ ensureCoValueLoaded,
27
+ subscribeToExistingCoValue,
12
28
  } from "../internal.js";
29
+ import { Effect, Stream } from "effect";
13
30
 
14
31
  /** @category Identity & Permissions */
15
32
  export class Profile extends CoMap {
@@ -17,7 +34,7 @@ export class Profile extends CoMap {
17
34
  }
18
35
 
19
36
  /** @category Identity & Permissions */
20
- export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
37
+ export class Group extends CoValueBase implements CoValue {
21
38
  declare id: ID<this>;
22
39
  declare _type: "Group";
23
40
  static {
@@ -41,7 +58,7 @@ export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
41
58
  [MembersSym]: {
42
59
  ref: () => Account,
43
60
  optional: false,
44
- } satisfies Schema,
61
+ } satisfies RefEncoded<Account>,
45
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
63
  } as any;
47
64
  Object.defineProperty(this.prototype, "_schema", {
@@ -97,7 +114,7 @@ export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
97
114
  const initOwner = options.owner;
98
115
  if (!initOwner) throw new Error("No owner provided");
99
116
  if (
100
- initOwner instanceof Account &&
117
+ initOwner._type === "Account" &&
101
118
  isControlledAccount(initOwner)
102
119
  ) {
103
120
  const rawOwner = initOwner._raw;
@@ -124,7 +141,7 @@ export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
124
141
  }
125
142
 
126
143
  static create<G extends Group>(
127
- this: ClassOf<G>,
144
+ this: CoValueClass<G>,
128
145
  options: { owner: Account },
129
146
  ) {
130
147
  return new this(options);
@@ -170,4 +187,60 @@ export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
170
187
  };
171
188
  });
172
189
  }
190
+
191
+ /** @category Subscription & Loading */
192
+ static load<G extends Group, Depth>(
193
+ this: CoValueClass<G>,
194
+ id: ID<G>,
195
+ as: Account,
196
+ depth: Depth & DepthsIn<G>,
197
+ ): Promise<DeeplyLoaded<G, Depth> | undefined> {
198
+ return loadCoValue(this, id, as, depth);
199
+ }
200
+
201
+ /** @category Subscription & Loading */
202
+ static loadEf<G extends Group, Depth>(
203
+ this: CoValueClass<G>,
204
+ id: ID<G>,
205
+ depth: Depth & DepthsIn<G>,
206
+ ): Effect.Effect<DeeplyLoaded<G, Depth>, UnavailableError, AccountCtx> {
207
+ return loadCoValueEf<G, Depth>(this, id, depth);
208
+ }
209
+
210
+ /** @category Subscription & Loading */
211
+ static subscribe<G extends Group, Depth>(
212
+ this: CoValueClass<G>,
213
+ id: ID<G>,
214
+ as: Account,
215
+ depth: Depth & DepthsIn<G>,
216
+ listener: (value: DeeplyLoaded<G, Depth>) => void,
217
+ ): () => void {
218
+ return subscribeToCoValue<G, Depth>(this, id, as, depth, listener);
219
+ }
220
+
221
+ /** @category Subscription & Loading */
222
+ static subscribeEf<G extends Group, Depth>(
223
+ this: CoValueClass<G>,
224
+ id: ID<G>,
225
+ depth: Depth & DepthsIn<G>,
226
+ ): Stream.Stream<DeeplyLoaded<G, Depth>, UnavailableError, AccountCtx> {
227
+ return subscribeToCoValueEf<G, Depth>(this, id, depth);
228
+ }
229
+
230
+ /** @category Subscription & Loading */
231
+ ensureLoaded<G extends Group, Depth>(
232
+ this: G,
233
+ depth: Depth & DepthsIn<G>,
234
+ ): Promise<DeeplyLoaded<G, Depth> | undefined> {
235
+ return ensureCoValueLoaded(this, depth);
236
+ }
237
+
238
+ /** @category Subscription & Loading */
239
+ subscribe<G extends Group, Depth>(
240
+ this: G,
241
+ depth: Depth & DepthsIn<G>,
242
+ listener: (value: DeeplyLoaded<G, Depth>) => void,
243
+ ): () => void {
244
+ return subscribeToExistingCoValue(this, depth, listener);
245
+ }
173
246
  }