jazz-tools 0.19.7 → 0.19.10
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/.turbo/turbo-build.log +65 -59
- package/CHANGELOG.md +34 -3
- package/dist/{chunk-CUS6O5NE.js → chunk-FFEEPZEG.js} +454 -122
- package/dist/chunk-FFEEPZEG.js.map +1 -0
- package/dist/expo/polyfills.js +22 -0
- package/dist/expo/polyfills.js.map +1 -0
- package/dist/index.js +26 -6
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.d.ts +1 -1
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +5 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/hooks.d.ts +59 -0
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +133 -34
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-core/tests/testUtils.d.ts +1 -0
- package/dist/react-core/tests/testUtils.d.ts.map +1 -1
- package/dist/react-core/tests/useSuspenseAccount.test.d.ts +2 -0
- package/dist/react-core/tests/useSuspenseAccount.test.d.ts.map +1 -0
- package/dist/react-core/tests/useSuspenseCoState.test.d.ts +2 -0
- package/dist/react-core/tests/useSuspenseCoState.test.d.ts.map +1 -0
- package/dist/react-core/use.d.ts +3 -0
- package/dist/react-core/use.d.ts.map +1 -0
- package/dist/react-native/index.d.ts +1 -1
- package/dist/react-native/index.d.ts.map +1 -1
- package/dist/react-native/index.js +717 -9
- package/dist/react-native/index.js.map +1 -1
- package/dist/react-native/polyfills.js +22 -0
- package/dist/react-native/polyfills.js.map +1 -0
- package/dist/react-native-core/crypto/RNCrypto.d.ts +2 -0
- package/dist/react-native-core/crypto/RNCrypto.d.ts.map +1 -0
- package/dist/react-native-core/crypto/RNCrypto.js +3 -0
- package/dist/react-native-core/crypto/RNCrypto.js.map +1 -0
- package/dist/react-native-core/hooks.d.ts +1 -1
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.d.ts.map +1 -1
- package/dist/react-native-core/index.js +5 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/react-native-core/platform.d.ts +2 -1
- package/dist/react-native-core/platform.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/account.d.ts +3 -3
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts +3 -3
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coList.d.ts +4 -4
- package/dist/tools/coValues/coList.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts +7 -7
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/coPlainText.d.ts +2 -2
- package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
- package/dist/tools/coValues/coVector.d.ts +2 -2
- package/dist/tools/coValues/coVector.d.ts.map +1 -1
- package/dist/tools/coValues/deepLoading.d.ts +24 -0
- package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
- package/dist/tools/coValues/group.d.ts +2 -2
- package/dist/tools/coValues/group.d.ts.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +7 -7
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/coValues/schemaUnion.d.ts +2 -2
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/config.d.ts +3 -0
- package/dist/tools/config.d.ts.map +1 -0
- package/dist/tools/exports.d.ts +2 -0
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/ContextManager.d.ts +3 -0
- package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +4 -4
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +4 -4
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +4 -4
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +8 -22
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
- package/dist/tools/subscribe/JazzError.d.ts.map +1 -1
- package/dist/tools/subscribe/SubscriptionCache.d.ts +51 -0
- package/dist/tools/subscribe/SubscriptionCache.d.ts.map +1 -0
- package/dist/tools/subscribe/SubscriptionScope.d.ts +27 -2
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/subscribe/errorReporting.d.ts +31 -0
- package/dist/tools/subscribe/errorReporting.d.ts.map +1 -0
- package/dist/tools/subscribe/utils.d.ts +9 -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/SubscriptionCache.test.d.ts +2 -0
- package/dist/tools/tests/SubscriptionCache.test.d.ts.map +1 -0
- package/dist/tools/tests/errorReporting.test.d.ts +2 -0
- package/dist/tools/tests/errorReporting.test.d.ts.map +1 -0
- package/package.json +22 -7
- package/src/react/hooks.tsx +2 -0
- package/src/react/index.ts +1 -14
- package/src/react-core/hooks.ts +181 -16
- package/src/react-core/tests/createCoValueSubscriptionContext.test.tsx +18 -8
- package/src/react-core/tests/testUtils.tsx +67 -5
- package/src/react-core/tests/useCoState.test.ts +3 -7
- package/src/react-core/tests/useSubscriptionSelector.test.ts +3 -7
- package/src/react-core/tests/useSuspenseAccount.test.tsx +343 -0
- package/src/react-core/tests/useSuspenseCoState.test.tsx +1182 -0
- package/src/react-core/use.ts +46 -0
- package/src/react-native/index.ts +1 -1
- package/src/react-native-core/crypto/RNCrypto.ts +1 -0
- package/src/react-native-core/hooks.tsx +2 -0
- package/src/react-native-core/index.ts +2 -0
- package/src/react-native-core/platform.ts +2 -1
- package/src/react-native-core/polyfills/index.js +28 -0
- package/src/tools/coValues/account.ts +3 -4
- package/src/tools/coValues/coFeed.ts +3 -2
- package/src/tools/coValues/coList.ts +4 -4
- package/src/tools/coValues/coMap.ts +4 -4
- package/src/tools/coValues/coPlainText.ts +2 -2
- package/src/tools/coValues/coVector.ts +2 -2
- package/src/tools/coValues/deepLoading.ts +31 -0
- package/src/tools/coValues/group.ts +2 -2
- package/src/tools/coValues/interfaces.ts +21 -26
- package/src/tools/coValues/schemaUnion.ts +2 -2
- package/src/tools/config.ts +9 -0
- package/src/tools/exports.ts +4 -0
- package/src/tools/implementation/ContextManager.ts +13 -0
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +4 -4
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +4 -4
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +4 -10
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +2 -2
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +2 -2
- package/src/tools/subscribe/CoValueCoreSubscription.ts +71 -100
- package/src/tools/subscribe/JazzError.ts +9 -6
- package/src/tools/subscribe/SubscriptionCache.ts +272 -0
- package/src/tools/subscribe/SubscriptionScope.ts +218 -29
- package/src/tools/subscribe/errorReporting.ts +67 -0
- package/src/tools/subscribe/utils.ts +77 -0
- package/src/tools/testing.ts +0 -3
- package/src/tools/tests/CoValueCoreSubscription.test.ts +46 -12
- package/src/tools/tests/ContextManager.test.ts +85 -0
- package/src/tools/tests/SubscriptionCache.test.ts +237 -0
- package/src/tools/tests/coMap.test.ts +5 -7
- package/src/tools/tests/deepLoading.test.ts +47 -47
- package/src/tools/tests/errorReporting.test.ts +103 -0
- package/src/tools/tests/load.test.ts +21 -1
- package/src/tools/tests/request.test.ts +2 -1
- package/src/tools/tests/subscribe.test.ts +44 -0
- package/tsup.config.ts +17 -0
- package/dist/chunk-CUS6O5NE.js.map +0 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
// shim from https://github.com/pmndrs/jotai/blob/f287c5d665a807e676bc731e83174c62c1fe1fc9/src/react/useAtomValue.ts#L13C1-L56C1
|
|
4
|
+
const attachPromiseStatus = <T>(
|
|
5
|
+
promise: PromiseLike<T> & {
|
|
6
|
+
status?: "pending" | "fulfilled" | "rejected";
|
|
7
|
+
value?: T;
|
|
8
|
+
reason?: unknown;
|
|
9
|
+
},
|
|
10
|
+
) => {
|
|
11
|
+
if (!promise.status) {
|
|
12
|
+
promise.status = "pending";
|
|
13
|
+
promise.then(
|
|
14
|
+
(v) => {
|
|
15
|
+
promise.status = "fulfilled";
|
|
16
|
+
promise.value = v;
|
|
17
|
+
},
|
|
18
|
+
(e) => {
|
|
19
|
+
promise.status = "rejected";
|
|
20
|
+
promise.reason = e;
|
|
21
|
+
},
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const use =
|
|
27
|
+
React.use ||
|
|
28
|
+
// A shim for older React versions
|
|
29
|
+
(<T>(
|
|
30
|
+
promise: PromiseLike<T> & {
|
|
31
|
+
status?: "pending" | "fulfilled" | "rejected";
|
|
32
|
+
value?: T;
|
|
33
|
+
reason?: unknown;
|
|
34
|
+
},
|
|
35
|
+
): T => {
|
|
36
|
+
if (promise.status === "pending") {
|
|
37
|
+
throw promise;
|
|
38
|
+
} else if (promise.status === "fulfilled") {
|
|
39
|
+
return promise.value as T;
|
|
40
|
+
} else if (promise.status === "rejected") {
|
|
41
|
+
throw promise.reason;
|
|
42
|
+
} else {
|
|
43
|
+
attachPromiseStatus(promise);
|
|
44
|
+
throw promise;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "cojson/crypto/RNCrypto";
|
|
@@ -11,7 +11,9 @@ export {
|
|
|
11
11
|
} from "jazz-tools/react-core";
|
|
12
12
|
|
|
13
13
|
export { SQLiteDatabaseDriverAsync } from "cojson";
|
|
14
|
+
|
|
14
15
|
export { parseInviteLink } from "jazz-tools";
|
|
16
|
+
|
|
15
17
|
export { createInviteLink, setupKvStore } from "./platform.js";
|
|
16
18
|
export {
|
|
17
19
|
ReactNativeContextManager,
|
|
@@ -24,12 +24,13 @@ import { KvStore, KvStoreContext } from "./storage/kv-store-context.js";
|
|
|
24
24
|
import { SQLiteDatabaseDriverAsync } from "cojson";
|
|
25
25
|
import { WebSocketPeerWithReconnection } from "cojson-transport-ws";
|
|
26
26
|
import type { RNQuickCrypto } from "jazz-tools/react-native-core/crypto";
|
|
27
|
+
import type { RNCrypto } from "cojson/crypto/RNCrypto";
|
|
27
28
|
|
|
28
29
|
export type BaseReactNativeContextOptions = {
|
|
29
30
|
sync: SyncConfig;
|
|
30
31
|
reconnectionTimeout?: number;
|
|
31
32
|
storage?: SQLiteDatabaseDriverAsync | "disabled";
|
|
32
|
-
CryptoProvider?: typeof PureJSCrypto | typeof RNQuickCrypto;
|
|
33
|
+
CryptoProvider?: typeof PureJSCrypto | typeof RNQuickCrypto | typeof RNCrypto;
|
|
33
34
|
authSecretStorage: AuthSecretStorage;
|
|
34
35
|
};
|
|
35
36
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ReadableStream } from "readable-stream"; // polyfill ReadableStream
|
|
2
|
+
import "@azure/core-asynciterator-polyfill"; // polyfill Async Iterator
|
|
3
|
+
import "react-native-get-random-values"; // polyfill getRandomValues
|
|
4
|
+
import FastTextEncoder from "react-native-fast-encoder"; // polyfill TextEncoder/TextDecoder
|
|
5
|
+
|
|
6
|
+
// Add encodeInto method which the fast encoder doesn't provide
|
|
7
|
+
if (!FastTextEncoder.prototype.encodeInto) {
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} source
|
|
10
|
+
* @param {Uint8Array} destination
|
|
11
|
+
*/
|
|
12
|
+
FastTextEncoder.prototype.encodeInto = function (source, destination) {
|
|
13
|
+
const encoded = this.encode(source);
|
|
14
|
+
const writeLength = Math.min(encoded.length, destination.length);
|
|
15
|
+
for (let i = 0; i < writeLength; i++) {
|
|
16
|
+
destination[i] = encoded[i];
|
|
17
|
+
}
|
|
18
|
+
return { read: source.length, written: writeLength };
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Install polyfills
|
|
23
|
+
globalThis.TextEncoder = FastTextEncoder;
|
|
24
|
+
globalThis.TextDecoder = FastTextEncoder;
|
|
25
|
+
globalThis.ReadableStream = ReadableStream;
|
|
26
|
+
if (__DEV__) {
|
|
27
|
+
console.log("[Jazz] - Polyfills successfully installed");
|
|
28
|
+
}
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
ID,
|
|
29
29
|
InstanceOrPrimitiveOfSchema,
|
|
30
30
|
MaybeLoaded,
|
|
31
|
+
Settled,
|
|
31
32
|
Profile,
|
|
32
33
|
Ref,
|
|
33
34
|
type RefEncoded,
|
|
@@ -131,9 +132,7 @@ export class Account extends CoValueBase implements CoValue {
|
|
|
131
132
|
valueID: string,
|
|
132
133
|
inviteSecret: InviteSecret,
|
|
133
134
|
coValueClass?: S,
|
|
134
|
-
): Promise<
|
|
135
|
-
MaybeLoaded<Resolved<InstanceOfSchemaCoValuesMaybeLoaded<S>, true>>
|
|
136
|
-
> {
|
|
135
|
+
): Promise<Settled<Resolved<InstanceOfSchemaCoValuesMaybeLoaded<S>, true>>> {
|
|
137
136
|
if (!this.$jazz.isLocalNodeOwner) {
|
|
138
137
|
throw new Error("Only a controlled account can accept invites");
|
|
139
138
|
}
|
|
@@ -380,7 +379,7 @@ export class Account extends CoValueBase implements CoValue {
|
|
|
380
379
|
resolve?: RefsToResolveStrict<A, R>;
|
|
381
380
|
loadAs?: Account | AnonymousJazzAgent;
|
|
382
381
|
},
|
|
383
|
-
): Promise<
|
|
382
|
+
): Promise<Settled<Resolved<A, R>>> {
|
|
384
383
|
return loadCoValueWithoutMe(this, id, options);
|
|
385
384
|
}
|
|
386
385
|
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
Group,
|
|
21
21
|
ID,
|
|
22
22
|
MaybeLoaded,
|
|
23
|
+
Settled,
|
|
23
24
|
LoadedAndRequired,
|
|
24
25
|
unstable_mergeBranch,
|
|
25
26
|
RefsToResolve,
|
|
@@ -297,7 +298,7 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
|
|
|
297
298
|
resolve?: RefsToResolveStrict<F, R>;
|
|
298
299
|
loadAs?: Account | AnonymousJazzAgent;
|
|
299
300
|
},
|
|
300
|
-
): Promise<
|
|
301
|
+
): Promise<Settled<Resolved<F, R>>> {
|
|
301
302
|
return loadCoValueWithoutMe(this, id, options ?? {});
|
|
302
303
|
}
|
|
303
304
|
|
|
@@ -983,7 +984,7 @@ export class FileStream extends CoValueBase implements CoValue {
|
|
|
983
984
|
loadAs?: Account | AnonymousJazzAgent;
|
|
984
985
|
allowUnfinished?: boolean;
|
|
985
986
|
},
|
|
986
|
-
): Promise<
|
|
987
|
+
): Promise<Settled<FileStream>> {
|
|
987
988
|
const stream = await loadCoValueWithoutMe(this, id, options);
|
|
988
989
|
|
|
989
990
|
/**
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
Group,
|
|
12
12
|
ID,
|
|
13
13
|
AsLoaded,
|
|
14
|
-
|
|
14
|
+
Settled,
|
|
15
15
|
unstable_mergeBranch,
|
|
16
16
|
RefEncoded,
|
|
17
17
|
RefsToResolve,
|
|
@@ -268,7 +268,7 @@ export class CoList<out Item = any>
|
|
|
268
268
|
resolve?: RefsToResolveStrict<L, R>;
|
|
269
269
|
loadAs?: Account | AnonymousJazzAgent;
|
|
270
270
|
},
|
|
271
|
-
): Promise<
|
|
271
|
+
): Promise<Settled<Resolved<L, R>>> {
|
|
272
272
|
return loadCoValueWithoutMe(this, id, options);
|
|
273
273
|
}
|
|
274
274
|
|
|
@@ -380,7 +380,7 @@ export class CoList<out Item = any>
|
|
|
380
380
|
owner: Account | Group;
|
|
381
381
|
resolve?: RefsToResolveStrict<L, R>;
|
|
382
382
|
},
|
|
383
|
-
): Promise<
|
|
383
|
+
): Promise<Settled<Resolved<L, R>>> {
|
|
384
384
|
const header = CoList._getUniqueHeader(
|
|
385
385
|
options.unique,
|
|
386
386
|
options.owner.$jazz.id,
|
|
@@ -420,7 +420,7 @@ export class CoList<out Item = any>
|
|
|
420
420
|
resolve?: RefsToResolveStrict<L, R>;
|
|
421
421
|
loadAs?: Account | AnonymousJazzAgent;
|
|
422
422
|
},
|
|
423
|
-
): Promise<
|
|
423
|
+
): Promise<Settled<Resolved<L, R>>> {
|
|
424
424
|
const header = CoList._getUniqueHeader(unique, ownerID);
|
|
425
425
|
|
|
426
426
|
const owner = await Group.load(ownerID, {
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
getCoValueOwner,
|
|
19
19
|
Group,
|
|
20
20
|
ID,
|
|
21
|
-
|
|
21
|
+
Settled,
|
|
22
22
|
PartialOnUndefined,
|
|
23
23
|
RefEncoded,
|
|
24
24
|
RefIfCoValue,
|
|
@@ -377,7 +377,7 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
|
377
377
|
loadAs?: Account | AnonymousJazzAgent;
|
|
378
378
|
skipRetry?: boolean;
|
|
379
379
|
},
|
|
380
|
-
): Promise<
|
|
380
|
+
): Promise<Settled<Resolved<M, R>>> {
|
|
381
381
|
return loadCoValueWithoutMe(this, id, options);
|
|
382
382
|
}
|
|
383
383
|
|
|
@@ -494,7 +494,7 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
|
494
494
|
owner: Account | Group;
|
|
495
495
|
resolve?: RefsToResolveStrict<M, R>;
|
|
496
496
|
},
|
|
497
|
-
): Promise<
|
|
497
|
+
): Promise<Settled<Resolved<M, R>>> {
|
|
498
498
|
const header = CoMap._getUniqueHeader(
|
|
499
499
|
options.unique,
|
|
500
500
|
options.owner.$jazz.id,
|
|
@@ -536,7 +536,7 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
|
536
536
|
resolve?: RefsToResolveStrict<M, R>;
|
|
537
537
|
loadAs?: Account | AnonymousJazzAgent;
|
|
538
538
|
},
|
|
539
|
-
): Promise<
|
|
539
|
+
): Promise<Settled<Resolved<M, R>>> {
|
|
540
540
|
const header = CoMap._getUniqueHeader(unique, ownerID);
|
|
541
541
|
|
|
542
542
|
const owner = await Group.load(ownerID, {
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
CoValueJazzApi,
|
|
8
8
|
CoValueLoadingState,
|
|
9
9
|
ID,
|
|
10
|
-
|
|
10
|
+
Settled,
|
|
11
11
|
Resolved,
|
|
12
12
|
SubscribeListenerOptions,
|
|
13
13
|
SubscribeRestArgs,
|
|
@@ -164,7 +164,7 @@ export class CoPlainText extends String implements CoValue {
|
|
|
164
164
|
this: CoValueClass<T>,
|
|
165
165
|
id: ID<T>,
|
|
166
166
|
options?: { loadAs?: Account | AnonymousJazzAgent },
|
|
167
|
-
): Promise<
|
|
167
|
+
): Promise<Settled<T>> {
|
|
168
168
|
return loadCoValueWithoutMe(this, id, options);
|
|
169
169
|
}
|
|
170
170
|
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
SubscribeListenerOptions,
|
|
12
12
|
SubscribeRestArgs,
|
|
13
13
|
TypeSym,
|
|
14
|
-
|
|
14
|
+
Settled,
|
|
15
15
|
Account,
|
|
16
16
|
CoValueJazzApi,
|
|
17
17
|
inspect,
|
|
@@ -232,7 +232,7 @@ export class CoVector
|
|
|
232
232
|
options?: {
|
|
233
233
|
loadAs?: Account | AnonymousJazzAgent;
|
|
234
234
|
},
|
|
235
|
-
): Promise<
|
|
235
|
+
): Promise<Settled<C>> {
|
|
236
236
|
const coVector = await loadCoValueWithoutMe(this, id, options);
|
|
237
237
|
|
|
238
238
|
/**
|
|
@@ -31,9 +31,16 @@ type IsUnion<T, U = T> = (
|
|
|
31
31
|
// into the definition of CoValues.
|
|
32
32
|
export type MaybeLoaded<T> = T | NotLoaded<T>;
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A CoValue that is either successfully loaded or that could not be loaded.
|
|
36
|
+
*/
|
|
37
|
+
export type Settled<T> = T | Inaccessible<T>;
|
|
38
|
+
|
|
34
39
|
/**
|
|
35
40
|
* A CoValue that is not loaded.
|
|
36
41
|
*/
|
|
42
|
+
// Manually inlining the type to reduce type-checking complexity
|
|
43
|
+
// type NotLoaded<T> = Loading<T> | Inaccessible<T>;
|
|
37
44
|
export type NotLoaded<T> = {
|
|
38
45
|
$jazz: {
|
|
39
46
|
id: ID<T>;
|
|
@@ -45,6 +52,30 @@ export type NotLoaded<T> = {
|
|
|
45
52
|
$isLoaded: false;
|
|
46
53
|
};
|
|
47
54
|
|
|
55
|
+
/**
|
|
56
|
+
* A CoValue that is being loaded
|
|
57
|
+
*/
|
|
58
|
+
export type Loading<T> = {
|
|
59
|
+
$jazz: {
|
|
60
|
+
id: ID<T>;
|
|
61
|
+
loadingState: typeof CoValueLoadingState.LOADING;
|
|
62
|
+
};
|
|
63
|
+
$isLoaded: false;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A CoValue that could not be loaded
|
|
68
|
+
*/
|
|
69
|
+
export type Inaccessible<T> = {
|
|
70
|
+
$jazz: {
|
|
71
|
+
id: ID<T>;
|
|
72
|
+
loadingState:
|
|
73
|
+
| typeof CoValueLoadingState.UNAVAILABLE
|
|
74
|
+
| typeof CoValueLoadingState.UNAUTHORIZED;
|
|
75
|
+
};
|
|
76
|
+
$isLoaded: false;
|
|
77
|
+
};
|
|
78
|
+
|
|
48
79
|
/**
|
|
49
80
|
* Narrows a maybe-loaded, optional CoValue to a loaded and required CoValue.
|
|
50
81
|
*/
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
CoValue,
|
|
15
15
|
CoValueClass,
|
|
16
16
|
ID,
|
|
17
|
-
|
|
17
|
+
Settled,
|
|
18
18
|
RefEncoded,
|
|
19
19
|
RefsToResolve,
|
|
20
20
|
RefsToResolveStrict,
|
|
@@ -283,7 +283,7 @@ export class Group extends CoValueBase implements CoValue {
|
|
|
283
283
|
resolve?: RefsToResolveStrict<G, R>;
|
|
284
284
|
loadAs?: Account | AnonymousJazzAgent;
|
|
285
285
|
},
|
|
286
|
-
): Promise<
|
|
286
|
+
): Promise<Settled<Resolved<G, R>>> {
|
|
287
287
|
return loadCoValueWithoutMe(this, id, options);
|
|
288
288
|
}
|
|
289
289
|
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
type CojsonInternalTypes,
|
|
5
5
|
type RawCoValue,
|
|
6
6
|
} from "cojson";
|
|
7
|
-
import { AvailableCoValueCore } from "cojson
|
|
7
|
+
import { AvailableCoValueCore } from "cojson";
|
|
8
8
|
import {
|
|
9
9
|
Account,
|
|
10
10
|
AnonymousJazzAgent,
|
|
@@ -13,7 +13,9 @@ import {
|
|
|
13
13
|
NotLoadedCoValueState,
|
|
14
14
|
type Group,
|
|
15
15
|
Loaded,
|
|
16
|
+
Inaccessible,
|
|
16
17
|
MaybeLoaded,
|
|
18
|
+
Settled,
|
|
17
19
|
RefsToResolve,
|
|
18
20
|
RefsToResolveStrict,
|
|
19
21
|
RegisteredSchemas,
|
|
@@ -21,7 +23,6 @@ import {
|
|
|
21
23
|
ResolveQueryStrict,
|
|
22
24
|
Resolved,
|
|
23
25
|
SubscriptionScope,
|
|
24
|
-
type SubscriptionValue,
|
|
25
26
|
TypeSym,
|
|
26
27
|
NotLoaded,
|
|
27
28
|
activeAccountContext,
|
|
@@ -29,7 +30,7 @@ import {
|
|
|
29
30
|
inspect,
|
|
30
31
|
} from "../internal.js";
|
|
31
32
|
import type { BranchDefinition } from "../subscribe/types.js";
|
|
32
|
-
import { CoValueHeader } from "cojson
|
|
33
|
+
import { CoValueHeader } from "cojson";
|
|
33
34
|
|
|
34
35
|
/** @category Abstract interfaces */
|
|
35
36
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -145,7 +146,7 @@ export function loadCoValueWithoutMe<
|
|
|
145
146
|
skipRetry?: boolean;
|
|
146
147
|
unstable_branch?: BranchDefinition;
|
|
147
148
|
},
|
|
148
|
-
): Promise<
|
|
149
|
+
): Promise<Settled<Resolved<V, R>>> {
|
|
149
150
|
return loadCoValue(cls, id, {
|
|
150
151
|
...options,
|
|
151
152
|
loadAs: options?.loadAs ?? activeAccountContext.get(),
|
|
@@ -165,7 +166,7 @@ export function loadCoValue<
|
|
|
165
166
|
skipRetry?: boolean;
|
|
166
167
|
unstable_branch?: BranchDefinition;
|
|
167
168
|
},
|
|
168
|
-
): Promise<
|
|
169
|
+
): Promise<Settled<Resolved<V, R>>> {
|
|
169
170
|
return new Promise((resolve) => {
|
|
170
171
|
subscribeToCoValue<V, R>(
|
|
171
172
|
cls,
|
|
@@ -302,8 +303,8 @@ export function subscribeToCoValue<
|
|
|
302
303
|
options: {
|
|
303
304
|
resolve?: RefsToResolveStrict<V, R>;
|
|
304
305
|
loadAs: Account | AnonymousJazzAgent;
|
|
305
|
-
onUnavailable?: (value:
|
|
306
|
-
onUnauthorized?: (value:
|
|
306
|
+
onUnavailable?: (value: Inaccessible<V>) => void;
|
|
307
|
+
onUnauthorized?: (value: Inaccessible<V>) => void;
|
|
307
308
|
syncResolution?: boolean;
|
|
308
309
|
skipRetry?: boolean;
|
|
309
310
|
unstable_branch?: BranchDefinition;
|
|
@@ -342,16 +343,10 @@ export function subscribeToCoValue<
|
|
|
342
343
|
|
|
343
344
|
switch (value.$jazz.loadingState) {
|
|
344
345
|
case CoValueLoadingState.UNAVAILABLE:
|
|
345
|
-
options.onUnavailable?.(value);
|
|
346
|
-
|
|
347
|
-
// Don't log unavailable errors when `loadUnique` or `upsertUnique` are used
|
|
348
|
-
if (!options.skipRetry) {
|
|
349
|
-
console.error(value.toString());
|
|
350
|
-
}
|
|
346
|
+
options.onUnavailable?.(value as Inaccessible<V>);
|
|
351
347
|
break;
|
|
352
348
|
case CoValueLoadingState.UNAUTHORIZED:
|
|
353
|
-
options.onUnauthorized?.(value);
|
|
354
|
-
console.error(value.toString());
|
|
349
|
+
options.onUnauthorized?.(value as Inaccessible<V>);
|
|
355
350
|
break;
|
|
356
351
|
}
|
|
357
352
|
};
|
|
@@ -546,7 +541,7 @@ export async function internalLoadUnique<
|
|
|
546
541
|
owner: Account | Group;
|
|
547
542
|
resolve?: RefsToResolveStrict<V, R>;
|
|
548
543
|
},
|
|
549
|
-
): Promise<
|
|
544
|
+
): Promise<Settled<Resolved<V, R>>> {
|
|
550
545
|
const loadAs = options.owner.$jazz.loadedAs;
|
|
551
546
|
|
|
552
547
|
const node =
|
|
@@ -682,18 +677,18 @@ export async function exportCoValue<
|
|
|
682
677
|
);
|
|
683
678
|
|
|
684
679
|
const value = await new Promise<Loaded<S, R> | null>((resolve) => {
|
|
685
|
-
rootNode.setListener((
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
680
|
+
rootNode.setListener(() => {
|
|
681
|
+
const value = rootNode.getCurrentValue();
|
|
682
|
+
|
|
683
|
+
if (value.$isLoaded) {
|
|
684
|
+
resolve(value as Loaded<S, R>);
|
|
685
|
+
} else if (
|
|
686
|
+
value.$jazz.loadingState === CoValueLoadingState.UNAVAILABLE ||
|
|
687
|
+
value.$jazz.loadingState === CoValueLoadingState.UNAUTHORIZED
|
|
688
|
+
) {
|
|
690
689
|
resolve(null);
|
|
691
|
-
|
|
692
|
-
} else if (value.type === CoValueLoadingState.LOADED) {
|
|
693
|
-
resolve(value.value as Loaded<S, R>);
|
|
690
|
+
rootNode.destroy();
|
|
694
691
|
}
|
|
695
|
-
|
|
696
|
-
rootNode.destroy();
|
|
697
692
|
});
|
|
698
693
|
});
|
|
699
694
|
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
CoValueJazzApi,
|
|
11
11
|
Group,
|
|
12
12
|
ID,
|
|
13
|
-
|
|
13
|
+
Settled,
|
|
14
14
|
RefsToResolve,
|
|
15
15
|
RefsToResolveStrict,
|
|
16
16
|
Resolved,
|
|
@@ -162,7 +162,7 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
|
|
|
162
162
|
loadAs?: Account | AnonymousJazzAgent;
|
|
163
163
|
skipRetry?: boolean;
|
|
164
164
|
},
|
|
165
|
-
): Promise<
|
|
165
|
+
): Promise<Settled<Resolved<M, R>>> {
|
|
166
166
|
return loadCoValueWithoutMe(this, id, options);
|
|
167
167
|
}
|
|
168
168
|
|
package/src/tools/exports.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { JazzContextType } from "../types.js";
|
|
|
9
9
|
import { AnonymousJazzAgent } from "./anonymousJazzAgent.js";
|
|
10
10
|
import { createAnonymousJazzContext } from "./createContext.js";
|
|
11
11
|
import { InstanceOfSchema } from "./zodSchema/typeConverters/InstanceOfSchema.js";
|
|
12
|
+
import { SubscriptionCache } from "../subscribe/SubscriptionCache.js";
|
|
12
13
|
|
|
13
14
|
export type JazzContextManagerAuthProps = {
|
|
14
15
|
credentials?: AuthCredentials;
|
|
@@ -75,6 +76,7 @@ export class JazzContextManager<
|
|
|
75
76
|
protected keepContextOpen = false;
|
|
76
77
|
contextPromise: Promise<void> | undefined;
|
|
77
78
|
protected authenticatingAccountID: string | null = null;
|
|
79
|
+
private subscriptionCache: SubscriptionCache;
|
|
78
80
|
|
|
79
81
|
constructor(opts?: {
|
|
80
82
|
useAnonymousFallback?: boolean;
|
|
@@ -82,6 +84,7 @@ export class JazzContextManager<
|
|
|
82
84
|
}) {
|
|
83
85
|
KvStoreContext.getInstance().initialize(this.getKvStore());
|
|
84
86
|
this.authSecretStorage = new AuthSecretStorage(opts?.authSecretStorageKey);
|
|
87
|
+
this.subscriptionCache = new SubscriptionCache();
|
|
85
88
|
|
|
86
89
|
if (opts?.useAnonymousFallback) {
|
|
87
90
|
this.value = getAnonymousFallback();
|
|
@@ -130,6 +133,9 @@ export class JazzContextManager<
|
|
|
130
133
|
context: PlatformSpecificContext<Acc>,
|
|
131
134
|
authProps?: JazzContextManagerAuthProps,
|
|
132
135
|
) {
|
|
136
|
+
// Clear cache before updating context to prevent subscription leaks across authentication boundaries
|
|
137
|
+
this.subscriptionCache.clear();
|
|
138
|
+
|
|
133
139
|
// When keepContextOpen we don't want to close the previous context
|
|
134
140
|
// because we might need to handle the onAnonymousAccountDiscarded callback
|
|
135
141
|
if (!this.keepContextOpen) {
|
|
@@ -178,6 +184,10 @@ export class JazzContextManager<
|
|
|
178
184
|
return this.authenticatingAccountID;
|
|
179
185
|
}
|
|
180
186
|
|
|
187
|
+
getSubscriptionScopeCache(): SubscriptionCache {
|
|
188
|
+
return this.subscriptionCache;
|
|
189
|
+
}
|
|
190
|
+
|
|
181
191
|
logOut = async () => {
|
|
182
192
|
if (!this.context || !this.props) {
|
|
183
193
|
return;
|
|
@@ -185,6 +195,9 @@ export class JazzContextManager<
|
|
|
185
195
|
|
|
186
196
|
this.authenticatingAccountID = null;
|
|
187
197
|
|
|
198
|
+
// Clear cache on logout to prevent subscription leaks across authentication boundaries
|
|
199
|
+
this.subscriptionCache.clear();
|
|
200
|
+
|
|
188
201
|
await this.props.onLogOut?.();
|
|
189
202
|
|
|
190
203
|
if (this.props.logOutReplacement) {
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
CoMapSchemaDefinition,
|
|
6
6
|
coOptionalDefiner,
|
|
7
7
|
Group,
|
|
8
|
-
|
|
8
|
+
Settled,
|
|
9
9
|
RefsToResolveStrict,
|
|
10
10
|
RefsToResolve,
|
|
11
11
|
Resolved,
|
|
@@ -84,7 +84,7 @@ export class AccountSchema<
|
|
|
84
84
|
loadAs?: Account | AnonymousJazzAgent;
|
|
85
85
|
resolve?: RefsToResolveStrict<AccountSchema<Shape>, R>;
|
|
86
86
|
},
|
|
87
|
-
): Promise<
|
|
87
|
+
): Promise<Settled<Loaded<AccountSchema<Shape>, R>>> {
|
|
88
88
|
// @ts-expect-error
|
|
89
89
|
return this.coValueClass.load(
|
|
90
90
|
id,
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
BranchDefinition,
|
|
6
6
|
InstanceOfSchema,
|
|
7
7
|
InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded,
|
|
8
|
-
|
|
8
|
+
Settled,
|
|
9
9
|
RefsToResolve,
|
|
10
10
|
RefsToResolveStrict,
|
|
11
11
|
Resolved,
|
|
@@ -87,7 +87,7 @@ export class CoDiscriminatedUnionSchema<
|
|
|
87
87
|
unstable_branch?: BranchDefinition;
|
|
88
88
|
},
|
|
89
89
|
): Promise<
|
|
90
|
-
|
|
90
|
+
Settled<
|
|
91
91
|
Resolved<
|
|
92
92
|
CoDiscriminatedUnionInstanceCoValuesMaybeLoaded<Options> & SchemaUnion,
|
|
93
93
|
R
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
BranchDefinition,
|
|
5
5
|
CoFeed,
|
|
6
6
|
Group,
|
|
7
|
-
|
|
7
|
+
Settled,
|
|
8
8
|
RefsToResolve,
|
|
9
9
|
RefsToResolveStrict,
|
|
10
10
|
Resolved,
|
|
@@ -69,7 +69,7 @@ export class CoFeedSchema<
|
|
|
69
69
|
loadAs?: Account | AnonymousJazzAgent;
|
|
70
70
|
unstable_branch?: BranchDefinition;
|
|
71
71
|
},
|
|
72
|
-
): Promise<
|
|
72
|
+
): Promise<Settled<Resolved<CoFeedInstanceCoValuesMaybeLoaded<T>, R>>> {
|
|
73
73
|
// @ts-expect-error
|
|
74
74
|
return this.coValueClass.load(
|
|
75
75
|
id,
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
CoList,
|
|
5
5
|
Group,
|
|
6
6
|
ID,
|
|
7
|
-
|
|
7
|
+
Settled,
|
|
8
8
|
RefsToResolve,
|
|
9
9
|
RefsToResolveStrict,
|
|
10
10
|
Resolved,
|
|
@@ -77,7 +77,7 @@ export class CoListSchema<
|
|
|
77
77
|
loadAs?: Account | AnonymousJazzAgent;
|
|
78
78
|
unstable_branch?: BranchDefinition;
|
|
79
79
|
},
|
|
80
|
-
): Promise<
|
|
80
|
+
): Promise<Settled<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
|
|
81
81
|
// @ts-expect-error
|
|
82
82
|
return this.coValueClass.load(
|
|
83
83
|
id,
|
|
@@ -147,7 +147,7 @@ export class CoListSchema<
|
|
|
147
147
|
unique: CoValueUniqueness["uniqueness"];
|
|
148
148
|
owner: Account | Group;
|
|
149
149
|
resolve?: RefsToResolveStrict<CoListInstanceCoValuesMaybeLoaded<T>, R>;
|
|
150
|
-
}): Promise<
|
|
150
|
+
}): Promise<Settled<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
|
|
151
151
|
// @ts-expect-error
|
|
152
152
|
return this.coValueClass.upsertUnique(
|
|
153
153
|
// @ts-expect-error
|
|
@@ -166,7 +166,7 @@ export class CoListSchema<
|
|
|
166
166
|
resolve?: RefsToResolveStrict<CoListInstanceCoValuesMaybeLoaded<T>, R>;
|
|
167
167
|
loadAs?: Account | AnonymousJazzAgent;
|
|
168
168
|
},
|
|
169
|
-
): Promise<
|
|
169
|
+
): Promise<Settled<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
|
|
170
170
|
// @ts-expect-error
|
|
171
171
|
return this.coValueClass.loadUnique(
|
|
172
172
|
unique,
|