jazz-tools 0.16.2 → 0.16.4

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 (76) hide show
  1. package/.turbo/turbo-build.log +38 -38
  2. package/CHANGELOG.md +22 -0
  3. package/dist/{chunk-CL3ROOZM.js → chunk-74LZG2M3.js} +112 -36
  4. package/dist/chunk-74LZG2M3.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react-core/hooks.d.ts +150 -2
  7. package/dist/react-core/hooks.d.ts.map +1 -1
  8. package/dist/react-core/index.js.map +1 -1
  9. package/dist/testing.js +1 -1
  10. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  11. package/dist/tools/coValues/coList.d.ts.map +1 -1
  12. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  13. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  14. package/dist/tools/coValues/schemaUnion.d.ts +7 -2
  15. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  16. package/dist/tools/exports.d.ts +1 -1
  17. package/dist/tools/exports.d.ts.map +1 -1
  18. package/dist/tools/implementation/schema.d.ts +12 -2
  19. package/dist/tools/implementation/schema.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -4
  21. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  22. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +2 -4
  23. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +23 -7
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  26. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts +20 -6
  27. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +4 -4
  29. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +3 -3
  31. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +3 -25
  33. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  34. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +3 -25
  35. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts +24 -0
  37. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts.map +1 -0
  38. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  39. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +4 -5
  41. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  42. package/dist/tools/internal.d.ts +1 -0
  43. package/dist/tools/internal.d.ts.map +1 -1
  44. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  45. package/package.json +4 -4
  46. package/src/react-core/hooks.ts +143 -0
  47. package/src/tools/coValues/coFeed.ts +12 -4
  48. package/src/tools/coValues/coList.ts +41 -19
  49. package/src/tools/coValues/coMap.ts +12 -2
  50. package/src/tools/coValues/inbox.ts +1 -1
  51. package/src/tools/coValues/interfaces.ts +2 -3
  52. package/src/tools/coValues/schemaUnion.ts +28 -3
  53. package/src/tools/exports.ts +0 -1
  54. package/src/tools/implementation/schema.ts +28 -2
  55. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -7
  56. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +2 -7
  57. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +61 -12
  58. package/src/tools/implementation/zodSchema/typeConverters/CoFieldInit.ts +74 -9
  59. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +9 -4
  60. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +3 -3
  61. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +3 -114
  62. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +5 -129
  63. package/src/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.ts +79 -0
  64. package/src/tools/implementation/zodSchema/unionUtils.ts +6 -9
  65. package/src/tools/implementation/zodSchema/zodSchema.ts +4 -14
  66. package/src/tools/internal.ts +1 -0
  67. package/src/tools/subscribe/SubscriptionScope.ts +4 -2
  68. package/src/tools/subscribe/utils.ts +2 -2
  69. package/src/tools/tests/coFeed.test.ts +40 -0
  70. package/src/tools/tests/coList.test.ts +42 -0
  71. package/src/tools/tests/coMap.test-d.ts +184 -1
  72. package/src/tools/tests/coMap.test.ts +182 -58
  73. package/src/tools/tests/groupsAndAccounts.test.ts +54 -0
  74. package/src/tools/tests/load.test.ts +19 -0
  75. package/src/tools/tests/zod.test.ts +16 -0
  76. package/dist/chunk-CL3ROOZM.js.map +0 -1
@@ -141,13 +141,102 @@ function useCoValueSubscription<
141
141
  return subscription.subscription;
142
142
  }
143
143
 
144
+ /**
145
+ * React hook for subscribing to CoValues and handling loading states.
146
+ *
147
+ * This hook provides a convenient way to subscribe to CoValues and automatically
148
+ * handles the subscription lifecycle (subscribe on mount, unsubscribe on unmount).
149
+ * It also supports deep loading of nested CoValues through resolve queries.
150
+ *
151
+ * @returns The loaded CoValue, or `undefined` if loading, or `null` if not found/not accessible
152
+ *
153
+ * @example
154
+ * ```tsx
155
+ * // Deep loading with resolve queries
156
+ * const Project = co.map({
157
+ * name: z.string(),
158
+ * tasks: co.list(Task),
159
+ * owner: TeamMember,
160
+ * });
161
+ *
162
+ * function ProjectView({ projectId }: { projectId: string }) {
163
+ * const project = useCoState(Project, projectId, {
164
+ * resolve: {
165
+ * tasks: { $each: true },
166
+ * owner: true,
167
+ * },
168
+ * });
169
+ *
170
+ * if (!project) {
171
+ * return project === null
172
+ * ? "Project not found or not accessible"
173
+ * : "Loading project...";
174
+ * }
175
+ *
176
+ * return (
177
+ * <div>
178
+ * <h1>{project.name}</h1>
179
+ * <p>Owner: {project.owner.name}</p>
180
+ * <ul>
181
+ * {project.tasks.map((task) => (
182
+ * <li key={task.id}>{task.title}</li>
183
+ * ))}
184
+ * </ul>
185
+ * </div>
186
+ * );
187
+ * }
188
+ * ```
189
+ *
190
+ * @example
191
+ * ```tsx
192
+ * // Using with optional references and error handling
193
+ * const Task = co.map({
194
+ * title: z.string(),
195
+ * assignee: co.optional(TeamMember),
196
+ * subtasks: co.list(Task),
197
+ * });
198
+ *
199
+ * function TaskDetail({ taskId }: { taskId: string }) {
200
+ * const task = useCoState(Task, taskId, {
201
+ * resolve: {
202
+ * assignee: true,
203
+ * subtasks: { $each: { $onError: null } },
204
+ * },
205
+ * });
206
+ *
207
+ * if (!task) {
208
+ * return task === null
209
+ * ? "Task not found or not accessible"
210
+ * : "Loading task...";
211
+ * }
212
+ *
213
+ * return (
214
+ * <div>
215
+ * <h2>{task.title}</h2>
216
+ * {task.assignee && <p>Assigned to: {task.assignee.name}</p>}
217
+ * <ul>
218
+ * {task.subtasks.map((subtask, index) => (
219
+ * subtask ? <li key={subtask.id}>{subtask.title}</li> : <li key={index}>Inaccessible subtask</li>
220
+ * ))}
221
+ * </ul>
222
+ * </div>
223
+ * );
224
+ * }
225
+ * ```
226
+ *
227
+ * For more examples, see the [subscription and deep loading](https://jazz.tools/docs/react/using-covalues/subscription-and-loading) documentation.
228
+ */
144
229
  export function useCoState<
145
230
  S extends CoValueClassOrSchema,
146
231
  const R extends ResolveQuery<S> = true,
147
232
  >(
233
+ /** The CoValue schema or class constructor */
148
234
  Schema: S,
235
+ /** The ID of the CoValue to subscribe to. If `undefined`, returns `null` */
149
236
  id: string | undefined,
237
+ /** Optional configuration for the subscription */
150
238
  options?: {
239
+ /** Resolve query to specify which nested CoValues to load */
151
240
  resolve?: ResolveQueryStrict<S, R>;
152
241
  },
153
242
  ): Loaded<S, R> | undefined | null {
@@ -229,12 +318,66 @@ function useAccountSubscription<
229
318
  return subscription.subscription;
230
319
  }
231
320
 
321
+ /**
322
+ * React hook for accessing the current user's account and authentication state.
323
+ *
324
+ * This hook provides access to the current user's account profile and root data,
325
+ * along with authentication utilities. It automatically handles subscription to
326
+ * the user's account data and provides a logout function.
327
+ *
328
+ * @returns An object containing:
329
+ * - `me`: The loaded account data, or `undefined` if loading, or `null` if not authenticated
330
+ * - `agent`: The current agent (anonymous or authenticated user). Can be used as `loadAs` parameter for load and subscribe methods.
331
+ * - `logOut`: Function to log out the current user
332
+
333
+ * @example
334
+ * ```tsx
335
+ * // Deep loading with resolve queries
336
+ * function ProjectListWithDetails() {
337
+ * const { me } = useAccount(MyAppAccount, {
338
+ * resolve: {
339
+ * profile: true,
340
+ * root: {
341
+ * myProjects: {
342
+ * $each: {
343
+ * tasks: true,
344
+ * },
345
+ * },
346
+ * },
347
+ * },
348
+ * });
349
+ *
350
+ * if (!me) {
351
+ * return me === null
352
+ * ? <div>Failed to load your projects</div>
353
+ * : <div>Loading...</div>;
354
+ * }
355
+ *
356
+ * return (
357
+ * <div>
358
+ * <h1>{me.profile.name}'s projects</h1>
359
+ * <ul>
360
+ * {me.root.myProjects.map((project) => (
361
+ * <li key={project.id}>
362
+ * {project.name} ({project.tasks.length} tasks)
363
+ * </li>
364
+ * ))}
365
+ * </ul>
366
+ * </div>
367
+ * );
368
+ * }
369
+ * ```
370
+ *
371
+ */
232
372
  export function useAccount<
233
373
  A extends AccountClass<Account> | AnyAccountSchema,
234
374
  R extends ResolveQuery<A> = true,
235
375
  >(
376
+ /** The account schema to use. Defaults to the base Account schema */
236
377
  AccountSchema: A = Account as unknown as A,
378
+ /** Optional configuration for the subscription */
237
379
  options?: {
380
+ /** Resolve query to specify which nested CoValues to load from the account */
238
381
  resolve?: ResolveQueryStrict<A, R>;
239
382
  },
240
383
  ): {
@@ -34,6 +34,7 @@ import {
34
34
  coField,
35
35
  ensureCoValueLoaded,
36
36
  inspect,
37
+ instantiateRefEncodedWithInit,
37
38
  isRefEncoded,
38
39
  loadCoValueWithoutMe,
39
40
  parseCoValueCreateOptions,
@@ -278,7 +279,16 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
278
279
  } else if ("encoded" in itemDescriptor) {
279
280
  this._raw.push(itemDescriptor.encoded.encode(item));
280
281
  } else if (isRefEncoded(itemDescriptor)) {
281
- this._raw.push((item as unknown as CoValue).id);
282
+ let refId = (item as unknown as CoValue).id;
283
+ if (!refId) {
284
+ const coValue = instantiateRefEncodedWithInit(
285
+ itemDescriptor,
286
+ item,
287
+ this._owner,
288
+ );
289
+ refId = coValue.id;
290
+ }
291
+ this._raw.push(refId);
282
292
  }
283
293
  }
284
294
 
@@ -418,9 +428,7 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
418
428
  *
419
429
  * @category Subscription & Loading
420
430
  */
421
- waitForSync(options?: {
422
- timeout?: number;
423
- }) {
431
+ waitForSync(options?: { timeout?: number }) {
424
432
  return this._raw.core.waitForSync(options);
425
433
  }
426
434
  }
@@ -29,6 +29,7 @@ import {
29
29
  coValuesCache,
30
30
  ensureCoValueLoaded,
31
31
  inspect,
32
+ instantiateRefEncodedWithInit,
32
33
  isRefEncoded,
33
34
  loadCoValueWithoutMe,
34
35
  makeRefs,
@@ -240,7 +241,7 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
240
241
  const { owner } = parseCoValueCreateOptions(options);
241
242
  const instance = new this({ init: items, owner });
242
243
  const raw = owner._raw.createList(
243
- toRawItems(items, instance._schema[ItemsSym]),
244
+ toRawItems(items, instance._schema[ItemsSym], owner),
244
245
  );
245
246
 
246
247
  Object.defineProperties(instance, {
@@ -256,7 +257,7 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
256
257
 
257
258
  push(...items: Item[]): number {
258
259
  this._raw.appendItems(
259
- toRawItems(items, this._schema[ItemsSym]),
260
+ toRawItems(items, this._schema[ItemsSym], this._owner),
260
261
  undefined,
261
262
  "private",
262
263
  );
@@ -265,7 +266,11 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
265
266
  }
266
267
 
267
268
  unshift(...items: Item[]): number {
268
- for (const item of toRawItems(items as Item[], this._schema[ItemsSym])) {
269
+ for (const item of toRawItems(
270
+ items as Item[],
271
+ this._schema[ItemsSym],
272
+ this._owner,
273
+ )) {
269
274
  this._raw.prepend(item);
270
275
  }
271
276
 
@@ -306,7 +311,11 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
306
311
  this._raw.delete(idxToDelete);
307
312
  }
308
313
 
309
- const rawItems = toRawItems(items as Item[], this._schema[ItemsSym]);
314
+ const rawItems = toRawItems(
315
+ items as Item[],
316
+ this._schema[ItemsSym],
317
+ this._owner,
318
+ );
310
319
 
311
320
  // If there are no items to insert, return the deleted items
312
321
  if (rawItems.length === 0) {
@@ -551,23 +560,36 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
551
560
  * Convert an array of items to a raw array of items.
552
561
  * @param items - The array of items to convert.
553
562
  * @param itemDescriptor - The descriptor of the items.
563
+ * @param owner - The owner of the CoList.
554
564
  * @returns The raw array of items.
555
565
  */
556
- function toRawItems<Item>(items: Item[], itemDescriptor: Schema) {
557
- const rawItems =
558
- itemDescriptor === "json"
559
- ? (items as JsonValue[])
560
- : "encoded" in itemDescriptor
561
- ? items?.map((e) => itemDescriptor.encoded.encode(e))
562
- : isRefEncoded(itemDescriptor)
563
- ? items?.map((v) => {
564
- if (!v) return null;
565
-
566
- return (v as unknown as CoValue).id;
567
- })
568
- : (() => {
569
- throw new Error("Invalid element descriptor");
570
- })();
566
+ function toRawItems<Item>(
567
+ items: Item[],
568
+ itemDescriptor: Schema,
569
+ owner: Account | Group,
570
+ ) {
571
+ let rawItems: JsonValue[] = [];
572
+ if (itemDescriptor === "json") {
573
+ rawItems = items as JsonValue[];
574
+ } else if ("encoded" in itemDescriptor) {
575
+ rawItems = items?.map((e) => itemDescriptor.encoded.encode(e));
576
+ } else if (isRefEncoded(itemDescriptor)) {
577
+ rawItems = items?.map((value) => {
578
+ if (value == null) return null;
579
+ let refId = (value as unknown as CoValue).id;
580
+ if (!refId) {
581
+ const coValue = instantiateRefEncodedWithInit(
582
+ itemDescriptor,
583
+ value,
584
+ owner,
585
+ );
586
+ refId = coValue.id;
587
+ }
588
+ return refId;
589
+ });
590
+ } else {
591
+ throw new Error("Invalid element descriptor");
592
+ }
571
593
  return rawItems;
572
594
  }
573
595
 
@@ -37,6 +37,7 @@ import {
37
37
  activeAccountContext,
38
38
  ensureCoValueLoaded,
39
39
  inspect,
40
+ instantiateRefEncodedWithInit,
40
41
  isRefEncoded,
41
42
  loadCoValueWithoutMe,
42
43
  makeRefs,
@@ -424,8 +425,17 @@ export class CoMap extends CoValueBase implements CoValue {
424
425
  if (descriptor === "json") {
425
426
  rawInit[key] = initValue as JsonValue;
426
427
  } else if (isRefEncoded(descriptor)) {
427
- if (initValue) {
428
- rawInit[key] = (initValue as unknown as CoValue).id;
428
+ if (initValue != null) {
429
+ let refId = (initValue as unknown as CoValue).id;
430
+ if (!refId) {
431
+ const coValue = instantiateRefEncodedWithInit(
432
+ descriptor,
433
+ initValue,
434
+ owner,
435
+ );
436
+ refId = coValue.id;
437
+ }
438
+ rawInit[key] = refId;
429
439
  }
430
440
  } else if ("encoded" in descriptor) {
431
441
  rawInit[key] = descriptor.encoded.encode(
@@ -145,7 +145,7 @@ export class Inbox {
145
145
 
146
146
  for (const [sessionID, items] of Object.entries(stream.items) as [
147
147
  SessionID,
148
- CoStreamItem<CoID<InboxMessage<InstanceOfSchema<M>, O>>>[],
148
+ CoStreamItem<CoID<InboxMessage<NonNullable<InstanceOfSchema<M>>, O>>>[],
149
149
  ][]) {
150
150
  const accountID = getAccountIDfromSessionID(sessionID);
151
151
 
@@ -2,7 +2,6 @@ import {
2
2
  type CoValueUniqueness,
3
3
  type CojsonInternalTypes,
4
4
  type RawCoValue,
5
- emptyKnownState,
6
5
  } from "cojson";
7
6
  import { AvailableCoValueCore } from "cojson/dist/coValueCore/coValueCore.js";
8
7
  import {
@@ -546,9 +545,9 @@ function loadContentPiecesFromCoValue(
546
545
  }
547
546
  }
548
547
 
549
- const pieces = core.verified.newContentSince(emptyKnownState(core.id));
548
+ const pieces = core.verified.newContentSince(undefined) ?? [];
550
549
 
551
- for (const piece of pieces ?? []) {
550
+ for (const piece of pieces) {
552
551
  contentPieces.push(piece);
553
552
  }
554
553
  }
@@ -1,12 +1,16 @@
1
+ import { JsonValue, RawCoMap } from "cojson";
1
2
  import {
2
3
  Account,
3
4
  AnonymousJazzAgent,
5
+ CoMapInit,
4
6
  CoValue,
5
7
  CoValueBase,
6
8
  CoValueClass,
7
9
  CoValueFromRaw,
10
+ Group,
8
11
  ID,
9
12
  Resolved,
13
+ Simplify,
10
14
  SubscribeListenerOptions,
11
15
  SubscribeRestArgs,
12
16
  loadCoValueWithoutMe,
@@ -20,6 +24,10 @@ import {
20
24
  export type SchemaUnionConcreteSubclass<V extends CoValue> =
21
25
  typeof SchemaUnion & CoValueClass<V>;
22
26
 
27
+ export type SchemaUnionDiscriminator<V extends CoValue> = (discriminable: {
28
+ get(key: string): JsonValue | undefined;
29
+ }) => CoValueClass<V> & CoValueFromRaw<V>;
30
+
23
31
  /**
24
32
  * SchemaUnion allows you to create union types of CoValues that can be discriminated at runtime.
25
33
  *
@@ -89,28 +97,45 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
89
97
  * @category Declaration
90
98
  **/
91
99
  static Of<V extends CoValue>(
92
- discriminator: (raw: V["_raw"]) => CoValueClass<V> & CoValueFromRaw<V>,
100
+ discriminator: SchemaUnionDiscriminator<V>,
93
101
  ): SchemaUnionConcreteSubclass<V> {
94
102
  return class SchemaUnionClass extends SchemaUnion {
103
+ static override create<V extends CoValue>(
104
+ this: CoValueClass<V>,
105
+ init: Simplify<CoMapInit<V>>,
106
+ owner: Account | Group,
107
+ ): V {
108
+ const ResolvedClass = discriminator(new Map(Object.entries(init)));
109
+ // @ts-expect-error - create is a static method in the CoMap class
110
+ return ResolvedClass.create(init, owner);
111
+ }
112
+
95
113
  static override fromRaw<T extends CoValue>(
96
114
  this: CoValueClass<T> & CoValueFromRaw<T>,
97
115
  raw: T["_raw"],
98
116
  ): T {
99
117
  const ResolvedClass = discriminator(
100
- raw as V["_raw"],
118
+ raw as RawCoMap,
101
119
  ) as unknown as CoValueClass<T> & CoValueFromRaw<T>;
102
120
  return ResolvedClass.fromRaw(raw);
103
121
  }
104
122
  } as unknown as SchemaUnionConcreteSubclass<V>;
105
123
  }
106
124
 
125
+ static create<V extends CoValue>(
126
+ this: CoValueClass<V>,
127
+ init: Simplify<CoMapInit<V>>,
128
+ owner: Account | Group,
129
+ ): V {
130
+ throw new Error("Not implemented");
131
+ }
132
+
107
133
  /**
108
134
  * Create an instance from raw data. This is called internally and should not be used directly.
109
135
  * Use {@link SchemaUnion.Of} to create a union type instead.
110
136
  *
111
137
  * @internal
112
138
  */
113
- // @ts-ignore
114
139
  static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: V["_raw"]): V {
115
140
  throw new Error("Not implemented");
116
141
  }
@@ -107,7 +107,6 @@ export {
107
107
  type CoreAccountSchema as AnyAccountSchema,
108
108
  type ResolveQuery,
109
109
  type ResolveQueryStrict,
110
- type InitFor,
111
110
  } from "./internal.js";
112
111
 
113
112
  export {
@@ -1,11 +1,12 @@
1
1
  import type { JsonValue, RawCoValue } from "cojson";
2
2
  import { CojsonInternalTypes } from "cojson";
3
3
  import {
4
+ Account,
4
5
  type CoValue,
5
6
  type CoValueClass,
6
7
  CoValueFromRaw,
8
+ Group,
7
9
  ItemsSym,
8
- JazzToolsSymbol,
9
10
  SchemaInit,
10
11
  isCoValueClass,
11
12
  } from "../internal.js";
@@ -140,7 +141,7 @@ export function isRefEncoded<V extends CoValue>(
140
141
  );
141
142
  }
142
143
 
143
- export function instantiateRefEncoded<V extends CoValue>(
144
+ export function instantiateRefEncodedFromRaw<V extends CoValue>(
144
145
  schema: RefEncoded<V>,
145
146
  raw: RawCoValue,
146
147
  ): V {
@@ -151,6 +152,31 @@ export function instantiateRefEncoded<V extends CoValue>(
151
152
  ).fromRaw(raw);
152
153
  }
153
154
 
155
+ /**
156
+ * Creates a new CoValue of the given ref type, using the provided init values.
157
+ *
158
+ * @param schema - The schema of the CoValue to create.
159
+ * @param init - The init values to use to create the CoValue.
160
+ * @param parentOwner - The owner of the referencing CoValue. Will be used
161
+ * as the parent group of the created CoValue's group
162
+ * @returns The created CoValue.
163
+ */
164
+ export function instantiateRefEncodedWithInit<V extends CoValue>(
165
+ schema: RefEncoded<V>,
166
+ init: any,
167
+ parentOwner: Account | Group,
168
+ ): V {
169
+ if (!isCoValueClass<V>(schema.ref)) {
170
+ throw Error(
171
+ `Cannot automatically create CoValue from value: ${JSON.stringify(init)}. Use the CoValue schema's create() method instead.`,
172
+ );
173
+ }
174
+ const owner = Group.create();
175
+ owner.addMember(parentOwner.castAs(Group));
176
+ // @ts-expect-error - create is a static method in all CoValue classes
177
+ return schema.ref.create(init, owner);
178
+ }
179
+
154
180
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
181
  export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
156
182
 
@@ -6,21 +6,16 @@ import {
6
6
  RefsToResolve,
7
7
  RefsToResolveStrict,
8
8
  Resolved,
9
- Simplify,
10
9
  SubscribeListenerOptions,
11
10
  coOptionalDefiner,
12
11
  } from "../../../internal.js";
13
12
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
14
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
13
+ import { CoFeedInit } from "../typeConverters/CoFieldInit.js";
15
14
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
16
15
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
17
16
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
17
  import { CoreCoValueSchema } from "./CoValueSchema.js";
19
18
 
20
- type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
21
- Array<CoFieldInit<T>>
22
- >;
23
-
24
19
  export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
25
20
  implements CoreCoFeedSchema<T>
26
21
  {
@@ -36,7 +31,7 @@ export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
36
31
  init: CoFeedInit<T>,
37
32
  options?: { owner: Account | Group } | Account | Group,
38
33
  ): CoFeedInstance<T> {
39
- return this.coValueClass.create(init, options) as CoFeedInstance<T>;
34
+ return this.coValueClass.create(init as any, options) as CoFeedInstance<T>;
40
35
  }
41
36
 
42
37
  load<const R extends RefsToResolve<CoFeedInstanceCoValuesNullable<T>> = true>(
@@ -5,22 +5,17 @@ import {
5
5
  RefsToResolve,
6
6
  RefsToResolveStrict,
7
7
  Resolved,
8
- Simplify,
9
8
  SubscribeListenerOptions,
10
9
  coOptionalDefiner,
11
10
  } from "../../../internal.js";
12
11
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
13
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
12
+ import { CoListInit } from "../typeConverters/CoFieldInit.js";
14
13
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
15
14
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
15
  import { AnyZodOrCoValueSchema } from "../zodSchema.js";
17
16
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
17
  import { CoreCoValueSchema } from "./CoValueSchema.js";
19
18
 
20
- type CoListInit<T extends AnyZodOrCoValueSchema> = Simplify<
21
- Array<CoFieldInit<T>>
22
- >;
23
-
24
19
  export class CoListSchema<T extends AnyZodOrCoValueSchema>
25
20
  implements CoreCoListSchema<T>
26
21
  {
@@ -36,7 +31,7 @@ export class CoListSchema<T extends AnyZodOrCoValueSchema>
36
31
  items: CoListInit<T>,
37
32
  options?: { owner: Account | Group } | Account | Group,
38
33
  ): CoListInstance<T> {
39
- return this.coValueClass.create(items, options) as CoListInstance<T>;
34
+ return this.coValueClass.create(items as any, options) as CoListInstance<T>;
40
35
  }
41
36
 
42
37
  load<const R extends RefsToResolve<CoListInstanceCoValuesNullable<T>> = true>(