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
@@ -6,7 +6,7 @@ import {
6
6
  DiscriminableCoValueSchemaDefinition,
7
7
  DiscriminableCoreCoValueSchema,
8
8
  Group,
9
- MaybeLoaded,
9
+ Settled,
10
10
  RefsToResolve,
11
11
  RefsToResolveStrict,
12
12
  Resolved,
@@ -97,7 +97,7 @@ export class CoMapSchema<
97
97
  unstable_branch?: BranchDefinition;
98
98
  },
99
99
  ): Promise<
100
- MaybeLoaded<
100
+ Settled<
101
101
  Resolved<Simplify<CoMapInstanceCoValuesMaybeLoaded<Shape>> & CoMap, R>
102
102
  >
103
103
  > {
@@ -183,7 +183,7 @@ export class CoMapSchema<
183
183
  R
184
184
  >;
185
185
  }): Promise<
186
- MaybeLoaded<
186
+ Settled<
187
187
  Resolved<Simplify<CoMapInstanceCoValuesMaybeLoaded<Shape>> & CoMap, R>
188
188
  >
189
189
  > {
@@ -210,7 +210,7 @@ export class CoMapSchema<
210
210
  loadAs?: Account | AnonymousJazzAgent;
211
211
  },
212
212
  ): Promise<
213
- MaybeLoaded<
213
+ Settled<
214
214
  Resolved<Simplify<CoMapInstanceCoValuesMaybeLoaded<Shape>> & CoMap, R>
215
215
  >
216
216
  > {
@@ -6,7 +6,7 @@ import {
6
6
  CoMapSchemaDefinition,
7
7
  Group,
8
8
  ID,
9
- MaybeLoaded,
9
+ Settled,
10
10
  RefsToResolve,
11
11
  RefsToResolveStrict,
12
12
  Resolved,
@@ -64,9 +64,7 @@ export interface CoRecordSchema<
64
64
  loadAs?: Account | AnonymousJazzAgent;
65
65
  unstable_branch?: BranchDefinition;
66
66
  },
67
- ): Promise<
68
- MaybeLoaded<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>
69
- >;
67
+ ): Promise<Settled<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>>;
70
68
 
71
69
  unstable_merge<
72
70
  const R extends RefsToResolve<
@@ -119,9 +117,7 @@ export interface CoRecordSchema<
119
117
  unique: CoValueUniqueness["uniqueness"];
120
118
  owner: Account | Group;
121
119
  resolve?: RefsToResolveStrict<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>;
122
- }): Promise<
123
- MaybeLoaded<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>
124
- >;
120
+ }): Promise<Settled<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>>;
125
121
 
126
122
  loadUnique<
127
123
  const R extends RefsToResolve<
@@ -138,9 +134,7 @@ export interface CoRecordSchema<
138
134
  >;
139
135
  loadAs?: Account | AnonymousJazzAgent;
140
136
  },
141
- ): Promise<
142
- MaybeLoaded<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>
143
- >;
137
+ ): Promise<Settled<Resolved<CoRecordInstanceCoValuesMaybeLoaded<K, V>, R>>>;
144
138
 
145
139
  getCoValueClass: () => typeof CoMap;
146
140
 
@@ -3,7 +3,7 @@ import {
3
3
  AnonymousJazzAgent,
4
4
  FileStream,
5
5
  Group,
6
- MaybeLoaded,
6
+ Settled,
7
7
  coOptionalDefiner,
8
8
  unstable_mergeBranchWithResolve,
9
9
  } from "../../../internal.js";
@@ -82,7 +82,7 @@ export class FileStreamSchema implements CoreFileStreamSchema {
82
82
  load(
83
83
  id: string,
84
84
  options?: { loadAs?: Account | AnonymousJazzAgent },
85
- ): Promise<MaybeLoaded<FileStream>> {
85
+ ): Promise<Settled<FileStream>> {
86
86
  return this.coValueClass.load(id, options);
87
87
  }
88
88
 
@@ -6,7 +6,7 @@ import {
6
6
  } from "../../../coValues/interfaces.js";
7
7
  import {
8
8
  Account,
9
- MaybeLoaded,
9
+ Settled,
10
10
  RefsToResolve,
11
11
  RefsToResolveStrict,
12
12
  Resolved,
@@ -52,7 +52,7 @@ export class GroupSchema implements CoreGroupSchema {
52
52
  loadAs?: Account;
53
53
  resolve?: RefsToResolveStrict<Group, R>;
54
54
  },
55
- ): Promise<MaybeLoaded<Group>> {
55
+ ): Promise<Settled<Group>> {
56
56
  return Group.load(id, options);
57
57
  }
58
58
  createInvite<G extends Group>(
@@ -4,7 +4,7 @@ import {
4
4
  BranchDefinition,
5
5
  CoPlainText,
6
6
  Group,
7
- MaybeLoaded,
7
+ Settled,
8
8
  coOptionalDefiner,
9
9
  unstable_mergeBranchWithResolve,
10
10
  } from "../../../internal.js";
@@ -50,7 +50,7 @@ export class PlainTextSchema implements CorePlainTextSchema {
50
50
  loadAs: Account | AnonymousJazzAgent;
51
51
  unstable_branch?: BranchDefinition;
52
52
  },
53
- ): Promise<MaybeLoaded<CoPlainText>> {
53
+ ): Promise<Settled<CoPlainText>> {
54
54
  return this.coValueClass.load(id, options);
55
55
  }
56
56
 
@@ -3,7 +3,7 @@ import {
3
3
  BranchDefinition,
4
4
  CoRichText,
5
5
  Group,
6
- MaybeLoaded,
6
+ Settled,
7
7
  coOptionalDefiner,
8
8
  unstable_mergeBranchWithResolve,
9
9
  } from "../../../internal.js";
@@ -49,7 +49,7 @@ export class RichTextSchema implements CoreRichTextSchema {
49
49
  loadAs: Account | AnonymousJazzAgent;
50
50
  unstable_branch?: BranchDefinition;
51
51
  },
52
- ): Promise<MaybeLoaded<CoRichText>> {
52
+ ): Promise<Settled<CoRichText>> {
53
53
  return this.coValueClass.load(id, options);
54
54
  }
55
55
 
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  cojsonInternals,
3
3
  CoValueCore,
4
+ isRawCoID,
4
5
  LocalNode,
5
6
  RawCoID,
6
7
  RawCoValue,
@@ -67,121 +68,76 @@ export class CoValueCoreSubscription {
67
68
  private initializeSubscription(): void {
68
69
  const source = this.source;
69
70
 
70
- // If the CoValue is already available, handle it immediately
71
- if (source.isAvailable()) {
72
- this.handleAvailableSource();
71
+ // If the ID is not a valid raw CoID, we immediately emit an unavailable event
72
+ if (!isRawCoID(source.id)) {
73
+ this.emit(CoValueLoadingState.UNAVAILABLE);
73
74
  return;
74
75
  }
75
76
 
76
- // If a specific branch is requested while the source is not available, attempt to checkout that branch
77
+ // If we have a branch name, we handle branching
77
78
  if (this.branchName) {
78
- this.handleBranchCheckout();
79
+ this.handleBranching(this.branchName, this.branchOwnerId);
79
80
  return;
80
81
  }
81
82
 
82
- // If we don't have a branch requested, load the CoValue
83
- this.loadCoValue();
83
+ // If we don't have a branch name, we subscribe to the source directly
84
+ this.subscribe(this.source);
84
85
  }
85
86
 
86
- /**
87
- * Handles the case where the CoValue source is immediately available.
88
- * Either subscribes directly or attempts to get the requested branch.
89
- */
90
- private handleAvailableSource(): void {
91
- if (!this.branchName || !cojsonInternals.canBeBranched(this.source)) {
92
- this.subscribe(this.source.getCurrentContent());
87
+ private handleBranching(branchName: string, branchOwnerId?: RawCoID) {
88
+ const source = this.source;
89
+
90
+ // If the source is not available, we wait for it to become available and then try to branch
91
+ if (!source.isAvailable()) {
92
+ this.waitForSourceToBecomeAvailable(branchName, branchOwnerId);
93
93
  return;
94
94
  }
95
95
 
96
- // Try to get the specific branch from the available source
97
- const branch = this.source.getBranch(this.branchName, this.branchOwnerId);
98
-
99
- if (branch.isAvailable()) {
100
- // Branch is available, subscribe to it
101
- this.subscribe(branch.getCurrentContent());
96
+ // If the source is not branchable (e.g. it is a group), we subscribe to it directly
97
+ if (!cojsonInternals.canBeBranched(source)) {
98
+ this.subscribe(source);
102
99
  return;
103
- // If the branch hasn't been created, we create it directly so we can syncronously subscribe to it
104
- } else if (!this.source.hasBranch(this.branchName, this.branchOwnerId)) {
105
- this.source.createBranch(this.branchName, this.branchOwnerId);
106
- this.subscribe(branch.getCurrentContent());
107
- } else {
108
- // Branch not available, fall through to checkout logic
109
- this.handleBranchCheckout();
110
100
  }
111
- }
112
101
 
113
- /**
114
- * Attempts to checkout a specific branch of the CoValue.
115
- * This is called when the source isn't available but a branch is requested.
116
- */
117
- private handleBranchCheckout(): void {
118
- this.localNode
119
- .checkoutBranch(this.source.id, this.branchName!, this.branchOwnerId)
120
- .then((value) => {
121
- if (this.unsubscribed) return;
122
-
123
- if (value !== CoValueLoadingState.UNAVAILABLE) {
124
- // Branch checkout successful, subscribe to it
125
- this.subscribe(value);
126
- } else {
127
- // Branch checkout failed, handle the error
128
- this.handleUnavailableBranch();
129
- }
130
- })
131
- .catch((error) => {
132
- // Handle unexpected errors during branch checkout
133
- console.error(error);
102
+ // Try to get the specific branch from the available source
103
+ const branch = source.getBranch(branchName, branchOwnerId);
104
+
105
+ // If the branch hasn't been created, we create it directly so we can syncronously subscribe to it
106
+ if (!branch.isAvailable() && !source.hasBranch(branchName, branchOwnerId)) {
107
+ try {
108
+ source.createBranch(branchName, branchOwnerId);
109
+ } catch (error) {
110
+ // If the branch creation fails (provided group is not available), we emit an unavailable event
111
+ console.error("error creating branch", error);
134
112
  this.emit(CoValueLoadingState.UNAVAILABLE);
135
- });
136
- }
137
-
138
- /**
139
- * Handles the case where a branch checkout fails.
140
- * Determines whether to retry or report unavailability.
141
- */
142
- private handleUnavailableBranch(): void {
143
- const source = this.source;
144
- if (source.isAvailable()) {
145
- // This should be impossible - if source is available we can create the branch and it should be available
146
- throw new Error("Branch is unavailable");
113
+ return;
114
+ }
147
115
  }
148
116
 
149
- // Source isn't available either, subscribe to state changes and report unavailability
150
- this.subscribeToUnavailableSource();
151
- this.emit(CoValueLoadingState.UNAVAILABLE);
117
+ this.subscribe(branch);
152
118
  }
153
119
 
154
120
  /**
155
- * Loads the CoValue core from the network/storage.
156
- * This is the fallback strategy when immediate availability fails.
121
+ * Loads a CoValue core and emits an unavailable event if it is still unavailable after the retries.
157
122
  */
158
- private loadCoValue(): void {
123
+ load(value: CoValueCore) {
159
124
  this.localNode
160
- .loadCoValueCore(this.source.id, undefined, this.skipRetry)
161
- .then((value) => {
162
- if (this.unsubscribed) return;
163
-
164
- if (value.isAvailable()) {
165
- // Loading successful, subscribe to the loaded value
166
- this.subscribe(value.getCurrentContent());
167
- } else {
168
- // Loading failed, subscribe to state changes and report unavailability
169
- this.subscribeToUnavailableSource();
125
+ .loadCoValueCore(value.id, undefined, this.skipRetry)
126
+ .then(() => {
127
+ // If after the retries the value is still unavailable, we emit an unavailable event
128
+ if (!value.isAvailable()) {
170
129
  this.emit(CoValueLoadingState.UNAVAILABLE);
171
130
  }
172
- })
173
- .catch((error) => {
174
- // Handle unexpected errors during loading
175
- console.error(error);
176
- this.emit(CoValueLoadingState.UNAVAILABLE);
177
131
  });
178
132
  }
179
133
 
180
134
  /**
181
- * Subscribes to state changes of an unavailable CoValue source.
182
- * This allows the subscription to become active when the source becomes available after a first loading attempt.
135
+ * Waits for the source to become available and then tries to branch.
183
136
  */
184
- private subscribeToUnavailableSource(): void {
137
+ private waitForSourceToBecomeAvailable(
138
+ branchName: string,
139
+ branchOwnerId?: RawCoID,
140
+ ): void {
185
141
  const source = this.source;
186
142
 
187
143
  const handleStateChange = (
@@ -197,41 +153,60 @@ export class CoValueCoreSubscription {
197
153
 
198
154
  unsubFromStateChange();
199
155
 
200
- if (this.branchName) {
201
- // Branch was requested, attempt checkout again
202
- this.handleBranchCheckout();
203
- } else {
204
- // No branch requested, subscribe directly and cleanup state subscription
205
- this.subscribe(source.getCurrentContent());
206
- }
156
+ this.handleBranching(branchName, branchOwnerId);
207
157
  };
208
158
 
209
159
  // Subscribe to state changes and store the unsubscribe function
210
160
  this._unsubscribe = source.subscribe(handleStateChange);
161
+
162
+ this.load(source);
211
163
  }
212
164
 
213
165
  /**
214
166
  * Subscribes to a specific CoValue and notifies the listener.
215
167
  * This is the final step where we actually start receiving updates.
216
168
  */
217
- private subscribe(value: RawCoValue): void {
169
+ private subscribe(value: CoValueCore): void {
218
170
  if (this.unsubscribed) return;
219
171
 
220
172
  // Subscribe to the value and store the unsubscribe function
221
173
  this._unsubscribe = value.subscribe((value) => {
222
- this.emit(value);
174
+ if (value.isAvailable()) {
175
+ this.emit(value.getCurrentContent());
176
+ }
223
177
  });
178
+
179
+ if (!value.isAvailable()) {
180
+ this.load(value);
181
+ }
224
182
  }
225
183
 
184
+ lastState: CoValueLoadingState | undefined;
185
+
226
186
  emit(value: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE): void {
227
187
  if (this.unsubscribed) return;
228
- if (!isReadyForEmit(value)) {
188
+ if (!this.isReadyForEmit(value)) {
229
189
  return;
230
190
  }
231
191
 
232
192
  this.listener(value);
233
193
  }
234
194
 
195
+ isReadyForEmit(
196
+ value: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE,
197
+ ): boolean {
198
+ if (value === CoValueLoadingState.UNAVAILABLE) {
199
+ return true;
200
+ }
201
+
202
+ // If the value is not completely downloaded, we don't emit it to avoid providing partial data to the listener.
203
+ if (!isCompletelyDownloaded(value)) {
204
+ return false;
205
+ }
206
+
207
+ return true;
208
+ }
209
+
235
210
  /**
236
211
  * Unsubscribes from all active subscriptions and marks the instance as unsubscribed.
237
212
  * This prevents any further operations and ensures proper cleanup.
@@ -246,11 +221,7 @@ export class CoValueCoreSubscription {
246
221
  /**
247
222
  * This is true if the value is unavailable, or if the value is a binary coValue or a completely downloaded coValue.
248
223
  */
249
- function isReadyForEmit(value: RawCoValue | "unavailable") {
250
- if (value === "unavailable") {
251
- return true;
252
- }
253
-
224
+ function isCompletelyDownloaded(value: RawCoValue) {
254
225
  return (
255
226
  value.core.verified?.header.meta?.type === "binary" ||
256
227
  value.core.isCompletelyDownloaded()
@@ -11,21 +11,24 @@ export class JazzError {
11
11
  ) {}
12
12
 
13
13
  toString() {
14
- return this.issues
14
+ // Build the main error message with inline stack info so it shows even if truncated
15
+ let result = this.issues
15
16
  .map((issue) => {
16
17
  let message = `${issue.message}`;
17
18
 
18
- if (this.id) {
19
- message += ` from ${this.id}`;
20
- }
21
-
22
19
  if (issue.path.length > 0) {
23
- message += ` on path ${issue.path.join(".")}`;
20
+ if (this.id) {
21
+ message += `. Subscription starts from ${this.id}`;
22
+ }
23
+
24
+ message += ` and the value is on path ${issue.path.join(".")}`;
24
25
  }
25
26
 
26
27
  return message;
27
28
  })
28
29
  .join("\n");
30
+
31
+ return result;
29
32
  }
30
33
 
31
34
  prependPath(item: string) {