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.
Files changed (167) hide show
  1. package/.turbo/turbo-build.log +65 -59
  2. package/CHANGELOG.md +34 -3
  3. package/dist/{chunk-CUS6O5NE.js → chunk-FFEEPZEG.js} +454 -122
  4. package/dist/chunk-FFEEPZEG.js.map +1 -0
  5. package/dist/expo/polyfills.js +22 -0
  6. package/dist/expo/polyfills.js.map +1 -0
  7. package/dist/index.js +26 -6
  8. package/dist/index.js.map +1 -1
  9. package/dist/react/hooks.d.ts +1 -1
  10. package/dist/react/hooks.d.ts.map +1 -1
  11. package/dist/react/index.d.ts +1 -1
  12. package/dist/react/index.d.ts.map +1 -1
  13. package/dist/react/index.js +5 -1
  14. package/dist/react/index.js.map +1 -1
  15. package/dist/react-core/hooks.d.ts +59 -0
  16. package/dist/react-core/hooks.d.ts.map +1 -1
  17. package/dist/react-core/index.js +133 -34
  18. package/dist/react-core/index.js.map +1 -1
  19. package/dist/react-core/tests/testUtils.d.ts +1 -0
  20. package/dist/react-core/tests/testUtils.d.ts.map +1 -1
  21. package/dist/react-core/tests/useSuspenseAccount.test.d.ts +2 -0
  22. package/dist/react-core/tests/useSuspenseAccount.test.d.ts.map +1 -0
  23. package/dist/react-core/tests/useSuspenseCoState.test.d.ts +2 -0
  24. package/dist/react-core/tests/useSuspenseCoState.test.d.ts.map +1 -0
  25. package/dist/react-core/use.d.ts +3 -0
  26. package/dist/react-core/use.d.ts.map +1 -0
  27. package/dist/react-native/index.d.ts +1 -1
  28. package/dist/react-native/index.d.ts.map +1 -1
  29. package/dist/react-native/index.js +717 -9
  30. package/dist/react-native/index.js.map +1 -1
  31. package/dist/react-native/polyfills.js +22 -0
  32. package/dist/react-native/polyfills.js.map +1 -0
  33. package/dist/react-native-core/crypto/RNCrypto.d.ts +2 -0
  34. package/dist/react-native-core/crypto/RNCrypto.d.ts.map +1 -0
  35. package/dist/react-native-core/crypto/RNCrypto.js +3 -0
  36. package/dist/react-native-core/crypto/RNCrypto.js.map +1 -0
  37. package/dist/react-native-core/hooks.d.ts +1 -1
  38. package/dist/react-native-core/hooks.d.ts.map +1 -1
  39. package/dist/react-native-core/index.d.ts.map +1 -1
  40. package/dist/react-native-core/index.js +5 -1
  41. package/dist/react-native-core/index.js.map +1 -1
  42. package/dist/react-native-core/platform.d.ts +2 -1
  43. package/dist/react-native-core/platform.d.ts.map +1 -1
  44. package/dist/testing.js +1 -1
  45. package/dist/testing.js.map +1 -1
  46. package/dist/tools/coValues/account.d.ts +3 -3
  47. package/dist/tools/coValues/account.d.ts.map +1 -1
  48. package/dist/tools/coValues/coFeed.d.ts +3 -3
  49. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  50. package/dist/tools/coValues/coList.d.ts +4 -4
  51. package/dist/tools/coValues/coList.d.ts.map +1 -1
  52. package/dist/tools/coValues/coMap.d.ts +7 -7
  53. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  54. package/dist/tools/coValues/coPlainText.d.ts +2 -2
  55. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  56. package/dist/tools/coValues/coVector.d.ts +2 -2
  57. package/dist/tools/coValues/coVector.d.ts.map +1 -1
  58. package/dist/tools/coValues/deepLoading.d.ts +24 -0
  59. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  60. package/dist/tools/coValues/group.d.ts +2 -2
  61. package/dist/tools/coValues/group.d.ts.map +1 -1
  62. package/dist/tools/coValues/interfaces.d.ts +7 -7
  63. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  64. package/dist/tools/coValues/schemaUnion.d.ts +2 -2
  65. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  66. package/dist/tools/config.d.ts +3 -0
  67. package/dist/tools/config.d.ts.map +1 -0
  68. package/dist/tools/exports.d.ts +2 -0
  69. package/dist/tools/exports.d.ts.map +1 -1
  70. package/dist/tools/implementation/ContextManager.d.ts +3 -0
  71. package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
  72. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
  73. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +2 -2
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -2
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +4 -4
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +4 -4
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +4 -4
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  84. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +2 -2
  85. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  86. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts +2 -2
  87. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts.map +1 -1
  88. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +2 -2
  89. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  90. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +2 -2
  91. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  92. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  93. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +8 -22
  94. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  95. package/dist/tools/subscribe/JazzError.d.ts.map +1 -1
  96. package/dist/tools/subscribe/SubscriptionCache.d.ts +51 -0
  97. package/dist/tools/subscribe/SubscriptionCache.d.ts.map +1 -0
  98. package/dist/tools/subscribe/SubscriptionScope.d.ts +27 -2
  99. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  100. package/dist/tools/subscribe/errorReporting.d.ts +31 -0
  101. package/dist/tools/subscribe/errorReporting.d.ts.map +1 -0
  102. package/dist/tools/subscribe/utils.d.ts +9 -1
  103. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  104. package/dist/tools/testing.d.ts +2 -2
  105. package/dist/tools/testing.d.ts.map +1 -1
  106. package/dist/tools/tests/SubscriptionCache.test.d.ts +2 -0
  107. package/dist/tools/tests/SubscriptionCache.test.d.ts.map +1 -0
  108. package/dist/tools/tests/errorReporting.test.d.ts +2 -0
  109. package/dist/tools/tests/errorReporting.test.d.ts.map +1 -0
  110. package/package.json +22 -7
  111. package/src/react/hooks.tsx +2 -0
  112. package/src/react/index.ts +1 -14
  113. package/src/react-core/hooks.ts +181 -16
  114. package/src/react-core/tests/createCoValueSubscriptionContext.test.tsx +18 -8
  115. package/src/react-core/tests/testUtils.tsx +67 -5
  116. package/src/react-core/tests/useCoState.test.ts +3 -7
  117. package/src/react-core/tests/useSubscriptionSelector.test.ts +3 -7
  118. package/src/react-core/tests/useSuspenseAccount.test.tsx +343 -0
  119. package/src/react-core/tests/useSuspenseCoState.test.tsx +1182 -0
  120. package/src/react-core/use.ts +46 -0
  121. package/src/react-native/index.ts +1 -1
  122. package/src/react-native-core/crypto/RNCrypto.ts +1 -0
  123. package/src/react-native-core/hooks.tsx +2 -0
  124. package/src/react-native-core/index.ts +2 -0
  125. package/src/react-native-core/platform.ts +2 -1
  126. package/src/react-native-core/polyfills/index.js +28 -0
  127. package/src/tools/coValues/account.ts +3 -4
  128. package/src/tools/coValues/coFeed.ts +3 -2
  129. package/src/tools/coValues/coList.ts +4 -4
  130. package/src/tools/coValues/coMap.ts +4 -4
  131. package/src/tools/coValues/coPlainText.ts +2 -2
  132. package/src/tools/coValues/coVector.ts +2 -2
  133. package/src/tools/coValues/deepLoading.ts +31 -0
  134. package/src/tools/coValues/group.ts +2 -2
  135. package/src/tools/coValues/interfaces.ts +21 -26
  136. package/src/tools/coValues/schemaUnion.ts +2 -2
  137. package/src/tools/config.ts +9 -0
  138. package/src/tools/exports.ts +4 -0
  139. package/src/tools/implementation/ContextManager.ts +13 -0
  140. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
  141. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +2 -2
  142. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -2
  143. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +4 -4
  144. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +4 -4
  145. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +4 -10
  146. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +2 -2
  147. package/src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts +2 -2
  148. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +2 -2
  149. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +2 -2
  150. package/src/tools/subscribe/CoValueCoreSubscription.ts +71 -100
  151. package/src/tools/subscribe/JazzError.ts +9 -6
  152. package/src/tools/subscribe/SubscriptionCache.ts +272 -0
  153. package/src/tools/subscribe/SubscriptionScope.ts +218 -29
  154. package/src/tools/subscribe/errorReporting.ts +67 -0
  155. package/src/tools/subscribe/utils.ts +77 -0
  156. package/src/tools/testing.ts +0 -3
  157. package/src/tools/tests/CoValueCoreSubscription.test.ts +46 -12
  158. package/src/tools/tests/ContextManager.test.ts +85 -0
  159. package/src/tools/tests/SubscriptionCache.test.ts +237 -0
  160. package/src/tools/tests/coMap.test.ts +5 -7
  161. package/src/tools/tests/deepLoading.test.ts +47 -47
  162. package/src/tools/tests/errorReporting.test.ts +103 -0
  163. package/src/tools/tests/load.test.ts +21 -1
  164. package/src/tools/tests/request.test.ts +2 -1
  165. package/src/tools/tests/subscribe.test.ts +44 -0
  166. package/tsup.config.ts +17 -0
  167. 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
+ });
@@ -1,4 +1,4 @@
1
- export * from "jazz-tools/react-native-core";
1
+ export * from "../react-native-core/index.js";
2
2
 
3
3
  export * from "./provider.js";
4
4
  export * from "./storage/mmkv-store-adapter.js";
@@ -0,0 +1 @@
1
+ export * from "cojson/crypto/RNCrypto";
@@ -19,6 +19,8 @@ export {
19
19
  useCoValueSubscription,
20
20
  useAccountSubscription,
21
21
  useSubscriptionSelector,
22
+ useSuspenseCoState,
23
+ useSuspenseAccount,
22
24
  } from "jazz-tools/react-core";
23
25
 
24
26
  export function useAcceptInviteNative<S extends CoValueClassOrSchema>({
@@ -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<MaybeLoaded<Resolved<A, R>>> {
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<MaybeLoaded<Resolved<F, R>>> {
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<MaybeLoaded<FileStream>> {
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<L, R>>> {
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<MaybeLoaded<Resolved<L, R>>> {
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<MaybeLoaded<Resolved<L, R>>> {
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<M, R>>> {
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<MaybeLoaded<Resolved<M, R>>> {
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<MaybeLoaded<Resolved<M, R>>> {
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
- MaybeLoaded,
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<MaybeLoaded<T>> {
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
- MaybeLoaded,
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<MaybeLoaded<C>> {
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<G, R>>> {
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/dist/coValueCore/coValueCore.js";
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/dist/coValueCore/verifiedState.js";
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<MaybeLoaded<Resolved<V, R>>> {
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<MaybeLoaded<Resolved<V, R>>> {
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: NotLoaded<V>) => void;
306
- onUnauthorized?: (value: NotLoaded<V>) => void;
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<MaybeLoaded<Resolved<V, R>>> {
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((value) => {
686
- if (value.type === CoValueLoadingState.UNAVAILABLE) {
687
- resolve(null);
688
- console.error(value.toString());
689
- } else if (value.type === CoValueLoadingState.UNAUTHORIZED) {
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
- console.error(value.toString());
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<M, R>>> {
165
+ ): Promise<Settled<Resolved<M, R>>> {
166
166
  return loadCoValueWithoutMe(this, id, options);
167
167
  }
168
168
 
@@ -0,0 +1,9 @@
1
+ export {
2
+ setCustomErrorReporter,
3
+ enableCaptureErrorCause,
4
+ } from "./subscribe/errorReporting.js";
5
+
6
+ export {
7
+ logger,
8
+ LogLevel,
9
+ } from "cojson";
@@ -137,3 +137,7 @@ export {
137
137
  } from "./coValues/request.js";
138
138
 
139
139
  export * from "./ssr/index.js";
140
+
141
+ export { captureStack } from "./subscribe/errorReporting.js";
142
+
143
+ export * as jazzConfig from "./config.js";
@@ -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
- MaybeLoaded,
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<MaybeLoaded<Loaded<AccountSchema<Shape>, R>>> {
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
- MaybeLoaded,
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
- MaybeLoaded<
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<CoFeedInstanceCoValuesMaybeLoaded<T>, R>>> {
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
- MaybeLoaded,
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<MaybeLoaded<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
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<MaybeLoaded<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
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<MaybeLoaded<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
169
+ ): Promise<Settled<Resolved<CoListInstanceCoValuesMaybeLoaded<T>, R>>> {
170
170
  // @ts-expect-error
171
171
  return this.coValueClass.loadUnique(
172
172
  unique,