jazz-tools 0.15.15 → 0.16.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.
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
- package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
- package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
- package/.turbo/turbo-build.log +46 -50
- package/CHANGELOG.md +35 -0
- package/dist/browser/index.d.ts +2 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser-media-images/index.d.ts +3 -1
- package/dist/browser-media-images/index.d.ts.map +1 -1
- package/dist/browser-media-images/index.js.map +1 -1
- package/dist/{chunk-4CFNXQE7.js → chunk-MLCNE3TL.js} +791 -698
- package/dist/chunk-MLCNE3TL.js.map +1 -0
- package/dist/index.js +363 -11
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.d.ts +2 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/testing.js +3 -1
- package/dist/react/testing.js.map +1 -1
- package/dist/react-core/hooks.d.ts +2 -2
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +3 -3
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-native-core/hooks.d.ts +2 -2
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/svelte/jazz.class.svelte.d.ts +2 -2
- package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
- package/dist/svelte/jazz.class.svelte.js +5 -5
- package/dist/svelte/jazz.svelte.d.ts +2 -2
- package/dist/svelte/jazz.svelte.d.ts.map +1 -1
- package/dist/testing.js +3 -3
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts +3 -13
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/account.d.ts +2 -2
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coList.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts +5 -18
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/deepLoading.d.ts +4 -1
- package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
- package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
- package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/tools/coValues/group.d.ts +1 -0
- package/dist/tools/coValues/group.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts +2 -2
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +58 -17
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/coValues/request.d.ts +82 -0
- package/dist/tools/coValues/request.d.ts.map +1 -0
- package/dist/tools/coValues/schemaUnion.d.ts +5 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/exports.d.ts +4 -3
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/createContext.d.ts +4 -4
- package/dist/tools/implementation/createContext.d.ts.map +1 -1
- package/dist/tools/implementation/invites.d.ts +2 -2
- package/dist/tools/implementation/invites.d.ts.map +1 -1
- package/dist/tools/implementation/schemaUtils.d.ts +8 -0
- package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +3 -2
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/lib/id.d.ts +2 -0
- package/dist/tools/lib/id.d.ts.map +1 -0
- package/dist/tools/lib/utilityTypes.d.ts +10 -0
- package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
- package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -2
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/subscribe/utils.d.ts.map +1 -1
- package/dist/tools/testing.d.ts +2 -2
- package/dist/tools/testing.d.ts.map +1 -1
- package/dist/tools/tests/exportImport.test.d.ts +2 -0
- package/dist/tools/tests/exportImport.test.d.ts.map +1 -0
- package/dist/tools/tests/request.test.d.ts +2 -0
- package/dist/tools/tests/request.test.d.ts.map +1 -0
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +2 -2
- package/dist/worker/index.js.map +1 -1
- package/package.json +7 -6
- package/src/browser/index.ts +2 -4
- package/src/browser-media-images/index.ts +1 -1
- package/src/react/hooks.tsx +2 -2
- package/src/react-core/hooks.ts +6 -6
- package/src/react-core/tests/useAccount.test.ts +2 -2
- package/src/react-core/tests/useCoState.test.ts +3 -2
- package/src/react-native-core/hooks.tsx +2 -2
- package/src/svelte/jazz.class.svelte.ts +10 -7
- package/src/svelte/jazz.svelte.ts +2 -2
- package/src/tools/coValues/CoValueBase.ts +8 -20
- package/src/tools/coValues/account.ts +18 -14
- package/src/tools/coValues/coFeed.ts +0 -4
- package/src/tools/coValues/coList.ts +7 -9
- package/src/tools/coValues/coMap.ts +1 -6
- package/src/tools/coValues/coPlainText.ts +4 -4
- package/src/tools/coValues/deepLoading.ts +4 -1
- package/src/tools/coValues/extensions/imageDef.ts +3 -3
- package/src/tools/coValues/group.ts +1 -0
- package/src/tools/coValues/inbox.ts +8 -7
- package/src/tools/coValues/interfaces.ts +177 -69
- package/src/tools/coValues/request.ts +633 -0
- package/src/tools/coValues/schemaUnion.ts +8 -4
- package/src/tools/exports.ts +11 -14
- package/src/tools/implementation/createContext.ts +9 -9
- package/src/tools/implementation/invites.ts +2 -2
- package/src/tools/implementation/schemaUtils.ts +18 -0
- package/src/tools/implementation/zodSchema/coExport.ts +14 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
- package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
- package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
- package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
- package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
- package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
- package/src/tools/internal.ts +3 -2
- package/src/tools/lib/id.ts +3 -0
- package/src/tools/lib/utilityTypes.ts +7 -0
- package/src/tools/subscribe/SubscriptionScope.ts +10 -1
- package/src/tools/subscribe/utils.ts +4 -2
- package/src/tools/testing.ts +4 -4
- package/src/tools/tests/ContextManager.test.ts +8 -9
- package/src/tools/tests/account.test.ts +65 -3
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
- package/src/tools/tests/coFeed.test-d.ts +4 -3
- package/src/tools/tests/coList.test-d.ts +32 -3
- package/src/tools/tests/coList.test.ts +20 -2
- package/src/tools/tests/coMap.record.test-d.ts +31 -3
- package/src/tools/tests/coMap.record.test.ts +9 -9
- package/src/tools/tests/coMap.test-d.ts +8 -8
- package/src/tools/tests/coMap.test.ts +19 -5
- package/src/tools/tests/coOptional.test.ts +63 -1
- package/src/tools/tests/createContext.test.ts +7 -9
- package/src/tools/tests/deepLoading.test.ts +4 -10
- package/src/tools/tests/exportImport.test.ts +526 -0
- package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
- package/src/tools/tests/inbox.test.ts +3 -2
- package/src/tools/tests/load.test.ts +3 -29
- package/src/tools/tests/request.test.ts +951 -0
- package/src/tools/tests/schemaUnion.test.ts +2 -2
- package/src/tools/tests/subscribe.test.ts +22 -114
- package/src/tools/tests/testing.test.ts +6 -6
- package/src/tools/tests/zod.test-d.ts +27 -0
- package/src/tools/tests/zod.test.ts +50 -45
- package/src/worker/index.ts +0 -1
- package/tsup.config.ts +0 -2
- package/dist/chunk-4CFNXQE7.js.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
- package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -1,8 +1,14 @@
|
|
1
|
-
import type { CoValueUniqueness, RawCoValue } from "cojson";
|
2
1
|
import {
|
3
|
-
type
|
2
|
+
type CoValueUniqueness,
|
3
|
+
type CojsonInternalTypes,
|
4
|
+
type RawCoValue,
|
5
|
+
emptyKnownState,
|
6
|
+
} from "cojson";
|
7
|
+
import { AvailableCoValueCore } from "cojson/dist/coValueCore/coValueCore.js";
|
8
|
+
import {
|
9
|
+
Account,
|
4
10
|
AnonymousJazzAgent,
|
5
|
-
|
11
|
+
CoValueClassOrSchema,
|
6
12
|
type Group,
|
7
13
|
Loaded,
|
8
14
|
RefsToResolve,
|
@@ -14,7 +20,7 @@ import {
|
|
14
20
|
SubscriptionScope,
|
15
21
|
type SubscriptionValue,
|
16
22
|
activeAccountContext,
|
17
|
-
|
23
|
+
coValueClassFromCoValueClassOrSchema,
|
18
24
|
inspect,
|
19
25
|
} from "../internal.js";
|
20
26
|
|
@@ -304,71 +310,6 @@ export function subscribeToCoValue<
|
|
304
310
|
return unsubscribe;
|
305
311
|
}
|
306
312
|
|
307
|
-
/**
|
308
|
-
* @deprecated Used for the React integration in the past, but we moved to use SubscriptionScope directly.
|
309
|
-
*
|
310
|
-
* Going to be removed in the next minor version.
|
311
|
-
*/
|
312
|
-
export function createCoValueObservable<
|
313
|
-
S extends CoValueOrZodSchema,
|
314
|
-
const R extends ResolveQuery<S>,
|
315
|
-
>(initialValue: undefined | null = undefined) {
|
316
|
-
let currentValue: Loaded<S, R> | undefined | null = initialValue;
|
317
|
-
let subscriberCount = 0;
|
318
|
-
|
319
|
-
function subscribe(
|
320
|
-
cls: S,
|
321
|
-
id: string,
|
322
|
-
options: {
|
323
|
-
loadAs: Account | AnonymousJazzAgent;
|
324
|
-
resolve?: ResolveQueryStrict<S, R>;
|
325
|
-
onUnavailable?: () => void;
|
326
|
-
onUnauthorized?: () => void;
|
327
|
-
syncResolution?: boolean;
|
328
|
-
},
|
329
|
-
listener: () => void,
|
330
|
-
) {
|
331
|
-
subscriberCount++;
|
332
|
-
|
333
|
-
const unsubscribe = subscribeToCoValue(
|
334
|
-
anySchemaToCoSchema(cls),
|
335
|
-
id,
|
336
|
-
{
|
337
|
-
loadAs: options.loadAs,
|
338
|
-
resolve: options.resolve as any,
|
339
|
-
onUnavailable: () => {
|
340
|
-
currentValue = null;
|
341
|
-
options.onUnavailable?.();
|
342
|
-
},
|
343
|
-
onUnauthorized: () => {
|
344
|
-
currentValue = null;
|
345
|
-
options.onUnauthorized?.();
|
346
|
-
},
|
347
|
-
syncResolution: options.syncResolution,
|
348
|
-
},
|
349
|
-
(value) => {
|
350
|
-
currentValue = value as Loaded<S, R>;
|
351
|
-
listener();
|
352
|
-
},
|
353
|
-
);
|
354
|
-
|
355
|
-
return () => {
|
356
|
-
unsubscribe();
|
357
|
-
subscriberCount--;
|
358
|
-
if (subscriberCount === 0) {
|
359
|
-
currentValue = undefined;
|
360
|
-
}
|
361
|
-
};
|
362
|
-
}
|
363
|
-
|
364
|
-
const observable = {
|
365
|
-
getCurrentValue: () => currentValue,
|
366
|
-
subscribe,
|
367
|
-
};
|
368
|
-
|
369
|
-
return observable;
|
370
|
-
}
|
371
|
-
|
372
313
|
export function subscribeToExistingCoValue<
|
373
314
|
V extends CoValue,
|
374
315
|
const R extends RefsToResolve<V>,
|
@@ -462,3 +403,170 @@ export function parseGroupCreateOptions(
|
|
462
403
|
? { owner: options }
|
463
404
|
: { owner: options.owner ?? activeAccountContext.get() };
|
464
405
|
}
|
406
|
+
|
407
|
+
/**
|
408
|
+
* Deeply export a CoValue to a content piece.
|
409
|
+
*
|
410
|
+
* @param cls - The class of the CoValue to export.
|
411
|
+
* @param id - The ID of the CoValue to export.
|
412
|
+
* @param options - The options for the export.
|
413
|
+
* @returns The content pieces that were exported.
|
414
|
+
*
|
415
|
+
* @example
|
416
|
+
* ```ts
|
417
|
+
* const Address = co.map({
|
418
|
+
* street: z.string(),
|
419
|
+
* city: z.string(),
|
420
|
+
* });
|
421
|
+
*
|
422
|
+
* const Person = co.map({
|
423
|
+
* name: z.string(),
|
424
|
+
* address: Address,
|
425
|
+
* });
|
426
|
+
*
|
427
|
+
* const group = Group.create();
|
428
|
+
* const address = Address.create(
|
429
|
+
* { street: "123 Main St", city: "New York" },
|
430
|
+
* group,
|
431
|
+
* );
|
432
|
+
* const person = Person.create({ name: "John", address }, group);
|
433
|
+
* group.addMember("everyone", "reader");
|
434
|
+
*
|
435
|
+
* // Export with nested references resolved, values can be serialized to JSON
|
436
|
+
* const exportedWithResolve = await exportCoValue(Person, person.id, {
|
437
|
+
* resolve: { address: true },
|
438
|
+
* });
|
439
|
+
*
|
440
|
+
* // In another client or session
|
441
|
+
* // Load the exported content pieces into the node, they will be persisted
|
442
|
+
* importContentPieces(exportedWithResolve);
|
443
|
+
*
|
444
|
+
* // Now the person can be loaded from the node, even offline
|
445
|
+
* const person = await loadCoValue(Person, person.id, {
|
446
|
+
* resolve: { address: true },
|
447
|
+
* });
|
448
|
+
* ```
|
449
|
+
*/
|
450
|
+
export async function exportCoValue<
|
451
|
+
S extends CoValueClassOrSchema,
|
452
|
+
const R extends ResolveQuery<S>,
|
453
|
+
>(
|
454
|
+
cls: S,
|
455
|
+
id: ID<CoValue>,
|
456
|
+
options: {
|
457
|
+
resolve?: ResolveQueryStrict<S, R>;
|
458
|
+
loadAs: Account | AnonymousJazzAgent;
|
459
|
+
skipRetry?: boolean;
|
460
|
+
bestEffortResolution?: boolean;
|
461
|
+
},
|
462
|
+
) {
|
463
|
+
const loadAs = options.loadAs ?? activeAccountContext.get();
|
464
|
+
const node = "node" in loadAs ? loadAs.node : loadAs._raw.core.node;
|
465
|
+
|
466
|
+
const resolve = options.resolve ?? true;
|
467
|
+
|
468
|
+
const rootNode = new SubscriptionScope<CoValue>(
|
469
|
+
node,
|
470
|
+
resolve as any,
|
471
|
+
id,
|
472
|
+
{
|
473
|
+
ref: coValueClassFromCoValueClassOrSchema(cls),
|
474
|
+
optional: false,
|
475
|
+
},
|
476
|
+
options.skipRetry,
|
477
|
+
options.bestEffortResolution,
|
478
|
+
);
|
479
|
+
|
480
|
+
const value = await new Promise<Loaded<S, R> | null>((resolve) => {
|
481
|
+
rootNode.setListener((value) => {
|
482
|
+
if (value.type === "unavailable") {
|
483
|
+
resolve(null);
|
484
|
+
console.error(value.toString());
|
485
|
+
} else if (value.type === "unauthorized") {
|
486
|
+
resolve(null);
|
487
|
+
console.error(value.toString());
|
488
|
+
} else if (value.type === "loaded") {
|
489
|
+
resolve(value.value as Loaded<S, R>);
|
490
|
+
}
|
491
|
+
|
492
|
+
rootNode.destroy();
|
493
|
+
});
|
494
|
+
});
|
495
|
+
|
496
|
+
if (!value) {
|
497
|
+
return null;
|
498
|
+
}
|
499
|
+
|
500
|
+
const valuesExported = new Set<string>();
|
501
|
+
const contentPieces: CojsonInternalTypes.NewContentMessage[] = [];
|
502
|
+
|
503
|
+
loadContentPiecesFromSubscription(rootNode, valuesExported, contentPieces);
|
504
|
+
|
505
|
+
return contentPieces;
|
506
|
+
}
|
507
|
+
|
508
|
+
function loadContentPiecesFromSubscription(
|
509
|
+
subscription: SubscriptionScope<any>,
|
510
|
+
valuesExported: Set<string>,
|
511
|
+
contentPieces: CojsonInternalTypes.NewContentMessage[],
|
512
|
+
) {
|
513
|
+
if (valuesExported.has(subscription.id)) {
|
514
|
+
return;
|
515
|
+
}
|
516
|
+
|
517
|
+
valuesExported.add(subscription.id);
|
518
|
+
|
519
|
+
const core = subscription.getCurrentValue()?._raw
|
520
|
+
.core as AvailableCoValueCore;
|
521
|
+
|
522
|
+
if (core) {
|
523
|
+
loadContentPiecesFromCoValue(core, valuesExported, contentPieces);
|
524
|
+
}
|
525
|
+
|
526
|
+
for (const child of subscription.childNodes.values()) {
|
527
|
+
loadContentPiecesFromSubscription(child, valuesExported, contentPieces);
|
528
|
+
}
|
529
|
+
}
|
530
|
+
|
531
|
+
function loadContentPiecesFromCoValue(
|
532
|
+
core: AvailableCoValueCore,
|
533
|
+
valuesExported: Set<string>,
|
534
|
+
contentPieces: CojsonInternalTypes.NewContentMessage[],
|
535
|
+
) {
|
536
|
+
for (const dependency of core.getDependedOnCoValues()) {
|
537
|
+
if (valuesExported.has(dependency)) {
|
538
|
+
continue;
|
539
|
+
}
|
540
|
+
|
541
|
+
const depCoValue = core.node.getCoValue(dependency);
|
542
|
+
|
543
|
+
if (depCoValue.isAvailable()) {
|
544
|
+
valuesExported.add(dependency);
|
545
|
+
loadContentPiecesFromCoValue(depCoValue, valuesExported, contentPieces);
|
546
|
+
}
|
547
|
+
}
|
548
|
+
|
549
|
+
const pieces = core.verified.newContentSince(emptyKnownState(core.id));
|
550
|
+
|
551
|
+
for (const piece of pieces ?? []) {
|
552
|
+
contentPieces.push(piece);
|
553
|
+
}
|
554
|
+
}
|
555
|
+
|
556
|
+
/**
|
557
|
+
* Import content pieces into the node.
|
558
|
+
*
|
559
|
+
* @param contentPieces - The content pieces to import.
|
560
|
+
* @param loadAs - The account to load the content pieces as.
|
561
|
+
*/
|
562
|
+
export function importContentPieces(
|
563
|
+
contentPieces: CojsonInternalTypes.NewContentMessage[],
|
564
|
+
loadAs?: Account | AnonymousJazzAgent,
|
565
|
+
) {
|
566
|
+
const account = loadAs ?? Account.getMe();
|
567
|
+
const node = "node" in account ? account.node : account._raw.core.node;
|
568
|
+
|
569
|
+
for (const piece of contentPieces) {
|
570
|
+
node.syncManager.handleNewContent(piece, "import");
|
571
|
+
}
|
572
|
+
}
|