jazz-tools 0.13.30 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/.turbo/turbo-build.log +8 -8
  2. package/CHANGELOG.md +23 -3
  3. package/dist/auth/DemoAuth.d.ts.map +1 -1
  4. package/dist/auth/PassphraseAuth.d.ts +1 -3
  5. package/dist/auth/PassphraseAuth.d.ts.map +1 -1
  6. package/dist/{chunk-LMV6J7GN.js → chunk-2ASOGEYA.js} +3531 -3269
  7. package/dist/chunk-2ASOGEYA.js.map +1 -0
  8. package/dist/coValues/CoValueBase.d.ts +22 -0
  9. package/dist/coValues/CoValueBase.d.ts.map +1 -0
  10. package/dist/coValues/account.d.ts +12 -12
  11. package/dist/coValues/account.d.ts.map +1 -1
  12. package/dist/coValues/coFeed.d.ts +24 -25
  13. package/dist/coValues/coFeed.d.ts.map +1 -1
  14. package/dist/coValues/coList.d.ts +10 -13
  15. package/dist/coValues/coList.d.ts.map +1 -1
  16. package/dist/coValues/coMap.d.ts +32 -35
  17. package/dist/coValues/coMap.d.ts.map +1 -1
  18. package/dist/coValues/coPlainText.d.ts.map +1 -1
  19. package/dist/coValues/deepLoading.d.ts +28 -22
  20. package/dist/coValues/deepLoading.d.ts.map +1 -1
  21. package/dist/coValues/extensions/imageDef.d.ts +12 -11
  22. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  23. package/dist/coValues/group.d.ts +5 -9
  24. package/dist/coValues/group.d.ts.map +1 -1
  25. package/dist/coValues/inbox.d.ts +2 -3
  26. package/dist/coValues/inbox.d.ts.map +1 -1
  27. package/dist/coValues/interfaces.d.ts +8 -34
  28. package/dist/coValues/interfaces.d.ts.map +1 -1
  29. package/dist/coValues/profile.d.ts +4 -14
  30. package/dist/coValues/profile.d.ts.map +1 -1
  31. package/dist/coValues/registeredSchemas.d.ts +1 -3
  32. package/dist/coValues/registeredSchemas.d.ts.map +1 -1
  33. package/dist/coValues/schemaUnion.d.ts +6 -6
  34. package/dist/exports.d.ts +12 -16
  35. package/dist/exports.d.ts.map +1 -1
  36. package/dist/implementation/ContextManager.d.ts +1 -1
  37. package/dist/implementation/ContextManager.d.ts.map +1 -1
  38. package/dist/implementation/activeAccountContext.d.ts +1 -1
  39. package/dist/implementation/activeAccountContext.d.ts.map +1 -1
  40. package/dist/implementation/createContext.d.ts +10 -10
  41. package/dist/implementation/createContext.d.ts.map +1 -1
  42. package/dist/implementation/invites.d.ts +6 -6
  43. package/dist/implementation/invites.d.ts.map +1 -1
  44. package/dist/implementation/refs.d.ts +2 -2
  45. package/dist/implementation/refs.d.ts.map +1 -1
  46. package/dist/implementation/schema.d.ts +21 -28
  47. package/dist/implementation/schema.d.ts.map +1 -1
  48. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
  49. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
  50. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
  51. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
  52. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
  53. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
  54. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
  55. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
  56. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
  57. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
  58. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
  59. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
  60. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
  61. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
  62. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
  63. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
  64. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
  65. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
  66. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
  67. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
  68. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
  69. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
  70. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
  71. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
  72. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
  73. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
  74. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
  75. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
  76. package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
  77. package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
  78. package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
  79. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
  80. package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
  81. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
  82. package/dist/index.js +295 -10
  83. package/dist/index.js.map +1 -1
  84. package/dist/internal.d.ts +34 -0
  85. package/dist/internal.d.ts.map +1 -1
  86. package/dist/subscribe/SubscriptionScope.d.ts +2 -2
  87. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  88. package/dist/subscribe/utils.d.ts +2 -2
  89. package/dist/subscribe/utils.d.ts.map +1 -1
  90. package/dist/testing.d.ts +10 -8
  91. package/dist/testing.d.ts.map +1 -1
  92. package/dist/testing.js +1 -1
  93. package/dist/testing.js.map +1 -1
  94. package/dist/tests/utils.d.ts +6 -2
  95. package/dist/tests/utils.d.ts.map +1 -1
  96. package/dist/types.d.ts +1 -7
  97. package/dist/types.d.ts.map +1 -1
  98. package/package.json +3 -2
  99. package/src/auth/DemoAuth.ts +1 -2
  100. package/src/auth/PassphraseAuth.ts +1 -1
  101. package/src/coValues/CoValueBase.ts +83 -0
  102. package/src/coValues/account.ts +53 -43
  103. package/src/coValues/coFeed.ts +65 -83
  104. package/src/coValues/coList.ts +28 -21
  105. package/src/coValues/coMap.ts +54 -38
  106. package/src/coValues/coPlainText.ts +4 -1
  107. package/src/coValues/deepLoading.ts +46 -36
  108. package/src/coValues/extensions/imageDef.ts +21 -19
  109. package/src/coValues/group.ts +37 -38
  110. package/src/coValues/inbox.ts +24 -11
  111. package/src/coValues/interfaces.ts +29 -93
  112. package/src/coValues/profile.ts +12 -13
  113. package/src/coValues/registeredSchemas.ts +1 -3
  114. package/src/coValues/schemaUnion.ts +6 -6
  115. package/src/exports.ts +47 -25
  116. package/src/implementation/activeAccountContext.ts +1 -1
  117. package/src/implementation/createContext.ts +39 -24
  118. package/src/implementation/invites.ts +15 -12
  119. package/src/implementation/refs.ts +6 -4
  120. package/src/implementation/schema.ts +22 -34
  121. package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
  122. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
  123. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
  124. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
  125. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
  126. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
  127. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
  128. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
  129. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
  130. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
  131. package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
  132. package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
  133. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
  134. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
  135. package/src/implementation/zodSchema/unionUtils.ts +139 -0
  136. package/src/implementation/zodSchema/zodCo.ts +409 -0
  137. package/src/implementation/zodSchema/zodSchema.ts +116 -0
  138. package/src/internal.ts +38 -0
  139. package/src/subscribe/SubscriptionScope.ts +30 -3
  140. package/src/subscribe/utils.ts +7 -2
  141. package/src/testing.ts +14 -16
  142. package/src/tests/ContextManager.test.ts +73 -47
  143. package/src/tests/DemoAuth.test.ts +1 -1
  144. package/src/tests/account.test.ts +6 -9
  145. package/src/tests/coFeed.test.ts +102 -63
  146. package/src/tests/coList.test.ts +82 -95
  147. package/src/tests/coMap.record.test.ts +53 -87
  148. package/src/tests/coMap.test.ts +297 -312
  149. package/src/tests/coPlainText.test.ts +19 -39
  150. package/src/tests/createContext.test.ts +33 -15
  151. package/src/tests/deepLoading.test.ts +397 -131
  152. package/src/tests/groupsAndAccounts.test.ts +81 -72
  153. package/src/tests/imageDef.test.ts +22 -13
  154. package/src/tests/inbox.test.ts +36 -29
  155. package/src/tests/load.test.ts +10 -10
  156. package/src/tests/patterns/requestToJoin.test.ts +31 -31
  157. package/src/tests/schema.test.ts +37 -38
  158. package/src/tests/schemaUnion.test.ts +54 -64
  159. package/src/tests/subscribe.test.ts +118 -116
  160. package/src/tests/testing.test.ts +33 -33
  161. package/src/tests/utils.ts +3 -2
  162. package/src/types.ts +1 -8
  163. package/dist/chunk-LMV6J7GN.js.map +0 -1
@@ -12,10 +12,12 @@ import type {
12
12
  import { MAX_RECOMMENDED_TX_SIZE, cojsonInternals } from "cojson";
13
13
  import type {
14
14
  AnonymousJazzAgent,
15
+ AnyAccountSchema,
15
16
  CoValue,
16
17
  CoValueClass,
18
+ Group,
17
19
  ID,
18
- IfCo,
20
+ InstanceOfSchema,
19
21
  RefsToResolve,
20
22
  RefsToResolveStrict,
21
23
  Resolved,
@@ -23,15 +25,17 @@ import type {
23
25
  SchemaFor,
24
26
  SubscribeListenerOptions,
25
27
  SubscribeRestArgs,
26
- UnCo,
27
28
  } from "../internal.js";
28
29
  import {
30
+ Account,
29
31
  CoValueBase,
30
32
  ItemsSym,
31
33
  Ref,
34
+ RegisteredSchemas,
32
35
  SchemaInit,
33
36
  accessChildById,
34
- co,
37
+ anySchemaToCoSchema,
38
+ coField,
35
39
  ensureCoValueLoaded,
36
40
  inspect,
37
41
  isRefEncoded,
@@ -41,10 +45,6 @@ import {
41
45
  subscribeToCoValueWithoutMe,
42
46
  subscribeToExistingCoValue,
43
47
  } from "../internal.js";
44
- import { RegisteredAccount } from "../types.js";
45
- import { type Account } from "./account.js";
46
- import { type Group } from "./group.js";
47
- import { RegisteredSchemas } from "./registeredSchemas.js";
48
48
 
49
49
  /** @deprecated Use CoFeedEntry instead */
50
50
  export type CoStreamEntry<Item> = CoFeedEntry<Item>;
@@ -59,7 +59,7 @@ export type SingleCoStreamEntry<Item> = SingleCoFeedEntry<Item>;
59
59
  export type SingleCoFeedEntry<Item> = {
60
60
  value: NonNullable<Item> extends CoValue ? NonNullable<Item> | null : Item;
61
61
  ref: NonNullable<Item> extends CoValue ? Ref<NonNullable<Item>> : never;
62
- by?: RegisteredAccount | null;
62
+ by: Account | null;
63
63
  madeAt: Date;
64
64
  tx: CojsonInternalTypes.TransactionID;
65
65
  };
@@ -83,22 +83,27 @@ export { CoFeed as CoStream };
83
83
  *
84
84
  * @category CoValues
85
85
  */
86
- export class CoFeed<Item = any> extends CoValueBase implements CoValue {
86
+ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
87
87
  /**
88
- * Declare a `CoFeed` by subclassing `CoFeed.Of(...)` and passing the item schema using a `co` primitive or a `co.ref`.
88
+ * Declare a `CoFeed` by subclassing `CoFeed.Of(...)` and passing the item schema using a `co` primitive or a `coField.ref`.
89
89
  *
90
90
  * @example
91
91
  * ```ts
92
- * class ColorFeed extends CoFeed.Of(co.string) {}
93
- * class AnimalFeed extends CoFeed.Of(co.ref(Animal)) {}
92
+ * class ColorFeed extends CoFeed.Of(coField.string) {}
93
+ * class AnimalFeed extends CoFeed.Of(coField.ref(Animal)) {}
94
94
  * ```
95
95
  *
96
96
  * @category Declaration
97
97
  */
98
- static Of<Item>(item: IfCo<Item, Item>): typeof CoFeed<Item> {
99
- return class CoFeedOf extends CoFeed<Item> {
100
- [co.items] = item;
98
+ static Of<Item>(item: Item): typeof CoFeed<Item> {
99
+ const cls = class CoFeedOf extends CoFeed<Item> {
100
+ [coField.items] = item;
101
101
  };
102
+
103
+ cls._schema ||= {};
104
+ cls._schema[ItemsSym] = (item as any)[SchemaInit];
105
+
106
+ return cls;
102
107
  }
103
108
 
104
109
  /**
@@ -120,10 +125,21 @@ export class CoFeed<Item = any> extends CoValueBase implements CoValue {
120
125
  static _schema: any;
121
126
  /** @internal */
122
127
  get _schema(): {
123
- [ItemsSym]: SchemaFor<Item>;
128
+ [ItemsSym]: SchemaFor<Item> | any;
124
129
  } {
125
130
  return (this.constructor as typeof CoFeed)._schema;
126
131
  }
132
+ /**
133
+ * The current account's view of this `CoFeed`
134
+ * @category Content
135
+ */
136
+ get byMe(): CoFeedEntry<Item> | undefined {
137
+ if (this._loadedAs._type === "Account") {
138
+ return this.perAccount[this._loadedAs.id];
139
+ } else {
140
+ return undefined;
141
+ }
142
+ }
127
143
 
128
144
  /**
129
145
  * The per-account view of this `CoFeed`
@@ -147,27 +163,24 @@ export class CoFeed<Item = any> extends CoValueBase implements CoValue {
147
163
  *
148
164
  * @category Content
149
165
  */
150
- [key: ID<Account>]: CoFeedEntry<Item>;
151
-
152
- /**
153
- * The current account's view of this `CoFeed`
154
- * @category Content
155
- */
156
- get byMe(): CoFeedEntry<Item> | undefined {
157
- if (this._loadedAs._type === "Account") {
158
- return this[this._loadedAs.id];
159
- } else {
160
- return undefined;
161
- }
166
+ get perAccount(): {
167
+ [key: ID<Account>]: CoFeedEntry<Item>;
168
+ } {
169
+ return new Proxy({}, CoStreamPerAccountProxyHandler(this)) as any;
162
170
  }
163
171
 
164
172
  /**
165
173
  * The per-session view of this `CoFeed`
166
174
  * @category Content
167
175
  */
168
- perSession!: {
176
+ get perSession(): {
169
177
  [key: SessionID]: CoFeedEntry<Item>;
170
- };
178
+ } {
179
+ return new Proxy(
180
+ {},
181
+ CoStreamPerSessionProxyHandler(this, this) as any,
182
+ ) as any;
183
+ }
171
184
 
172
185
  /**
173
186
  * The current session's view of this `CoFeed`
@@ -201,7 +214,7 @@ export class CoFeed<Item = any> extends CoValueBase implements CoValue {
201
214
  });
202
215
  }
203
216
 
204
- return new Proxy(this, CoStreamProxyHandler as ProxyHandler<this>);
217
+ return this;
205
218
  }
206
219
 
207
220
  /**
@@ -210,7 +223,7 @@ export class CoFeed<Item = any> extends CoValueBase implements CoValue {
210
223
  */
211
224
  static create<S extends CoFeed>(
212
225
  this: CoValueClass<S>,
213
- init: S extends CoFeed<infer Item> ? UnCo<Item>[] : never,
226
+ init: S extends CoFeed<infer Item> ? Item[] : never,
214
227
  options?: { owner: Account | Group } | Account | Group,
215
228
  ) {
216
229
  const { owner } = parseCoValueCreateOptions(options);
@@ -471,7 +484,7 @@ function entryFromRawEntry<Item>(
471
484
  return (
472
485
  accountID &&
473
486
  accessChildById(accessFrom, accountID, {
474
- ref: RegisteredSchemas["Account"],
487
+ ref: Account,
475
488
  optional: false,
476
489
  })
477
490
  );
@@ -485,23 +498,25 @@ function entryFromRawEntry<Item>(
485
498
  * The proxy handler for `CoFeed` instances
486
499
  * @internal
487
500
  */
488
- export const CoStreamProxyHandler: ProxyHandler<CoFeed> = {
489
- get(target, key, receiver) {
501
+ export const CoStreamPerAccountProxyHandler = (
502
+ innerTarget: CoFeed,
503
+ ): ProxyHandler<{}> => ({
504
+ get(_target, key, receiver) {
490
505
  if (typeof key === "string" && key.startsWith("co_")) {
491
- const rawEntry = target._raw.lastItemBy(key as RawAccountID);
506
+ const rawEntry = innerTarget._raw.lastItemBy(key as RawAccountID);
492
507
 
493
508
  if (!rawEntry) return;
494
509
  const entry = entryFromRawEntry(
495
510
  receiver,
496
511
  rawEntry,
497
- target._loadedAs,
512
+ innerTarget._loadedAs,
498
513
  key as unknown as ID<Account>,
499
- target._schema[ItemsSym],
514
+ innerTarget._schema[ItemsSym],
500
515
  );
501
516
 
502
517
  Object.defineProperty(entry, "all", {
503
518
  get: () => {
504
- const allRawEntries = target._raw.itemsBy(key as RawAccountID);
519
+ const allRawEntries = innerTarget._raw.itemsBy(key as RawAccountID);
505
520
  return (function* () {
506
521
  while (true) {
507
522
  const rawEntry = allRawEntries.next();
@@ -509,9 +524,9 @@ export const CoStreamProxyHandler: ProxyHandler<CoFeed> = {
509
524
  yield entryFromRawEntry(
510
525
  receiver,
511
526
  rawEntry.value,
512
- target._loadedAs,
527
+ innerTarget._loadedAs,
513
528
  key as unknown as ID<Account>,
514
- target._schema[ItemsSym],
529
+ innerTarget._schema[ItemsSym],
515
530
  );
516
531
  }
517
532
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -520,47 +535,14 @@ export const CoStreamProxyHandler: ProxyHandler<CoFeed> = {
520
535
  });
521
536
 
522
537
  return entry;
523
- } else if (key === "perSession") {
524
- return new Proxy({}, CoStreamPerSessionProxyHandler(target, receiver));
525
- } else {
526
- return Reflect.get(target, key, receiver);
527
- }
528
- },
529
- set(target, key, value, receiver) {
530
- if (key === ItemsSym && typeof value === "object" && SchemaInit in value) {
531
- (target.constructor as typeof CoFeed)._schema ||= {};
532
- (target.constructor as typeof CoFeed)._schema[ItemsSym] =
533
- value[SchemaInit];
534
- return true;
535
538
  } else {
536
- return Reflect.set(target, key, value, receiver);
539
+ return Reflect.get(innerTarget, key, receiver);
537
540
  }
538
541
  },
539
- defineProperty(target, key, descriptor) {
540
- if (
541
- descriptor.value &&
542
- key === ItemsSym &&
543
- typeof descriptor.value === "object" &&
544
- SchemaInit in descriptor.value
545
- ) {
546
- (target.constructor as typeof CoFeed)._schema ||= {};
547
- (target.constructor as typeof CoFeed)._schema[ItemsSym] =
548
- descriptor.value[SchemaInit];
549
- return true;
550
- } else {
551
- return Reflect.defineProperty(target, key, descriptor);
552
- }
542
+ ownKeys(_target) {
543
+ return Array.from(innerTarget._raw.accounts());
553
544
  },
554
- ownKeys(target) {
555
- const keys = Reflect.ownKeys(target);
556
-
557
- for (const accountID of target._raw.accounts()) {
558
- keys.push(accountID);
559
- }
560
-
561
- return keys;
562
- },
563
- getOwnPropertyDescriptor(target, key) {
545
+ getOwnPropertyDescriptor(_target, key) {
564
546
  if (typeof key === "string" && key.startsWith("co_")) {
565
547
  return {
566
548
  configurable: true,
@@ -568,10 +550,10 @@ export const CoStreamProxyHandler: ProxyHandler<CoFeed> = {
568
550
  writable: false,
569
551
  };
570
552
  } else {
571
- return Reflect.getOwnPropertyDescriptor(target, key);
553
+ return Reflect.getOwnPropertyDescriptor(innerTarget, key);
572
554
  }
573
555
  },
574
- };
556
+ });
575
557
 
576
558
  /**
577
559
  * The proxy handler for the per-session view of a `CoFeed`
@@ -652,10 +634,10 @@ export { FileStream as BinaryCoStream };
652
634
  * `FileStream` can be referenced in schemas.
653
635
  *
654
636
  * ```ts
655
- * import { co, FileStream } from "jazz-tools";
637
+ * import { coField, FileStream } from "jazz-tools";
656
638
  *
657
639
  * class MyCoMap extends CoMap {
658
- * file = co.ref(FileStream);
640
+ * file = coField.ref(FileStream);
659
641
  * }
660
642
  * ```
661
643
  *
@@ -817,7 +799,7 @@ export class FileStream extends CoValueBase implements CoValue {
817
799
  *
818
800
  * @example
819
801
  * ```ts
820
- * import { co, FileStream } from "jazz-tools";
802
+ * import { coField, FileStream } from "jazz-tools";
821
803
  *
822
804
  * const fileStream = await FileStream.createFromBlob(file, {owner: group})
823
805
  * ```
@@ -2,10 +2,14 @@ import type { JsonValue, RawCoList } from "cojson";
2
2
  import { ControlledAccount, RawAccount } from "cojson";
3
3
  import { calcPatch } from "fast-myers-diff";
4
4
  import type {
5
+ Account,
6
+ AnyAccountSchema,
5
7
  CoValue,
6
8
  CoValueClass,
7
9
  CoValueFromRaw,
10
+ Group,
8
11
  ID,
12
+ InstanceOfSchema,
9
13
  RefEncoded,
10
14
  RefsToResolve,
11
15
  RefsToResolveStrict,
@@ -14,15 +18,17 @@ import type {
14
18
  SchemaFor,
15
19
  SubscribeListenerOptions,
16
20
  SubscribeRestArgs,
17
- UnCo,
18
21
  } from "../internal.js";
19
22
  import {
20
23
  AnonymousJazzAgent,
21
24
  ItemsSym,
22
25
  Ref,
26
+ RegisteredSchemas,
23
27
  SchemaInit,
24
28
  accessChildByKey,
25
- co,
29
+ anySchemaToCoSchema,
30
+ coField,
31
+ coValuesCache,
26
32
  ensureCoValueLoaded,
27
33
  inspect,
28
34
  isRefEncoded,
@@ -33,11 +39,6 @@ import {
33
39
  subscribeToCoValueWithoutMe,
34
40
  subscribeToExistingCoValue,
35
41
  } from "../internal.js";
36
- import { coValuesCache } from "../lib/cache.js";
37
- import { RegisteredAccount } from "../types.js";
38
- import { type Account } from "./account.js";
39
- import { type Group } from "./group.js";
40
- import { RegisteredSchemas } from "./registeredSchemas.js";
41
42
 
42
43
  /**
43
44
  * CoLists are collaborative versions of plain arrays.
@@ -57,17 +58,17 @@ import { RegisteredSchemas } from "./registeredSchemas.js";
57
58
  * @category CoValues
58
59
  */
59
60
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
- export class CoList<Item = any> extends Array<Item> implements CoValue {
61
+ export class CoList<out Item = any> extends Array<Item> implements CoValue {
61
62
  /**
62
63
  * Declare a `CoList` by subclassing `CoList.Of(...)` and passing the item schema using `co`.
63
64
  *
64
65
  * @example
65
66
  * ```ts
66
67
  * class ColorList extends CoList.Of(
67
- * co.string
68
+ * coField.string
68
69
  * ) {}
69
70
  * class AnimalList extends CoList.Of(
70
- * co.ref(Animal)
71
+ * coField.ref(Animal)
71
72
  * ) {}
72
73
  * ```
73
74
  *
@@ -76,7 +77,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
76
77
  static Of<Item>(item: Item): typeof CoList<Item> {
77
78
  // TODO: cache superclass for item class
78
79
  return class CoListOf extends CoList<Item> {
79
- [co.items] = item;
80
+ [coField.items] = item;
80
81
  };
81
82
  }
82
83
 
@@ -108,7 +109,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
108
109
  static _schema: any;
109
110
  /** @internal */
110
111
  get _schema(): {
111
- [ItemsSym]: SchemaFor<Item>;
112
+ [ItemsSym]: SchemaFor<Item> | any;
112
113
  } {
113
114
  return (this.constructor as typeof CoList)._schema;
114
115
  }
@@ -116,12 +117,14 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
116
117
  /** @category Collaboration */
117
118
  get _owner(): Account | Group {
118
119
  return this._raw.group instanceof RawAccount
119
- ? RegisteredSchemas["Account"].fromRaw(this._raw.group)
120
+ ? anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
121
+ this._raw.group,
122
+ )
120
123
  : RegisteredSchemas["Group"].fromRaw(this._raw.group);
121
124
  }
122
125
 
123
126
  /**
124
- * If a `CoList`'s items are a `co.ref(...)`, you can use `coList._refs[i]` to access
127
+ * If a `CoList`'s items are a `coField.ref(...)`, you can use `coList._refs[i]` to access
125
128
  * the `Ref` instead of the potentially loaded/null value.
126
129
  *
127
130
  * This allows you to always get the ID or load the value manually.
@@ -138,7 +141,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
138
141
  **/
139
142
  get _refs(): {
140
143
  [idx: number]: Exclude<Item, null> extends CoValue
141
- ? Ref<UnCo<Exclude<Item, null>>>
144
+ ? Ref<Exclude<Item, null>>
142
145
  : never;
143
146
  } & {
144
147
  length: number;
@@ -160,7 +163,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
160
163
  [idx: number]: {
161
164
  value?: Item;
162
165
  ref?: Item extends CoValue ? Ref<Item> : never;
163
- by?: RegisteredAccount;
166
+ by: Account | null;
164
167
  madeAt: Date;
165
168
  };
166
169
  } {
@@ -172,7 +175,9 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
172
175
 
173
176
  if (agent instanceof ControlledAccount) {
174
177
  return coValuesCache.get(agent.account, () =>
175
- RegisteredSchemas["Account"].fromRaw(agent.account),
178
+ anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
179
+ agent.account,
180
+ ),
176
181
  );
177
182
  }
178
183
 
@@ -231,7 +236,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
231
236
  **/
232
237
  static create<L extends CoList>(
233
238
  this: CoValueClass<L>,
234
- items: UnCo<L[number]>[],
239
+ items: L[number][],
235
240
  options?: { owner: Account | Group } | Account | Group,
236
241
  ) {
237
242
  const { owner } = parseCoValueCreateOptions(options);
@@ -580,7 +585,7 @@ const CoListProxyHandler: ProxyHandler<CoList> = {
580
585
  ? undefined
581
586
  : itemDescriptor.encoded.decode(rawValue);
582
587
  } else if (isRefEncoded(itemDescriptor)) {
583
- return rawValue === undefined
588
+ return rawValue === undefined || rawValue === null
584
589
  ? undefined
585
590
  : accessChildByKey(target, rawValue as string, key);
586
591
  }
@@ -605,11 +610,13 @@ const CoListProxyHandler: ProxyHandler<CoList> = {
605
610
  } else if ("encoded" in itemDescriptor) {
606
611
  rawValue = itemDescriptor.encoded.encode(value);
607
612
  } else if (isRefEncoded(itemDescriptor)) {
608
- if (value === null) {
613
+ if (value === undefined) {
609
614
  if (itemDescriptor.optional) {
610
615
  rawValue = null;
611
616
  } else {
612
- throw new Error(`Cannot set required reference ${key} to null`);
617
+ throw new Error(
618
+ `Cannot set required reference ${key} to undefined`,
619
+ );
613
620
  }
614
621
  } else if (value?.id) {
615
622
  rawValue = value.id;
@@ -4,16 +4,18 @@ import {
4
4
  CojsonInternalTypes,
5
5
  type JsonValue,
6
6
  RawAccountID,
7
+ RawCoID,
7
8
  type RawCoMap,
8
9
  cojsonInternals,
9
10
  } from "cojson";
10
- import { activeAccountContext } from "../implementation/activeAccountContext.js";
11
11
  import type {
12
12
  AnonymousJazzAgent,
13
+ AnyAccountSchema,
13
14
  CoValue,
14
15
  CoValueClass,
16
+ Group,
15
17
  ID,
16
- IfCo,
18
+ InstanceOfSchema,
17
19
  RefEncoded,
18
20
  RefIfCoValue,
19
21
  RefsToResolve,
@@ -22,15 +24,18 @@ import type {
22
24
  Schema,
23
25
  SubscribeListenerOptions,
24
26
  SubscribeRestArgs,
25
- co,
26
27
  } from "../internal.js";
27
28
  import {
29
+ Account,
28
30
  CoValueBase,
29
31
  ItemsSym,
30
32
  Ref,
33
+ RegisteredSchemas,
31
34
  SchemaInit,
32
35
  accessChildById,
33
36
  accessChildByKey,
37
+ activeAccountContext,
38
+ anySchemaToCoSchema,
34
39
  ensureCoValueLoaded,
35
40
  inspect,
36
41
  isRefEncoded,
@@ -41,15 +46,11 @@ import {
41
46
  subscribeToCoValueWithoutMe,
42
47
  subscribeToExistingCoValue,
43
48
  } from "../internal.js";
44
- import { RegisteredAccount } from "../types.js";
45
- import { type Account } from "./account.js";
46
- import { type Group } from "./group.js";
47
- import { RegisteredSchemas } from "./registeredSchemas.js";
48
49
 
49
50
  type CoMapEdit<V> = {
50
51
  value?: V;
51
52
  ref?: RefIfCoValue<V>;
52
- by?: RegisteredAccount;
53
+ by: Account | null;
53
54
  madeAt: Date;
54
55
  key?: string;
55
56
  };
@@ -68,16 +69,16 @@ export type Simplify<A> = {
68
69
  * @categoryDescription Declaration
69
70
  * Declare your own CoMap schemas by subclassing `CoMap` and assigning field schemas with `co`.
70
71
  *
71
- * Optional `co.ref(...)` fields must be marked with `{ optional: true }`.
72
+ * Optional `coField.ref(...)` fields must be marked with `{ optional: true }`.
72
73
  *
73
74
  * ```ts
74
- * import { co, CoMap } from "jazz-tools";
75
+ * import { coField, CoMap } from "jazz-tools";
75
76
  *
76
77
  * class Person extends CoMap {
77
- * name = co.string;
78
- * age = co.number;
79
- * pet = co.ref(Animal);
80
- * car = co.ref(Car, { optional: true });
78
+ * name = coField.string;
79
+ * age = coField.number;
80
+ * pet = coField.ref(Animal);
81
+ * car = coField.ref(Car, { optional: true });
81
82
  * }
82
83
  * ```
83
84
  *
@@ -119,7 +120,7 @@ export class CoMap extends CoValueBase implements CoValue {
119
120
  }
120
121
 
121
122
  /**
122
- * If property `prop` is a `co.ref(...)`, you can use `coMaps._refs.prop` to access
123
+ * If property `prop` is a `coField.ref(...)`, you can use `coMaps._refs.prop` to access
123
124
  * the `Ref` instead of the potentially loaded/null value.
124
125
  *
125
126
  * This allows you to always get the ID or load the value manually.
@@ -134,9 +135,19 @@ export class CoMap extends CoValueBase implements CoValue {
134
135
  *
135
136
  * @category Content
136
137
  **/
137
- get _refs(): {
138
- [Key in CoKeys<this>]: IfCo<this[Key], RefIfCoValue<this[Key]>>;
139
- } {
138
+ get _refs(): Simplify<
139
+ {
140
+ [Key in CoKeys<this> as NonNullable<this[Key]> extends CoValue
141
+ ? Key
142
+ : never]?: RefIfCoValue<this[Key]>;
143
+ } & {
144
+ [Key in CoKeys<this> as this[Key] extends undefined
145
+ ? never
146
+ : this[Key] extends CoValue
147
+ ? Key
148
+ : never]: RefIfCoValue<this[Key]>;
149
+ }
150
+ > {
140
151
  return makeRefs<CoKeys<this>>(
141
152
  this,
142
153
  (key) => this._raw.get(key as string) as unknown as ID<CoValue>,
@@ -186,12 +197,15 @@ export class CoMap extends CoValueBase implements CoValue {
186
197
  target,
187
198
  )
188
199
  : undefined,
189
- by:
190
- rawEdit.by &&
191
- accessChildById(target, rawEdit.by, {
192
- ref: RegisteredSchemas["Account"],
193
- optional: false,
194
- }),
200
+ get by() {
201
+ return (
202
+ rawEdit.by &&
203
+ accessChildById(target, rawEdit.by, {
204
+ ref: Account,
205
+ optional: false,
206
+ })
207
+ );
208
+ },
195
209
  madeAt: rawEdit.at,
196
210
  key,
197
211
  };
@@ -233,7 +247,7 @@ export class CoMap extends CoValueBase implements CoValue {
233
247
  },
234
248
  },
235
249
  ) as {
236
- [Key in CoKeys<this>]: IfCo<this[Key], LastAndAllCoMapEdits<this[Key]>>;
250
+ [Key in CoKeys<this>]?: LastAndAllCoMapEdits<this[Key]>;
237
251
  };
238
252
  }
239
253
 
@@ -413,10 +427,10 @@ export class CoMap extends CoValueBase implements CoValue {
413
427
  *
414
428
  * @example
415
429
  * ```ts
416
- * import { co, CoMap } from "jazz-tools";
430
+ * import { coField, CoMap } from "jazz-tools";
417
431
  *
418
432
  * class ColorToFruitMap extends CoMap.Record(
419
- * co.ref(Fruit)
433
+ * coField.ref(Fruit)
420
434
  * ) {}
421
435
  *
422
436
  * // assume we have map: ColorToFruitMap
@@ -426,7 +440,7 @@ export class CoMap extends CoValueBase implements CoValue {
426
440
  *
427
441
  * @category Declaration
428
442
  */
429
- static Record<Value>(value: IfCo<Value, Value>) {
443
+ static Record<Value>(value: Value) {
430
444
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
431
445
  class RecordLikeCoMap extends CoMap {
432
446
  [ItemsSym] = value;
@@ -527,7 +541,7 @@ export class CoMap extends CoValueBase implements CoValue {
527
541
  type: "comap" as const,
528
542
  ruleset: {
529
543
  type: "ownedByGroup" as const,
530
- group: ownerID,
544
+ group: ownerID as RawCoID,
531
545
  },
532
546
  meta: null,
533
547
  uniqueness: unique,
@@ -625,8 +639,8 @@ export type CoKeys<Map extends object> = Exclude<
625
639
  * Force required ref fields to be non nullable
626
640
  *
627
641
  * Considering that:
628
- * - Optional refs are typed as co<InstanceType<CoValueClass> | null | undefined>
629
- * - Required refs are typed as co<InstanceType<CoValueClass> | null>
642
+ * - Optional refs are typed as coField<InstanceType<CoValueClass> | null | undefined>
643
+ * - Required refs are typed as coField<InstanceType<CoValueClass> | null>
630
644
  *
631
645
  * This type works in two steps:
632
646
  * - Remove the null from both types
@@ -646,18 +660,18 @@ export type CoKeys<Map extends object> = Exclude<
646
660
  *
647
661
  * map.requiredRef // this value is still nullable
648
662
  */
649
- type ForceRequiredRef<V> = V extends co<InstanceType<CoValueClass> | null>
663
+ type ForceRequiredRef<V> = V extends InstanceType<CoValueClass> | null
650
664
  ? NonNullable<V>
651
- : V extends co<InstanceType<CoValueClass> | undefined>
665
+ : V extends InstanceType<CoValueClass> | undefined
652
666
  ? V | null
653
667
  : V;
654
668
 
655
669
  export type CoMapInit<Map extends object> = {
656
670
  [Key in CoKeys<Map> as undefined extends Map[Key]
657
671
  ? never
658
- : IfCo<Map[Key], Key>]: ForceRequiredRef<Map[Key]>;
672
+ : Key]: ForceRequiredRef<Map[Key]>;
659
673
  } & {
660
- [Key in CoKeys<Map> as IfCo<Map[Key], Key>]?: ForceRequiredRef<Map[Key]>;
674
+ [Key in CoKeys<Map>]?: ForceRequiredRef<Map[Key]>;
661
675
  };
662
676
 
663
677
  // TODO: cache handlers per descriptor for performance?
@@ -685,7 +699,7 @@ const CoMapProxyHandler: ProxyHandler<CoMap> = {
685
699
  } else if ("encoded" in descriptor) {
686
700
  return raw === undefined ? undefined : descriptor.encoded.decode(raw);
687
701
  } else if (isRefEncoded(descriptor)) {
688
- return raw === undefined
702
+ return raw === undefined || raw === null
689
703
  ? undefined
690
704
  : accessChildByKey(target, raw as string, key);
691
705
  }
@@ -713,11 +727,13 @@ const CoMapProxyHandler: ProxyHandler<CoMap> = {
713
727
  } else if ("encoded" in descriptor) {
714
728
  target._raw.set(key, descriptor.encoded.encode(value));
715
729
  } else if (isRefEncoded(descriptor)) {
716
- if (value === null) {
730
+ if (value === undefined) {
717
731
  if (descriptor.optional) {
718
732
  target._raw.set(key, null);
719
733
  } else {
720
- throw new Error(`Cannot set required reference ${key} to null`);
734
+ throw new Error(
735
+ `Cannot set required reference ${key} to undefined`,
736
+ );
721
737
  }
722
738
  } else if (value?.id) {
723
739
  target._raw.set(key, value.id);