jazz-tools 0.14.19 → 0.14.21

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 (60) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +19 -0
  3. package/dist/{chunk-AA3SCYKI.js → chunk-DAOCWHIV.js} +248 -125
  4. package/dist/chunk-DAOCWHIV.js.map +1 -0
  5. package/dist/coValues/CoValueBase.d.ts +14 -3
  6. package/dist/coValues/CoValueBase.d.ts.map +1 -1
  7. package/dist/coValues/coMap.d.ts +27 -1
  8. package/dist/coValues/coMap.d.ts.map +1 -1
  9. package/dist/coValues/group.d.ts +8 -0
  10. package/dist/coValues/group.d.ts.map +1 -1
  11. package/dist/coValues/interfaces.d.ts +5 -0
  12. package/dist/coValues/interfaces.d.ts.map +1 -1
  13. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  14. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  15. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  16. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +1 -0
  17. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  18. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +1 -0
  19. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  20. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +2 -0
  21. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  22. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -1
  23. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  24. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +1 -0
  25. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  26. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +1 -0
  27. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  28. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +1 -0
  29. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  30. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
  31. package/dist/implementation/zodSchema/zodSchema.d.ts +1 -1
  32. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  33. package/dist/index.js +1 -1
  34. package/dist/lib/migration.d.ts +3 -0
  35. package/dist/lib/migration.d.ts.map +1 -0
  36. package/dist/subscribe/SubscriptionScope.d.ts +3 -1
  37. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  38. package/dist/testing.js +1 -1
  39. package/package.json +2 -2
  40. package/src/coValues/CoValueBase.ts +31 -7
  41. package/src/coValues/coMap.ts +14 -1
  42. package/src/coValues/group.ts +12 -0
  43. package/src/coValues/interfaces.ts +5 -0
  44. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +12 -15
  45. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +3 -1
  46. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -0
  47. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +2 -0
  48. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +11 -0
  49. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +2 -1
  50. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +1 -0
  51. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +1 -0
  52. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +1 -0
  53. package/src/implementation/zodSchema/zodCo.ts +214 -246
  54. package/src/implementation/zodSchema/zodSchema.ts +1 -1
  55. package/src/lib/migration.ts +23 -0
  56. package/src/subscribe/SubscriptionScope.ts +48 -3
  57. package/src/tests/coMap.test.ts +280 -1
  58. package/src/tests/groupsAndAccounts.test.ts +12 -0
  59. package/src/tests/schemaUnion.test.ts +5 -5
  60. package/dist/chunk-AA3SCYKI.js.map +0 -1
@@ -1,11 +1,7 @@
1
- import { CoValueUniqueness } from "cojson";
2
- import { ZodObject } from "zod/v4";
3
1
  import {
4
- Account,
2
+ type Account,
5
3
  AccountCreationProps,
6
- AccountInstance,
7
4
  AccountSchema,
8
- AnonymousJazzAgent,
9
5
  AnyCoMapSchema,
10
6
  CoFeed,
11
7
  CoFeedSchema,
@@ -18,7 +14,6 @@ import {
18
14
  DefaultProfileShape,
19
15
  FileStream,
20
16
  FileStreamSchema,
21
- Group,
22
17
  ImageDefinition,
23
18
  PlainTextSchema,
24
19
  Simplify,
@@ -55,10 +50,19 @@ function enrichCoMapSchema<Shape extends z.core.$ZodLooseShape>(
55
50
  withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
56
51
  return Object.assign(schema, helpers(schema));
57
52
  },
53
+ withMigration: (migration: (value: any) => undefined) => {
54
+ coSchema.prototype.migrate = migration;
55
+
56
+ return enrichedSchema;
57
+ },
58
+ getCoSchema: () => {
59
+ return coSchema;
60
+ },
58
61
  }) as unknown as CoMapSchema<Shape>;
59
62
 
60
63
  // Needs to be derived from the enriched schema
61
64
  const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
65
+
62
66
  return enrichedSchema;
63
67
  }
64
68
 
@@ -72,6 +76,66 @@ export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
72
76
  return enrichCoMapSchema(objectSchema);
73
77
  };
74
78
 
79
+ function enrichAccountSchema<
80
+ Shape extends {
81
+ profile: AnyCoMapSchema<{
82
+ name: z.core.$ZodString<string>;
83
+ inbox?: z.core.$ZodOptional<z.core.$ZodString>;
84
+ inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
85
+ }>;
86
+ root: AnyCoMapSchema;
87
+ },
88
+ >(schema: z.ZodObject<Shape, z.core.$strip>) {
89
+ const enrichedSchema = Object.assign(schema, {
90
+ collaborative: true,
91
+ builtin: "Account",
92
+ create: (...args: any[]) => {
93
+ return coSchema.create(...args);
94
+ },
95
+ createAs: (...args: any[]) => {
96
+ return coSchema.createAs(...args);
97
+ },
98
+ getMe: (...args: any[]) => {
99
+ return coSchema.getMe(...args);
100
+ },
101
+ load: (...args: any[]) => {
102
+ return coSchema.load(...args);
103
+ },
104
+ subscribe: (...args: any[]) => {
105
+ return coSchema.subscribe(...args);
106
+ },
107
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
108
+ return Object.assign(schema, helpers(schema));
109
+ },
110
+ fromRaw: (...args: any[]) => {
111
+ return coSchema.fromRaw(...args);
112
+ },
113
+ withMigration: (
114
+ migration: (
115
+ value: any,
116
+ creationProps?: AccountCreationProps,
117
+ ) => void | Promise<void>,
118
+ ) => {
119
+ (coSchema.prototype as Account).migrate = async function (
120
+ this,
121
+ creationProps,
122
+ ) {
123
+ await migration(this, creationProps);
124
+ };
125
+
126
+ return enrichedSchema;
127
+ },
128
+ getCoSchema: () => {
129
+ return coSchema;
130
+ },
131
+ }) as unknown as AccountSchema<Shape>;
132
+
133
+ // Needs to be derived from the enriched schema
134
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
135
+
136
+ return enrichedSchema;
137
+ }
138
+
75
139
  export const coAccountDefiner = <
76
140
  Shape extends {
77
141
  profile: AnyCoMapSchema<{
@@ -95,76 +159,7 @@ export const coAccountDefiner = <
95
159
  collaborative: true,
96
160
  });
97
161
 
98
- type CleanedType = Pick<
99
- typeof objectSchema,
100
- "_zod" | "def" | "~standard" | "catchall"
101
- >;
102
-
103
- const accountSchema = objectSchema as unknown as CleanedType & {
104
- collaborative: true;
105
- builtin: "Account";
106
- migration?: (
107
- account: AccountInstance<Shape>,
108
- creationProps?: AccountCreationProps,
109
- ) => void;
110
-
111
- create: AccountSchema<Shape>["create"];
112
- createAs: AccountSchema<Shape>["createAs"];
113
- getMe: AccountSchema<Shape>["getMe"];
114
- load: AccountSchema<Shape>["load"];
115
- subscribe: AccountSchema<Shape>["subscribe"];
116
- /** @deprecated Define your helper methods separately, in standalone functions. */
117
- withHelpers: AccountSchema<Shape>["withHelpers"];
118
- withMigration: AccountSchema<Shape>["withMigration"];
119
- };
120
-
121
- accountSchema.collaborative = true;
122
- accountSchema.builtin = "Account";
123
-
124
- accountSchema.create = function (this: AccountSchema<Shape>, ...args: any[]) {
125
- return (zodSchemaToCoSchema(this) as any).create(...args);
126
- } as AccountSchema<Shape>["create"];
127
-
128
- accountSchema.createAs = function (
129
- this: AccountSchema<Shape>,
130
- ...args: any[]
131
- ) {
132
- return (zodSchemaToCoSchema(this) as any).createAs(...args);
133
- } as AccountSchema<Shape>["createAs"];
134
-
135
- accountSchema.getMe = function (this: AccountSchema<Shape>) {
136
- return (zodSchemaToCoSchema(this) as any).getMe();
137
- } as AccountSchema<Shape>["getMe"];
138
-
139
- accountSchema.load = function (this: AccountSchema<Shape>, ...args: any[]) {
140
- return (zodSchemaToCoSchema(this) as any).load(...args);
141
- } as AccountSchema<Shape>["load"];
142
-
143
- accountSchema.subscribe = function (
144
- this: AccountSchema<Shape>,
145
- ...args: any[]
146
- ) {
147
- return (zodSchemaToCoSchema(this) as any).subscribe(...args);
148
- } as AccountSchema<Shape>["subscribe"];
149
-
150
- accountSchema.withHelpers = function (
151
- this: CoMapSchema<Shape>,
152
- helpers: object,
153
- ) {
154
- return { ...this, ...helpers };
155
- } as CoMapSchema<Shape>["withHelpers"];
156
-
157
- accountSchema.withMigration = function (
158
- this: AccountSchema<Shape>,
159
- migration: (
160
- account: AccountInstance<Shape>,
161
- creationProps?: AccountCreationProps,
162
- ) => void,
163
- ) {
164
- return Object.assign(this, { migration });
165
- } as AccountSchema<Shape>["withMigration"];
166
-
167
- return accountSchema as unknown as AccountSchema<Shape>;
162
+ return enrichAccountSchema(objectSchema) as unknown as AccountSchema<Shape>;
168
163
  };
169
164
 
170
165
  export const coRecordDefiner = <
@@ -180,6 +175,32 @@ export const coRecordDefiner = <
180
175
  >;
181
176
  };
182
177
 
178
+ function enrichCoListSchema<T extends z.core.$ZodType>(schema: z.ZodArray<T>) {
179
+ const enrichedSchema = Object.assign(schema, {
180
+ collaborative: true,
181
+ create: (...args: any[]) => {
182
+ return coSchema.create(...args);
183
+ },
184
+ load: (...args: any[]) => {
185
+ return coSchema.load(...args);
186
+ },
187
+ subscribe: (...args: any[]) => {
188
+ return coSchema.subscribe(...args);
189
+ },
190
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
191
+ return Object.assign(schema, helpers(schema));
192
+ },
193
+ getCoSchema: () => {
194
+ return coSchema;
195
+ },
196
+ }) as unknown as CoListSchema<T>;
197
+
198
+ // Needs to be derived from the enriched schema
199
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
200
+
201
+ return enrichedSchema;
202
+ }
203
+
183
204
  export const coListDefiner = <T extends z.core.$ZodType>(
184
205
  element: T,
185
206
  ): CoListSchema<T> => {
@@ -187,39 +208,7 @@ export const coListDefiner = <T extends z.core.$ZodType>(
187
208
  collaborative: true,
188
209
  });
189
210
 
190
- type CleanedType = Pick<typeof arraySchema, "_zod" | "def" | "~standard">;
191
-
192
- const coListSchema = arraySchema as unknown as CleanedType & {
193
- collaborative: true;
194
- create: CoListSchema<T>["create"];
195
- load: CoListSchema<T>["load"];
196
- subscribe: CoListSchema<T>["subscribe"];
197
- /** @deprecated Define your helper methods separately, in standalone functions. */
198
- withHelpers: CoListSchema<T>["withHelpers"];
199
- };
200
-
201
- coListSchema.collaborative = true;
202
-
203
- coListSchema.create = function (this: CoListSchema<T>, ...args: any[]) {
204
- return (zodSchemaToCoSchema(this) as any).create(...args);
205
- } as CoListSchema<T>["create"];
206
-
207
- coListSchema.load = function (this: CoListSchema<T>, ...args: any[]) {
208
- return (zodSchemaToCoSchema(this) as any).load(...args);
209
- } as CoListSchema<T>["load"];
210
-
211
- coListSchema.subscribe = function (this: CoListSchema<T>, ...args: any[]) {
212
- return (zodSchemaToCoSchema(this) as any).subscribe(...args);
213
- } as CoListSchema<T>["subscribe"];
214
-
215
- coListSchema.withHelpers = function (
216
- this: CoListSchema<T>,
217
- helpers: (Self: CoListSchema<T>) => object,
218
- ) {
219
- return Object.assign(this, helpers(this));
220
- } as CoListSchema<T>["withHelpers"];
221
-
222
- return coListSchema;
211
+ return enrichCoListSchema(arraySchema);
223
212
  };
224
213
 
225
214
  export const coProfileDefiner = <
@@ -240,155 +229,134 @@ export const coProfileDefiner = <
240
229
  return coMapDefiner(ehnancedShape) as CoProfileSchema<Shape>;
241
230
  };
242
231
 
232
+ function enrichCoFeedSchema<T extends z.core.$ZodType>(
233
+ schema: z.ZodCustom<CoFeed<unknown>, unknown>,
234
+ element: T,
235
+ ) {
236
+ const enrichedSchema = Object.assign(schema, {
237
+ collaborative: true,
238
+ builtin: "CoFeed",
239
+ element,
240
+ create: (...args: any[]) => {
241
+ return coSchema.create(...args);
242
+ },
243
+ load: (...args: any[]) => {
244
+ return coSchema.load(...args);
245
+ },
246
+ subscribe: (...args: any[]) => {
247
+ return coSchema.subscribe(...args);
248
+ },
249
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
250
+ return Object.assign(schema, helpers(schema));
251
+ },
252
+ getCoSchema: () => {
253
+ return coSchema;
254
+ },
255
+ }) as unknown as CoFeedSchema<T>;
256
+
257
+ // Needs to be derived from the enriched schema
258
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
259
+
260
+ return enrichedSchema;
261
+ }
262
+
243
263
  export const coFeedDefiner = <T extends z.core.$ZodType>(
244
264
  element: T,
245
265
  ): CoFeedSchema<T> => {
246
- const placeholderSchema = z.instanceof(CoFeed);
247
-
248
- const coFeedSchema = placeholderSchema as unknown as Pick<
249
- typeof placeholderSchema,
250
- "_zod" | "def" | "~standard"
251
- > & {
252
- collaborative: true;
253
- builtin: "CoFeed";
254
- element: T;
255
- create: CoFeedSchema<T>["create"];
256
- load: CoFeedSchema<T>["load"];
257
- subscribe: CoFeedSchema<T>["subscribe"];
258
- };
259
-
260
- coFeedSchema.collaborative = true;
261
- coFeedSchema.builtin = "CoFeed";
262
- coFeedSchema.element = element;
263
-
264
- coFeedSchema.create = function (this: CoFeedSchema<T>, ...args: any[]) {
265
- return (zodSchemaToCoSchema(this) as any).create(...args);
266
- } as CoFeedSchema<T>["create"];
267
-
268
- coFeedSchema.load = function (this: CoFeedSchema<T>, ...args: any[]) {
269
- return (zodSchemaToCoSchema(this) as any).load(...args);
270
- } as CoFeedSchema<T>["load"];
271
-
272
- coFeedSchema.subscribe = function (this: CoFeedSchema<T>, ...args: any[]) {
273
- return (zodSchemaToCoSchema(this) as any).subscribe(...args);
274
- } as CoFeedSchema<T>["subscribe"];
275
-
276
- return coFeedSchema as unknown as CoFeedSchema<T>;
266
+ return enrichCoFeedSchema(z.instanceof(CoFeed), element);
277
267
  };
278
268
 
269
+ function enrichFileStreamSchema(schema: z.ZodCustom<FileStream, unknown>) {
270
+ const enrichedSchema = Object.assign(schema, {
271
+ collaborative: true,
272
+ builtin: "FileStream",
273
+ create: (...args: any[]) => {
274
+ return coSchema.create(...args);
275
+ },
276
+ createFromBlob: (...args: any[]) => {
277
+ return coSchema.createFromBlob(...args);
278
+ },
279
+ load: (...args: any[]) => {
280
+ return coSchema.load(...args);
281
+ },
282
+ loadAsBlob: (...args: any[]) => {
283
+ return coSchema.loadAsBlob(...args);
284
+ },
285
+ subscribe: (...args: any[]) => {
286
+ return coSchema.subscribe(...args);
287
+ },
288
+ getCoSchema: () => {
289
+ return coSchema;
290
+ },
291
+ }) as unknown as FileStreamSchema;
292
+
293
+ // Needs to be derived from the enriched schema
294
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
295
+
296
+ return enrichedSchema;
297
+ }
298
+
279
299
  export const coFileStreamDefiner = (): FileStreamSchema => {
280
- const placeholderSchema = z.instanceof(FileStream);
281
-
282
- const fileStreamSchema = placeholderSchema as unknown as Pick<
283
- typeof placeholderSchema,
284
- "_zod" | "def" | "~standard"
285
- > & {
286
- collaborative: true;
287
- builtin: "FileStream";
288
- create: FileStreamSchema["create"];
289
- createFromBlob: FileStreamSchema["createFromBlob"];
290
- load: FileStreamSchema["load"];
291
- loadAsBlob: FileStreamSchema["loadAsBlob"];
292
- subscribe: FileStreamSchema["subscribe"];
293
- };
294
-
295
- fileStreamSchema.collaborative = true;
296
- fileStreamSchema.builtin = "FileStream";
297
-
298
- fileStreamSchema.create = function (options: any) {
299
- return FileStream.create(options);
300
- } as FileStreamSchema["create"];
301
-
302
- fileStreamSchema.createFromBlob = function (blob: Blob, options: any) {
303
- return FileStream.createFromBlob(blob, options);
304
- } as FileStreamSchema["createFromBlob"];
305
-
306
- fileStreamSchema.load = function (id: string, options: any) {
307
- return FileStream.load(id, options);
308
- } as FileStreamSchema["load"];
309
-
310
- fileStreamSchema.loadAsBlob = function (id: string, options: any) {
311
- return FileStream.loadAsBlob(id, options);
312
- } as FileStreamSchema["loadAsBlob"];
313
-
314
- fileStreamSchema.subscribe = function (
315
- id: string,
316
- options: any,
317
- listener: any,
318
- ) {
319
- return FileStream.subscribe(id, options, listener);
320
- } as FileStreamSchema["subscribe"];
321
-
322
- return fileStreamSchema;
300
+ return enrichFileStreamSchema(z.instanceof(FileStream));
323
301
  };
324
302
 
303
+ function enrichPlainTextSchema(schema: z.ZodCustom<CoPlainText, unknown>) {
304
+ const enrichedSchema = Object.assign(schema, {
305
+ collaborative: true,
306
+ builtin: "CoPlainText",
307
+ create: (...args: any[]) => {
308
+ return coSchema.create(...args);
309
+ },
310
+ load: (...args: any[]) => {
311
+ return coSchema.load(...args);
312
+ },
313
+ subscribe: (...args: any[]) => {
314
+ return coSchema.subscribe(...args);
315
+ },
316
+ fromRaw: (...args: any[]) => {
317
+ return coSchema.fromRaw(...args);
318
+ },
319
+ getCoSchema: () => {
320
+ return coSchema;
321
+ },
322
+ }) as unknown as PlainTextSchema;
323
+
324
+ // Needs to be derived from the enriched schema
325
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
326
+
327
+ return enrichedSchema;
328
+ }
329
+
325
330
  export const coPlainTextDefiner = (): PlainTextSchema => {
326
- const placeholderSchema = z.instanceof(CoPlainText);
327
-
328
- const plainTextSchema = placeholderSchema as unknown as Pick<
329
- typeof placeholderSchema,
330
- "_zod" | "def" | "~standard"
331
- > & {
332
- collaborative: true;
333
- builtin: "CoPlainText";
334
- create: PlainTextSchema["create"];
335
- load: PlainTextSchema["load"];
336
- subscribe: PlainTextSchema["subscribe"];
337
- fromRaw: PlainTextSchema["fromRaw"];
338
- };
339
-
340
- plainTextSchema.collaborative = true;
341
- plainTextSchema.builtin = "CoPlainText";
342
-
343
- plainTextSchema.create = function (...args: any[]) {
344
- return (CoPlainText as any).create(...args);
345
- } as PlainTextSchema["create"];
346
-
347
- plainTextSchema.load = function (...args: any[]) {
348
- return (CoPlainText as any).load(...args);
349
- } as PlainTextSchema["load"];
350
-
351
- plainTextSchema.subscribe = function (...args: any[]) {
352
- return (CoPlainText as any).subscribe(...args);
353
- } as PlainTextSchema["subscribe"];
354
-
355
- plainTextSchema.fromRaw = function (...args: any[]) {
356
- return (CoPlainText as any).fromRaw(...args);
357
- } as PlainTextSchema["fromRaw"];
358
-
359
- return plainTextSchema;
331
+ return enrichPlainTextSchema(z.instanceof(CoPlainText));
360
332
  };
361
333
 
334
+ function enrichRichTextSchema(schema: z.ZodCustom<CoRichText, unknown>) {
335
+ const enrichedSchema = Object.assign(schema, {
336
+ collaborative: true,
337
+ builtin: "CoRichText",
338
+ create: (...args: any[]) => {
339
+ return coSchema.create(...args);
340
+ },
341
+ load: (...args: any[]) => {
342
+ return coSchema.load(...args);
343
+ },
344
+ subscribe: (...args: any[]) => {
345
+ return coSchema.subscribe(...args);
346
+ },
347
+ getCoSchema: () => {
348
+ return coSchema;
349
+ },
350
+ }) as unknown as RichTextSchema;
351
+
352
+ // Needs to be derived from the enriched schema
353
+ const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
354
+
355
+ return enrichedSchema;
356
+ }
357
+
362
358
  export const coRichTextDefiner = (): RichTextSchema => {
363
- const placeholderSchema = z.instanceof(CoRichText);
364
-
365
- const richTextSchema = placeholderSchema as unknown as Pick<
366
- typeof placeholderSchema,
367
- "_zod" | "def" | "~standard"
368
- > & {
369
- collaborative: true;
370
- builtin: "CoRichText";
371
- create: RichTextSchema["create"];
372
- load: RichTextSchema["load"];
373
- subscribe: RichTextSchema["subscribe"];
374
- };
375
-
376
- richTextSchema.collaborative = true;
377
- richTextSchema.builtin = "CoRichText";
378
-
379
- richTextSchema.create = function (...args: any[]) {
380
- return (CoRichText as any).create(...args);
381
- } as RichTextSchema["create"];
382
-
383
- richTextSchema.load = function (...args: any[]) {
384
- return (CoRichText as any).load(...args);
385
- } as RichTextSchema["load"];
386
-
387
- richTextSchema.subscribe = function (...args: any[]) {
388
- return (CoRichText as any).subscribe(...args);
389
- } as RichTextSchema["subscribe"];
390
-
391
- return richTextSchema;
359
+ return enrichRichTextSchema(z.instanceof(CoRichText));
392
360
  };
393
361
 
394
362
  export const coImageDefiner = (): typeof ImageDefinition => {
@@ -80,7 +80,7 @@ type AccountClassEssentials = {
80
80
  fromNode: <A extends Account>(this: AccountClass<A>, node: LocalNode) => A;
81
81
  };
82
82
 
83
- type AnyCoSchema =
83
+ export type AnyCoSchema =
84
84
  | AnyCoMapSchema
85
85
  | AnyAccountSchema
86
86
  | AnyCoRecordSchema
@@ -0,0 +1,23 @@
1
+ import type { CoValue } from "../internal.js";
2
+
3
+ export function applyCoValueMigrations(instance: CoValue) {
4
+ const node = instance._raw.core.node;
5
+
6
+ // @ts-expect-error _migratedCoValues is a custom expando property
7
+ const migratedCoValues = (node._migratedCoValues ??= new Set<string>());
8
+
9
+ if (
10
+ "migrate" in instance &&
11
+ typeof instance.migrate === "function" &&
12
+ instance._type !== "Account" &&
13
+ !migratedCoValues.has(instance.id)
14
+ ) {
15
+ // We flag this before the migration to avoid that internal loads trigger the migration again
16
+ migratedCoValues.add(instance.id);
17
+
18
+ const result = instance.migrate?.(instance);
19
+ if (result && "then" in result) {
20
+ throw new Error("Migration function cannot be async");
21
+ }
22
+ }
23
+ }
@@ -7,8 +7,10 @@ import {
7
7
  type ID,
8
8
  type RefEncoded,
9
9
  type RefsToResolve,
10
+ instantiateRefEncoded,
10
11
  isRefEncoded,
11
12
  } from "../internal.js";
13
+ import { applyCoValueMigrations } from "../lib/migration.js";
12
14
  import { CoValueCoreSubscription } from "./CoValueCoreSubscription.js";
13
15
  import { JazzError, type JazzErrorIssue } from "./JazzError.js";
14
16
  import type { SubscriptionValue, Unloaded } from "./types.js";
@@ -32,6 +34,8 @@ export class SubscriptionScope<D extends CoValue> {
32
34
  autoloadedKeys = new Set<string>();
33
35
  skipInvalidKeys = new Set<string>();
34
36
  totalValidTransactions = 0;
37
+ migrated = false;
38
+ migrating = false;
35
39
 
36
40
  silenceUpdates = false;
37
41
 
@@ -43,7 +47,29 @@ export class SubscriptionScope<D extends CoValue> {
43
47
  ) {
44
48
  this.resolve = resolve;
45
49
  this.value = { type: "unloaded", id };
50
+
51
+ let lastUpdate: RawCoValue | "unavailable" | undefined;
46
52
  this.subscription = new CoValueCoreSubscription(node, id, (value) => {
53
+ lastUpdate = value;
54
+
55
+ // Need all these checks because the migration can trigger new syncronous updates
56
+ //
57
+ // We want to:
58
+ // - Run the migration only once
59
+ // - Skip all the updates until the migration is done
60
+ // - Trigger handleUpdate only with the final value
61
+ if (!this.migrated && value !== "unavailable") {
62
+ if (this.migrating) {
63
+ return;
64
+ }
65
+
66
+ this.migrating = true;
67
+ applyCoValueMigrations(instantiateRefEncoded(this.schema, value));
68
+ this.migrated = true;
69
+ this.handleUpdate(lastUpdate);
70
+ return;
71
+ }
72
+
47
73
  this.handleUpdate(value);
48
74
  });
49
75
  }
@@ -223,9 +249,28 @@ export class SubscriptionScope<D extends CoValue> {
223
249
  }
224
250
 
225
251
  getCurrentValue() {
226
- if (!this.shouldSendUpdates()) return;
227
- if (this.errorFromChildren) return this.errorFromChildren;
228
- return this.value;
252
+ if (
253
+ this.value.type === "unauthorized" ||
254
+ this.value.type === "unavailable"
255
+ ) {
256
+ console.error(this.value.toString());
257
+ return null;
258
+ }
259
+
260
+ if (!this.shouldSendUpdates()) {
261
+ return undefined;
262
+ }
263
+
264
+ if (this.errorFromChildren) {
265
+ console.error(this.errorFromChildren.toString());
266
+ return null;
267
+ }
268
+
269
+ if (this.value.type === "loaded") {
270
+ return this.value.value;
271
+ }
272
+
273
+ return undefined;
229
274
  }
230
275
 
231
276
  triggerUpdate() {