jazz-tools 0.18.37 → 0.19.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.
Files changed (309) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.d.ts +8 -8
  2. package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
  3. package/.svelte-kit/__package__/jazz.class.svelte.js +39 -14
  4. package/.svelte-kit/__package__/media/image.svelte +6 -5
  5. package/.svelte-kit/__package__/media/image.svelte.d.ts.map +1 -1
  6. package/.svelte-kit/__package__/tests/AccountCoState.svelte.test-d.d.ts +2 -0
  7. package/.svelte-kit/__package__/tests/AccountCoState.svelte.test-d.d.ts.map +1 -0
  8. package/.svelte-kit/__package__/tests/AccountCoState.svelte.test-d.js +19 -0
  9. package/.svelte-kit/__package__/tests/CoState.svelte.test-d.d.ts +2 -0
  10. package/.svelte-kit/__package__/tests/CoState.svelte.test-d.d.ts.map +1 -0
  11. package/.svelte-kit/__package__/tests/CoState.svelte.test-d.js +16 -0
  12. package/.svelte-kit/__package__/tests/CoState.svelte.test.d.ts +2 -0
  13. package/.svelte-kit/__package__/tests/CoState.svelte.test.d.ts.map +1 -0
  14. package/.svelte-kit/__package__/tests/CoState.svelte.test.js +42 -0
  15. package/.svelte-kit/__package__/tests/TestCoStateWrapper.svelte +23 -0
  16. package/.svelte-kit/__package__/tests/TestCoStateWrapper.svelte.d.ts +12 -0
  17. package/.svelte-kit/__package__/tests/TestCoStateWrapper.svelte.d.ts.map +1 -0
  18. package/.turbo/turbo-build.log +53 -53
  19. package/CHANGELOG.md +33 -0
  20. package/dist/better-auth/database-adapter/index.js +14 -11
  21. package/dist/better-auth/database-adapter/index.js.map +1 -1
  22. package/dist/better-auth/database-adapter/repository/generic.d.ts +1 -1
  23. package/dist/better-auth/database-adapter/repository/generic.d.ts.map +1 -1
  24. package/dist/better-auth/database-adapter/repository/user.d.ts.map +1 -1
  25. package/dist/{chunk-OSQ7S47Q.js → chunk-P3YLNFN4.js} +504 -232
  26. package/dist/chunk-P3YLNFN4.js.map +1 -0
  27. package/dist/index.js +14 -6
  28. package/dist/index.js.map +1 -1
  29. package/dist/inspector/{custom-element-SUVJ7CPN.js → custom-element-QESCMFY7.js} +13 -3
  30. package/dist/inspector/{custom-element-SUVJ7CPN.js.map → custom-element-QESCMFY7.js.map} +1 -1
  31. package/dist/inspector/index.js +12 -2
  32. package/dist/inspector/index.js.map +1 -1
  33. package/dist/inspector/register-custom-element.js +1 -1
  34. package/dist/inspector/viewer/history-view.d.ts.map +1 -1
  35. package/dist/media/{chunk-K6GCHLQU.js → chunk-3LKBM3G3.js} +23 -15
  36. package/dist/media/chunk-3LKBM3G3.js.map +1 -0
  37. package/dist/media/create-image/browser.d.ts +2 -2
  38. package/dist/media/create-image/react-native.d.ts +2 -2
  39. package/dist/media/create-image/server.d.ts +2 -2
  40. package/dist/media/create-image-factory.d.ts +2 -2
  41. package/dist/media/index.browser.js +1 -1
  42. package/dist/media/index.js +1 -1
  43. package/dist/media/index.native.js +1 -1
  44. package/dist/media/index.server.js +1 -1
  45. package/dist/media/utils.d.ts.map +1 -1
  46. package/dist/react/hooks.d.ts +1 -1
  47. package/dist/react/hooks.d.ts.map +1 -1
  48. package/dist/react/index.d.ts +1 -1
  49. package/dist/react/index.d.ts.map +1 -1
  50. package/dist/react/index.js +31 -15
  51. package/dist/react/index.js.map +1 -1
  52. package/dist/react/media/image.d.ts.map +1 -1
  53. package/dist/react-core/hooks.d.ts +126 -224
  54. package/dist/react-core/hooks.d.ts.map +1 -1
  55. package/dist/react-core/index.js +65 -63
  56. package/dist/react-core/index.js.map +1 -1
  57. package/dist/react-core/subscription-provider.d.ts.map +1 -1
  58. package/dist/react-core/tests/createCoValueSubscriptionContext.test.d.ts +2 -0
  59. package/dist/react-core/tests/createCoValueSubscriptionContext.test.d.ts.map +1 -0
  60. package/dist/react-core/tests/useAccount.selector.test.d.ts +2 -0
  61. package/dist/react-core/tests/useAccount.selector.test.d.ts.map +1 -0
  62. package/dist/react-core/tests/useCoState.selector.test.d.ts +2 -0
  63. package/dist/react-core/tests/useCoState.selector.test.d.ts.map +1 -0
  64. package/dist/react-native-core/hooks.d.ts +1 -1
  65. package/dist/react-native-core/hooks.d.ts.map +1 -1
  66. package/dist/react-native-core/index.js +7 -5
  67. package/dist/react-native-core/index.js.map +1 -1
  68. package/dist/react-native-core/media/image.d.ts.map +1 -1
  69. package/dist/svelte/jazz.class.svelte.d.ts +8 -8
  70. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  71. package/dist/svelte/jazz.class.svelte.js +39 -14
  72. package/dist/svelte/media/image.svelte +6 -5
  73. package/dist/svelte/media/image.svelte.d.ts.map +1 -1
  74. package/dist/svelte/tests/AccountCoState.svelte.test-d.d.ts +2 -0
  75. package/dist/svelte/tests/AccountCoState.svelte.test-d.d.ts.map +1 -0
  76. package/dist/svelte/tests/AccountCoState.svelte.test-d.js +19 -0
  77. package/dist/svelte/tests/CoState.svelte.test-d.d.ts +2 -0
  78. package/dist/svelte/tests/CoState.svelte.test-d.d.ts.map +1 -0
  79. package/dist/svelte/tests/CoState.svelte.test-d.js +16 -0
  80. package/dist/svelte/tests/CoState.svelte.test.d.ts +2 -0
  81. package/dist/svelte/tests/CoState.svelte.test.d.ts.map +1 -0
  82. package/dist/svelte/tests/CoState.svelte.test.js +42 -0
  83. package/dist/svelte/tests/TestCoStateWrapper.svelte +23 -0
  84. package/dist/svelte/tests/TestCoStateWrapper.svelte.d.ts +12 -0
  85. package/dist/svelte/tests/TestCoStateWrapper.svelte.d.ts.map +1 -0
  86. package/dist/testing.js +3 -1
  87. package/dist/testing.js.map +1 -1
  88. package/dist/tools/coValues/CoValueBase.d.ts +4 -1
  89. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  90. package/dist/tools/coValues/account.d.ts +6 -6
  91. package/dist/tools/coValues/account.d.ts.map +1 -1
  92. package/dist/tools/coValues/coFeed.d.ts +5 -5
  93. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  94. package/dist/tools/coValues/coList.d.ts +9 -8
  95. package/dist/tools/coValues/coList.d.ts.map +1 -1
  96. package/dist/tools/coValues/coMap.d.ts +17 -17
  97. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  98. package/dist/tools/coValues/coPlainText.d.ts +3 -3
  99. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  100. package/dist/tools/coValues/coVector.d.ts +3 -3
  101. package/dist/tools/coValues/coVector.d.ts.map +1 -1
  102. package/dist/tools/coValues/deepLoading.d.ts +66 -36
  103. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  104. package/dist/tools/coValues/extensions/imageDef.d.ts +1 -1
  105. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  106. package/dist/tools/coValues/group.d.ts +2 -2
  107. package/dist/tools/coValues/group.d.ts.map +1 -1
  108. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  109. package/dist/tools/coValues/interfaces.d.ts +14 -6
  110. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  111. package/dist/tools/coValues/request.d.ts.map +1 -1
  112. package/dist/tools/coValues/schemaUnion.d.ts +2 -2
  113. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  114. package/dist/tools/exports.d.ts +3 -3
  115. package/dist/tools/exports.d.ts.map +1 -1
  116. package/dist/tools/implementation/refs.d.ts +3 -3
  117. package/dist/tools/implementation/refs.d.ts.map +1 -1
  118. package/dist/tools/implementation/schema.d.ts +2 -2
  119. package/dist/tools/implementation/schema.d.ts.map +1 -1
  120. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  121. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -1
  122. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -1
  123. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +33 -18
  124. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  125. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +5 -4
  126. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  127. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +23 -12
  128. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  129. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +29 -18
  130. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  131. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +39 -22
  132. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  133. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +1 -0
  134. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  135. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +30 -19
  136. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  137. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +5 -0
  138. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -1
  139. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts +6 -5
  140. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts.map +1 -1
  141. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +3 -2
  142. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  143. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts +3 -2
  144. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts.map +1 -1
  145. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +3 -2
  146. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  147. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +3 -2
  148. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  149. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +3 -0
  150. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  151. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesMaybeLoaded.d.ts +22 -0
  152. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesMaybeLoaded.d.ts.map +1 -0
  153. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +4 -0
  154. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  155. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded.d.ts +9 -0
  156. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded.d.ts.map +1 -0
  157. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  158. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +7 -6
  159. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  160. package/dist/tools/internal.d.ts +3 -3
  161. package/dist/tools/internal.d.ts.map +1 -1
  162. package/dist/tools/lib/utils.d.ts +14 -0
  163. package/dist/tools/lib/utils.d.ts.map +1 -0
  164. package/dist/tools/lib/utils.test.d.ts +2 -0
  165. package/dist/tools/lib/utils.test.d.ts.map +1 -0
  166. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +3 -2
  167. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  168. package/dist/tools/subscribe/JazzError.d.ts +4 -3
  169. package/dist/tools/subscribe/JazzError.d.ts.map +1 -1
  170. package/dist/tools/subscribe/SubscriptionScope.d.ts +6 -5
  171. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  172. package/dist/tools/subscribe/index.d.ts.map +1 -1
  173. package/dist/tools/subscribe/types.d.ts +23 -3
  174. package/dist/tools/subscribe/types.d.ts.map +1 -1
  175. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  176. package/dist/tools/testing.d.ts +3 -2
  177. package/dist/tools/testing.d.ts.map +1 -1
  178. package/dist/tools/tests/schema.resolved.test.d.ts +2 -0
  179. package/dist/tools/tests/schema.resolved.test.d.ts.map +1 -0
  180. package/dist/tools/tests/utils.d.ts +2 -1
  181. package/dist/tools/tests/utils.d.ts.map +1 -1
  182. package/package.json +4 -4
  183. package/src/better-auth/database-adapter/index.ts +2 -2
  184. package/src/better-auth/database-adapter/repository/account.ts +3 -3
  185. package/src/better-auth/database-adapter/repository/generic.ts +9 -6
  186. package/src/better-auth/database-adapter/repository/user.ts +6 -2
  187. package/src/better-auth/database-adapter/schema.ts +1 -1
  188. package/src/better-auth/database-adapter/tests/index.test.ts +3 -4
  189. package/src/inspector/tests/viewer/history-view.test.tsx +74 -10
  190. package/src/inspector/viewer/history-view.tsx +14 -5
  191. package/src/media/utils.test.ts +5 -0
  192. package/src/media/utils.ts +25 -16
  193. package/src/react/hooks.tsx +2 -2
  194. package/src/react/index.ts +2 -2
  195. package/src/react/media/image.tsx +10 -2
  196. package/src/react/tests/useAcceptInvite.test.ts +3 -1
  197. package/src/react-core/hooks.ts +226 -304
  198. package/src/react-core/subscription-provider.tsx +14 -7
  199. package/src/react-core/tests/createCoValueSubscriptionContext.test.tsx +233 -0
  200. package/src/react-core/tests/subscription.bench.tsx +32 -15
  201. package/src/react-core/tests/{useAccountWithSelector.test.ts → useAccount.selector.test.ts} +72 -24
  202. package/src/react-core/tests/useAccount.test.ts +92 -106
  203. package/src/react-core/tests/{useCoStateWithSelector.test.ts → useCoState.selector.test.ts} +23 -8
  204. package/src/react-core/tests/useCoState.test.ts +173 -49
  205. package/src/react-core/tests/useInboxSender.test.ts +3 -1
  206. package/src/react-core/tests/usePassPhraseAuth.test.ts +11 -82
  207. package/src/react-core/tests/useSubscriptionSelector.test.ts +48 -14
  208. package/src/react-native-core/hooks.tsx +2 -2
  209. package/src/react-native-core/media/image.tsx +3 -1
  210. package/src/svelte/jazz.class.svelte.ts +103 -27
  211. package/src/svelte/media/image.svelte +6 -5
  212. package/src/svelte/tests/AccountCoState.svelte.test-d.ts +23 -0
  213. package/src/svelte/tests/CoState.svelte.test-d.ts +20 -0
  214. package/src/svelte/tests/CoState.svelte.test.ts +57 -0
  215. package/src/svelte/tests/TestCoStateWrapper.svelte +23 -0
  216. package/src/tools/coValues/CoValueBase.ts +12 -0
  217. package/src/tools/coValues/account.ts +17 -12
  218. package/src/tools/coValues/coFeed.ts +38 -15
  219. package/src/tools/coValues/coList.ts +16 -11
  220. package/src/tools/coValues/coMap.ts +15 -14
  221. package/src/tools/coValues/coPlainText.ts +6 -3
  222. package/src/tools/coValues/coVector.ts +6 -5
  223. package/src/tools/coValues/deepLoading.ts +98 -51
  224. package/src/tools/coValues/group.ts +3 -2
  225. package/src/tools/coValues/inbox.ts +11 -10
  226. package/src/tools/coValues/interfaces.ts +49 -27
  227. package/src/tools/coValues/request.ts +8 -6
  228. package/src/tools/coValues/schemaUnion.ts +2 -1
  229. package/src/tools/exports.ts +11 -1
  230. package/src/tools/implementation/refs.ts +19 -13
  231. package/src/tools/implementation/schema.ts +5 -4
  232. package/src/tools/implementation/schemaUtils.ts +15 -0
  233. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +4 -4
  234. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +131 -95
  235. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +19 -9
  236. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +73 -22
  237. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +86 -28
  238. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +204 -148
  239. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +1 -0
  240. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +71 -27
  241. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +7 -0
  242. package/src/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.ts +8 -6
  243. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +4 -1
  244. package/src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts +4 -1
  245. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +4 -1
  246. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +4 -1
  247. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +3 -0
  248. package/src/tools/implementation/zodSchema/typeConverters/{InstanceOfSchemaCoValuesNullable.ts → InstanceOfSchemaCoValuesMaybeLoaded.ts} +47 -39
  249. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +4 -0
  250. package/src/tools/implementation/zodSchema/typeConverters/{InstanceOrPrimitiveOfSchemaCoValuesNullable.ts → InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded.ts} +7 -3
  251. package/src/tools/implementation/zodSchema/zodSchema.ts +15 -7
  252. package/src/tools/internal.ts +3 -3
  253. package/src/tools/lib/utils.test.ts +56 -0
  254. package/src/tools/lib/utils.ts +32 -0
  255. package/src/tools/subscribe/CoValueCoreSubscription.ts +13 -8
  256. package/src/tools/subscribe/JazzError.ts +8 -2
  257. package/src/tools/subscribe/SubscriptionScope.ts +55 -44
  258. package/src/tools/subscribe/index.ts +12 -4
  259. package/src/tools/subscribe/types.ts +36 -2
  260. package/src/tools/subscribe/utils.ts +2 -1
  261. package/src/tools/testing.ts +5 -0
  262. package/src/tools/tests/CoValueCoreSubscription.test.ts +10 -4
  263. package/src/tools/tests/ContextManager.test.ts +12 -5
  264. package/src/tools/tests/account.test.ts +22 -12
  265. package/src/tools/tests/coDiscriminatedUnion.test.ts +8 -4
  266. package/src/tools/tests/coFeed.branch.test.ts +48 -32
  267. package/src/tools/tests/coFeed.test-d.ts +17 -10
  268. package/src/tools/tests/coFeed.test.ts +52 -30
  269. package/src/tools/tests/coList.branch.test.ts +21 -21
  270. package/src/tools/tests/coList.test-d.ts +39 -23
  271. package/src/tools/tests/coList.test.ts +51 -25
  272. package/src/tools/tests/coList.unique.test.ts +34 -29
  273. package/src/tools/tests/coMap.branch.test.ts +20 -21
  274. package/src/tools/tests/coMap.record.test-d.ts +28 -26
  275. package/src/tools/tests/coMap.record.test.ts +30 -20
  276. package/src/tools/tests/coMap.test-d.ts +31 -29
  277. package/src/tools/tests/coMap.test.ts +67 -40
  278. package/src/tools/tests/coMap.unique.test.ts +25 -24
  279. package/src/tools/tests/coVector.test.ts +29 -15
  280. package/src/tools/tests/createContext.test.ts +5 -3
  281. package/src/tools/tests/deepLoading.test.ts +314 -117
  282. package/src/tools/tests/exportImport.test.ts +16 -15
  283. package/src/tools/tests/groupsAndAccounts.test.ts +39 -16
  284. package/src/tools/tests/inbox.test.ts +3 -1
  285. package/src/tools/tests/load.test.ts +29 -23
  286. package/src/tools/tests/patterns/quest.test.ts +3 -2
  287. package/src/tools/tests/patterns/requestToJoin.test.ts +17 -17
  288. package/src/tools/tests/request.test.ts +12 -2
  289. package/src/tools/tests/schema.resolved.test.ts +723 -0
  290. package/src/tools/tests/schemaUnion.test.ts +7 -3
  291. package/src/tools/tests/subscribe.test.ts +39 -21
  292. package/src/tools/tests/testing.test.ts +3 -2
  293. package/src/tools/tests/utils.ts +15 -2
  294. package/dist/chunk-OSQ7S47Q.js.map +0 -1
  295. package/dist/media/chunk-K6GCHLQU.js.map +0 -1
  296. package/dist/react-core/tests/useAccountWithSelector.test.d.ts +0 -2
  297. package/dist/react-core/tests/useAccountWithSelector.test.d.ts.map +0 -1
  298. package/dist/react-core/tests/useCoStateWithSelector.test.d.ts +0 -2
  299. package/dist/react-core/tests/useCoStateWithSelector.test.d.ts.map +0 -1
  300. package/dist/tools/implementation/errors.d.ts +0 -2
  301. package/dist/tools/implementation/errors.d.ts.map +0 -1
  302. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +0 -19
  303. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +0 -1
  304. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +0 -5
  305. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +0 -1
  306. package/dist/tools/lib/id.d.ts +0 -2
  307. package/dist/tools/lib/id.d.ts.map +0 -1
  308. package/src/tools/implementation/errors.ts +0 -1
  309. package/src/tools/lib/id.ts +0 -3
@@ -15,4 +15,11 @@ export interface CoreCoValueSchema {
15
15
  * Used for discriminating between different CoValue schemas.
16
16
  */
17
17
  builtin: string;
18
+
19
+ resolveQuery: CoreResolveQuery;
18
20
  }
21
+
22
+ /**
23
+ * Simplified type for {@link ResolveQuery}, used to avoid circularity issues.
24
+ */
25
+ export type CoreResolveQuery = boolean | Record<string, any>;
@@ -4,7 +4,7 @@ import {
4
4
  CoVector,
5
5
  Group,
6
6
  InstanceOrPrimitiveOfSchema,
7
- InstanceOrPrimitiveOfSchemaCoValuesNullable,
7
+ InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded,
8
8
  coOptionalDefiner,
9
9
  } from "../../../internal.js";
10
10
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
@@ -22,12 +22,14 @@ export function createCoreCoVectorSchema(
22
22
  collaborative: true as const,
23
23
  builtin: "CoVector" as const,
24
24
  dimensions,
25
+ resolveQuery: true as const,
25
26
  };
26
27
  }
27
28
 
28
29
  export class CoVectorSchema implements CoreCoVectorSchema {
29
30
  readonly collaborative = true as const;
30
31
  readonly builtin = "CoVector" as const;
32
+ readonly resolveQuery = true as const;
31
33
 
32
34
  constructor(
33
35
  public dimensions: number,
@@ -63,7 +65,7 @@ export class CoVectorSchema implements CoreCoVectorSchema {
63
65
  load(
64
66
  id: string,
65
67
  options?: { loadAs: Account | AnonymousJazzAgent },
66
- ): Promise<CoVectorInstanceNullable> {
68
+ ): Promise<MaybeLoadedCoVectorInstance> {
67
69
  return this.coValueClass.load(id, options);
68
70
  }
69
71
 
@@ -74,14 +76,14 @@ export class CoVectorSchema implements CoreCoVectorSchema {
74
76
  id: string,
75
77
  options: { loadAs: Account | AnonymousJazzAgent },
76
78
  listener: (
77
- value: CoVectorInstanceNullable,
79
+ value: MaybeLoadedCoVectorInstance,
78
80
  unsubscribe: () => void,
79
81
  ) => void,
80
82
  ): () => void;
81
83
  subscribe(
82
84
  id: string,
83
85
  listener: (
84
- value: CoVectorInstanceNullable,
86
+ value: MaybeLoadedCoVectorInstance,
85
87
  unsubscribe: () => void,
86
88
  ) => void,
87
89
  ): () => void;
@@ -101,5 +103,5 @@ export class CoVectorSchema implements CoreCoVectorSchema {
101
103
 
102
104
  export type CoVectorInstance = InstanceOrPrimitiveOfSchema<CoVectorSchema>;
103
105
 
104
- export type CoVectorInstanceNullable =
105
- InstanceOrPrimitiveOfSchemaCoValuesNullable<CoVectorSchema>;
106
+ export type MaybeLoadedCoVectorInstance =
107
+ InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<CoVectorSchema>;
@@ -3,6 +3,7 @@ import {
3
3
  AnonymousJazzAgent,
4
4
  FileStream,
5
5
  Group,
6
+ MaybeLoaded,
6
7
  coOptionalDefiner,
7
8
  unstable_mergeBranchWithResolve,
8
9
  } from "../../../internal.js";
@@ -17,12 +18,14 @@ export function createCoreFileStreamSchema(): CoreFileStreamSchema {
17
18
  return {
18
19
  collaborative: true as const,
19
20
  builtin: "FileStream" as const,
21
+ resolveQuery: true as const,
20
22
  };
21
23
  }
22
24
 
23
25
  export class FileStreamSchema implements CoreFileStreamSchema {
24
26
  readonly collaborative = true as const;
25
27
  readonly builtin = "FileStream" as const;
28
+ readonly resolveQuery = true as const;
26
29
 
27
30
  constructor(private coValueClass: typeof FileStream) {}
28
31
 
@@ -79,7 +82,7 @@ export class FileStreamSchema implements CoreFileStreamSchema {
79
82
  load(
80
83
  id: string,
81
84
  options?: { loadAs?: Account | AnonymousJazzAgent },
82
- ): Promise<FileStream | null> {
85
+ ): Promise<MaybeLoaded<FileStream>> {
83
86
  return this.coValueClass.load(id, options);
84
87
  }
85
88
 
@@ -6,6 +6,7 @@ import {
6
6
  } from "../../../coValues/interfaces.js";
7
7
  import {
8
8
  Account,
9
+ MaybeLoaded,
9
10
  RefsToResolve,
10
11
  RefsToResolveStrict,
11
12
  Resolved,
@@ -24,12 +25,14 @@ export function createCoreGroupSchema(): CoreGroupSchema {
24
25
  return {
25
26
  collaborative: true as const,
26
27
  builtin: "Group" as const,
28
+ resolveQuery: true as const,
27
29
  };
28
30
  }
29
31
 
30
32
  export class GroupSchema implements CoreGroupSchema {
31
33
  readonly collaborative = true as const;
32
34
  readonly builtin = "Group" as const;
35
+ readonly resolveQuery = true as const;
33
36
 
34
37
  getCoValueClass(): typeof Group {
35
38
  return Group;
@@ -49,7 +52,7 @@ export class GroupSchema implements CoreGroupSchema {
49
52
  loadAs?: Account;
50
53
  resolve?: RefsToResolveStrict<Group, R>;
51
54
  },
52
- ): Promise<Group | null> {
55
+ ): Promise<MaybeLoaded<Group>> {
53
56
  return Group.load(id, options);
54
57
  }
55
58
  createInvite<G extends Group>(
@@ -4,6 +4,7 @@ import {
4
4
  BranchDefinition,
5
5
  CoPlainText,
6
6
  Group,
7
+ MaybeLoaded,
7
8
  coOptionalDefiner,
8
9
  unstable_mergeBranchWithResolve,
9
10
  } from "../../../internal.js";
@@ -19,12 +20,14 @@ export function createCoreCoPlainTextSchema(): CorePlainTextSchema {
19
20
  return {
20
21
  collaborative: true as const,
21
22
  builtin: "CoPlainText" as const,
23
+ resolveQuery: true as const,
22
24
  };
23
25
  }
24
26
 
25
27
  export class PlainTextSchema implements CorePlainTextSchema {
26
28
  readonly collaborative = true as const;
27
29
  readonly builtin = "CoPlainText" as const;
30
+ readonly resolveQuery = true as const;
28
31
 
29
32
  constructor(private coValueClass: typeof CoPlainText) {}
30
33
 
@@ -47,7 +50,7 @@ export class PlainTextSchema implements CorePlainTextSchema {
47
50
  loadAs: Account | AnonymousJazzAgent;
48
51
  unstable_branch?: BranchDefinition;
49
52
  },
50
- ): Promise<CoPlainText | null> {
53
+ ): Promise<MaybeLoaded<CoPlainText>> {
51
54
  return this.coValueClass.load(id, options);
52
55
  }
53
56
 
@@ -3,6 +3,7 @@ import {
3
3
  BranchDefinition,
4
4
  CoRichText,
5
5
  Group,
6
+ MaybeLoaded,
6
7
  coOptionalDefiner,
7
8
  unstable_mergeBranchWithResolve,
8
9
  } from "../../../internal.js";
@@ -18,12 +19,14 @@ export function createCoreCoRichTextSchema(): CoreRichTextSchema {
18
19
  return {
19
20
  collaborative: true as const,
20
21
  builtin: "CoRichText" as const,
22
+ resolveQuery: true as const,
21
23
  };
22
24
  }
23
25
 
24
26
  export class RichTextSchema implements CoreRichTextSchema {
25
27
  readonly collaborative = true as const;
26
28
  readonly builtin = "CoRichText" as const;
29
+ readonly resolveQuery = true as const;
27
30
 
28
31
  constructor(private coValueClass: typeof CoRichText) {}
29
32
 
@@ -46,7 +49,7 @@ export class RichTextSchema implements CoreRichTextSchema {
46
49
  loadAs: Account | AnonymousJazzAgent;
47
50
  unstable_branch?: BranchDefinition;
48
51
  },
49
- ): Promise<CoRichText | null> {
52
+ ): Promise<MaybeLoaded<CoRichText>> {
50
53
  return this.coValueClass.load(id, options);
51
54
  }
52
55
 
@@ -27,6 +27,9 @@ import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
27
27
  import { z } from "../zodReExport.js";
28
28
  import { InstanceOrPrimitiveOfSchema } from "./InstanceOrPrimitiveOfSchema.js";
29
29
 
30
+ /**
31
+ * A loaded CoValue whose references are also loaded.
32
+ */
30
33
  export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
31
34
  S extends CoreCoValueSchema
32
35
  ? S extends CoreAccountSchema<infer Shape>
@@ -17,6 +17,7 @@ import {
17
17
  CoVector,
18
18
  FileStream,
19
19
  Group,
20
+ MaybeLoaded,
20
21
  } from "../../../internal.js";
21
22
  import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
22
23
  import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
@@ -25,66 +26,73 @@ import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
25
26
  import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
26
27
  import { CoreGroupSchema } from "../schemaTypes/GroupSchema.js";
27
28
  import { z } from "../zodReExport.js";
28
- import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "./InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
29
+ import { InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded } from "./InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded.js";
29
30
 
30
- export type InstanceOfSchemaCoValuesNullable<
31
+ /**
32
+ * A loaded CoValue whose references may or may not be loaded.
33
+ */
34
+ export type InstanceOfSchemaCoValuesMaybeLoaded<
31
35
  S extends CoValueClass | AnyZodOrCoValueSchema,
32
36
  > = S extends CoreCoValueSchema
33
37
  ? S extends CoreAccountSchema<infer Shape>
34
- ?
35
- | ({
36
- readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
37
- Shape[key]
38
- >;
39
- } & Account)
40
- | null
38
+ ? MaybeLoaded<
39
+ {
40
+ readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<
41
+ Shape[key]
42
+ >;
43
+ } & Account
44
+ >
41
45
  : S extends CoreGroupSchema
42
- ? Group | null
46
+ ? MaybeLoaded<Group>
43
47
  : S extends CoreCoRecordSchema<infer K, infer V>
44
- ?
45
- | ({
46
- readonly [key in z.output<K> &
47
- string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
48
- } & CoMap)
49
- | null
48
+ ? MaybeLoaded<
49
+ {
50
+ readonly [key in z.output<K> &
51
+ string]: InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<V>;
52
+ } & CoMap
53
+ >
50
54
  : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
51
- ?
52
- | ({
53
- readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
54
- Shape[key]
55
- >;
56
- } & (CatchAll extends AnyZodOrCoValueSchema
57
- ? {
58
- readonly [
59
- key: string
60
- ]: InstanceOrPrimitiveOfSchemaCoValuesNullable<CatchAll>;
61
- }
62
- : {}) &
63
- CoMap)
64
- | null
55
+ ? MaybeLoaded<
56
+ {
57
+ readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<
58
+ Shape[key]
59
+ >;
60
+ } & (CatchAll extends AnyZodOrCoValueSchema
61
+ ? {
62
+ readonly [
63
+ key: string
64
+ ]: InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<CatchAll>;
65
+ }
66
+ : {}) &
67
+ CoMap
68
+ >
65
69
  : S extends CoreCoListSchema<infer T>
66
- ? CoList<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
70
+ ? MaybeLoaded<
71
+ CoList<InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<T>>
72
+ >
67
73
  : S extends CoreCoFeedSchema<infer T>
68
- ? CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
74
+ ? MaybeLoaded<
75
+ CoFeed<InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<T>>
76
+ >
69
77
  : S extends CorePlainTextSchema
70
- ? CoPlainText | null
78
+ ? MaybeLoaded<CoPlainText>
71
79
  : S extends CoreRichTextSchema
72
- ? CoRichText | null
80
+ ? MaybeLoaded<CoRichText>
73
81
  : S extends CoreFileStreamSchema
74
- ? FileStream | null
82
+ ? MaybeLoaded<FileStream>
75
83
  : S extends CoreCoVectorSchema
76
- ? Readonly<CoVector> | null
84
+ ? MaybeLoaded<Readonly<CoVector>>
77
85
  : S extends CoreCoOptionalSchema<infer Inner>
78
86
  ?
79
- | InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
87
+ | InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<Inner>
80
88
  | undefined
81
89
  : S extends CoreCoDiscriminatedUnionSchema<
82
90
  infer Members
83
91
  >
84
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<
92
+ ? InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<
85
93
  Members[number]
86
94
  >
87
95
  : never
88
96
  : S extends CoValueClass
89
- ? InstanceType<S> | null
97
+ ? MaybeLoaded<InstanceType<S>>
90
98
  : never;
@@ -6,6 +6,10 @@ import {
6
6
  import { z } from "../zodReExport.js";
7
7
  import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
8
8
 
9
+ /**
10
+ * A loaded CoValue or primitive type.
11
+ * If it's a CoValue, its references to other CoValues are also loaded.
12
+ */
9
13
  export type InstanceOrPrimitiveOfSchema<
10
14
  S extends CoValueClass | AnyZodOrCoValueSchema,
11
15
  > = S extends z.core.$ZodType ? TypeOfZodSchema<S> : InstanceOfSchema<S>;
@@ -1,13 +1,17 @@
1
1
  import {
2
2
  AnyZodOrCoValueSchema,
3
3
  CoValueClass,
4
- InstanceOfSchemaCoValuesNullable,
4
+ InstanceOfSchemaCoValuesMaybeLoaded,
5
5
  } from "../../../internal.js";
6
6
  import { z } from "../zodReExport.js";
7
7
  import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
8
8
 
9
- export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
9
+ /**
10
+ * A loaded CoValue or primitive type.
11
+ * If it's a CoValue, its references to other CoValues may or may not be loaded.
12
+ */
13
+ export type InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded<
10
14
  S extends CoValueClass | AnyZodOrCoValueSchema,
11
15
  > = S extends z.core.$ZodType
12
16
  ? TypeOfZodSchema<S>
13
- : InstanceOfSchemaCoValuesNullable<S>;
17
+ : InstanceOfSchemaCoValuesMaybeLoaded<S>;
@@ -2,6 +2,7 @@ import { LocalNode, RawAccount } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  AccountClass,
5
+ LoadedAndRequired,
5
6
  CoRecordSchema,
6
7
  CoValueClass,
7
8
  CoValueFromRaw,
@@ -45,7 +46,7 @@ import {
45
46
  CoreRichTextSchema,
46
47
  RichTextSchema,
47
48
  } from "./schemaTypes/RichTextSchema.js";
48
- import { InstanceOfSchemaCoValuesNullable } from "./typeConverters/InstanceOfSchemaCoValuesNullable.js";
49
+ import { InstanceOfSchemaCoValuesMaybeLoaded } from "./typeConverters/InstanceOfSchemaCoValuesMaybeLoaded.js";
49
50
  import { z } from "./zodReExport.js";
50
51
  import { CoreGroupSchema } from "./schemaTypes/GroupSchema.js";
51
52
  import { GroupSchema } from "./schemaTypes/GroupSchema.js";
@@ -92,8 +93,8 @@ export type CoValueSchemaFromCoreSchema<S extends CoreCoValueSchema> =
92
93
  export type CoValueClassFromAnySchema<S extends CoValueClassOrSchema> =
93
94
  S extends CoValueClass<any>
94
95
  ? S
95
- : CoValueClass<NonNullable<InstanceOfSchema<S>>> &
96
- CoValueFromRaw<NonNullable<InstanceOfSchema<S>>> &
96
+ : CoValueClass<LoadedAndRequired<InstanceOfSchema<S>>> &
97
+ CoValueFromRaw<LoadedAndRequired<InstanceOfSchema<S>>> &
97
98
  (S extends CoreAccountSchema ? AccountClassEssentials : {});
98
99
 
99
100
  type AccountClassEssentials = {
@@ -121,14 +122,21 @@ export type AnyZodOrCoValueSchema = AnyZodSchema | CoreCoValueSchema;
121
122
 
122
123
  export type Loaded<
123
124
  T extends CoValueClassOrSchema,
124
- R extends ResolveQuery<T> = true,
125
- > = Resolved<NonNullable<InstanceOfSchemaCoValuesNullable<T>>, R>;
125
+ // @ts-expect-error
126
+ R extends ResolveQuery<T> = SchemaResolveQuery<T>,
127
+ > = Resolved<LoadedAndRequired<InstanceOfSchemaCoValuesMaybeLoaded<T>>, R>;
126
128
 
127
129
  export type ResolveQuery<T extends CoValueClassOrSchema> = RefsToResolve<
128
- NonNullable<InstanceOfSchemaCoValuesNullable<T>>
130
+ LoadedAndRequired<InstanceOfSchemaCoValuesMaybeLoaded<T>>
129
131
  >;
130
132
 
131
133
  export type ResolveQueryStrict<
132
134
  T extends CoValueClassOrSchema,
133
135
  R extends ResolveQuery<T>,
134
- > = RefsToResolveStrict<NonNullable<InstanceOfSchemaCoValuesNullable<T>>, R>;
136
+ > = RefsToResolveStrict<
137
+ LoadedAndRequired<InstanceOfSchemaCoValuesMaybeLoaded<T>>,
138
+ R
139
+ >;
140
+
141
+ export type SchemaResolveQuery<T extends CoValueClassOrSchema> =
142
+ T extends CoreCoValueSchema ? T["resolveQuery"] : true;
@@ -23,13 +23,13 @@ export type * from "./subscribe/types.js";
23
23
  export * from "./coValues/deepLoading.js";
24
24
  export * from "./implementation/anonymousJazzAgent.js";
25
25
  export * from "./implementation/activeAccountContext.js";
26
- export * from "./implementation/errors.js";
27
26
  export * from "./implementation/refs.js";
28
27
  export * from "./implementation/schema.js";
29
28
  export * from "./subscribe/SubscriptionScope.js";
30
29
  export * from "./subscribe/types.js";
31
30
  export * from "./subscribe/index.js";
32
31
  export * from "./lib/cache.js";
32
+ export * from "./lib/utils.js";
33
33
  export * from "./lib/utilityTypes.js";
34
34
  export * from "./implementation/createContext.js";
35
35
 
@@ -47,9 +47,9 @@ export * from "./implementation/zodSchema/schemaTypes/FileStreamSchema.js";
47
47
  export * from "./implementation/zodSchema/schemaTypes/CoVectorSchema.js";
48
48
  export * from "./implementation/zodSchema/schemaTypes/PlainTextSchema.js";
49
49
  export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.js";
50
- export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
50
+ export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded.js";
51
51
  export * from "./implementation/zodSchema/typeConverters/InstanceOfSchema.js";
52
- export * from "./implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.js";
52
+ export * from "./implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesMaybeLoaded.js";
53
53
  export * from "./implementation/zodSchema/typeConverters/CoFieldSchemaInit.js";
54
54
  export * from "./implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.js";
55
55
  export * from "./implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.js";
@@ -0,0 +1,56 @@
1
+ import { beforeAll, describe, test, expect } from "vitest";
2
+ import {
3
+ assertLoaded,
4
+ co,
5
+ createUnloadedCoValue,
6
+ CoValueLoadingState,
7
+ getLoadedOrUndefined,
8
+ } from "../internal";
9
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing";
10
+
11
+ beforeAll(async () => {
12
+ await setupJazzTestSync();
13
+ await createJazzTestAccount({
14
+ isCurrentActiveAccount: true,
15
+ });
16
+ });
17
+
18
+ describe("assertLoaded", () => {
19
+ test("should throw an error if the CoValue is not loaded", () => {
20
+ const coValue = createUnloadedCoValue(
21
+ "id",
22
+ CoValueLoadingState.UNAVAILABLE,
23
+ );
24
+ expect(() => assertLoaded(coValue)).toThrow("CoValue is not loaded");
25
+ });
26
+
27
+ test("should narrow the value if it is loaded", () => {
28
+ const coValue = co.plainText().create("value");
29
+ assertLoaded(coValue);
30
+ expect(coValue.$isLoaded).toBe(true);
31
+ expect(coValue.toUpperCase()).toBe("VALUE");
32
+ });
33
+ });
34
+
35
+ describe("getLoadedOrUndefined", () => {
36
+ test("should return undefined if the CoValue is not loaded", () => {
37
+ const coValue = createUnloadedCoValue(
38
+ "id",
39
+ CoValueLoadingState.UNAVAILABLE,
40
+ );
41
+ expect(getLoadedOrUndefined(coValue)).toBeUndefined();
42
+ });
43
+
44
+ test("should return the CoValue if it is loaded", () => {
45
+ const coValue = co.plainText().create("value");
46
+ expect(getLoadedOrUndefined(coValue)).toBe(coValue);
47
+ });
48
+
49
+ test("should return undefined if the CoValue is null", () => {
50
+ expect(getLoadedOrUndefined(null)).toBeUndefined();
51
+ });
52
+
53
+ test("should return undefined if the CoValue is undefined", () => {
54
+ expect(getLoadedOrUndefined(undefined)).toBeUndefined();
55
+ });
56
+ });
@@ -0,0 +1,32 @@
1
+ import type { CoValue, LoadedAndRequired, MaybeLoaded } from "../internal.js";
2
+
3
+ export function isCoValueId(id: unknown): id is `co_z${string}` {
4
+ return typeof id === "string" && id.startsWith("co_z");
5
+ }
6
+
7
+ /**
8
+ * Assert that a CoValue is loaded, otherwise throw an error
9
+ */
10
+ export function assertLoaded<T extends MaybeLoaded<CoValue>>(
11
+ coValue: T,
12
+ ): asserts coValue is LoadedAndRequired<T> {
13
+ if (!coValue.$isLoaded) {
14
+ throw new Error("CoValue is not loaded");
15
+ }
16
+ }
17
+
18
+ /**
19
+ * Get a loaded CoValue or `undefined` if it is not loaded.
20
+ *
21
+ * Can be used to ease the transition between nullable loading states from Jazz 0.18
22
+ * and explicit loading states from Jazz 0.19.
23
+ */
24
+ export function getLoadedOrUndefined<T extends MaybeLoaded<CoValue>>(
25
+ coValue: T | undefined | null,
26
+ ): LoadedAndRequired<T> | undefined {
27
+ if (!coValue?.$isLoaded) {
28
+ return undefined;
29
+ }
30
+
31
+ return coValue as LoadedAndRequired<T>;
32
+ }
@@ -6,6 +6,7 @@ import {
6
6
  RawCoValue,
7
7
  } from "cojson";
8
8
  import type { BranchDefinition } from "./types.js";
9
+ import { CoValueLoadingState } from "./types.js";
9
10
 
10
11
  /**
11
12
  * Manages subscriptions to CoValue cores, handling both direct subscriptions
@@ -21,13 +22,17 @@ export class CoValueCoreSubscription {
21
22
  private branchName?: string;
22
23
  private source: CoValueCore;
23
24
  private localNode: LocalNode;
24
- private listener: (value: RawCoValue | "unavailable") => void;
25
+ private listener: (
26
+ value: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE,
27
+ ) => void;
25
28
  private skipRetry?: boolean;
26
29
 
27
30
  constructor(
28
31
  localNode: LocalNode,
29
32
  id: string,
30
- listener: (value: RawCoValue | "unavailable") => void,
33
+ listener: (
34
+ value: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE,
35
+ ) => void,
31
36
  skipRetry?: boolean,
32
37
  branch?: BranchDefinition,
33
38
  ) {
@@ -115,7 +120,7 @@ export class CoValueCoreSubscription {
115
120
  .then((value) => {
116
121
  if (this.unsubscribed) return;
117
122
 
118
- if (value !== "unavailable") {
123
+ if (value !== CoValueLoadingState.UNAVAILABLE) {
119
124
  // Branch checkout successful, subscribe to it
120
125
  this.subscribe(value);
121
126
  } else {
@@ -126,7 +131,7 @@ export class CoValueCoreSubscription {
126
131
  .catch((error) => {
127
132
  // Handle unexpected errors during branch checkout
128
133
  console.error(error);
129
- this.emit("unavailable");
134
+ this.emit(CoValueLoadingState.UNAVAILABLE);
130
135
  });
131
136
  }
132
137
 
@@ -143,7 +148,7 @@ export class CoValueCoreSubscription {
143
148
 
144
149
  // Source isn't available either, subscribe to state changes and report unavailability
145
150
  this.subscribeToUnavailableSource();
146
- this.emit("unavailable");
151
+ this.emit(CoValueLoadingState.UNAVAILABLE);
147
152
  }
148
153
 
149
154
  /**
@@ -162,13 +167,13 @@ export class CoValueCoreSubscription {
162
167
  } else {
163
168
  // Loading failed, subscribe to state changes and report unavailability
164
169
  this.subscribeToUnavailableSource();
165
- this.emit("unavailable");
170
+ this.emit(CoValueLoadingState.UNAVAILABLE);
166
171
  }
167
172
  })
168
173
  .catch((error) => {
169
174
  // Handle unexpected errors during loading
170
175
  console.error(error);
171
- this.emit("unavailable");
176
+ this.emit(CoValueLoadingState.UNAVAILABLE);
172
177
  });
173
178
  }
174
179
 
@@ -218,7 +223,7 @@ export class CoValueCoreSubscription {
218
223
  });
219
224
  }
220
225
 
221
- emit(value: RawCoValue | "unavailable"): void {
226
+ emit(value: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE): void {
222
227
  if (this.unsubscribed) return;
223
228
  if (!isReadyForEmit(value)) {
224
229
  return;
@@ -1,9 +1,12 @@
1
1
  import type { CoValue, ID } from "../internal.js";
2
+ import { CoValueLoadingState } from "./types.js";
2
3
 
3
4
  export class JazzError {
4
5
  constructor(
5
6
  public id: ID<CoValue> | undefined,
6
- public type: "unavailable" | "unauthorized",
7
+ public type:
8
+ | typeof CoValueLoadingState.UNAVAILABLE
9
+ | typeof CoValueLoadingState.UNAUTHORIZED,
7
10
  public issues: JazzErrorIssue[],
8
11
  ) {}
9
12
 
@@ -41,7 +44,10 @@ export class JazzError {
41
44
  }
42
45
  }
43
46
  export type JazzErrorIssue = {
44
- code: "unavailable" | "unauthorized" | "validationError";
47
+ code:
48
+ | typeof CoValueLoadingState.UNAVAILABLE
49
+ | typeof CoValueLoadingState.UNAUTHORIZED
50
+ | "validationError";
45
51
  message: string;
46
52
  params: Record<string, any>;
47
53
  path: string[];