jazz-tools 0.19.3 → 0.19.4

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 (78) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
  2. package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
  3. package/.svelte-kit/__package__/jazz.class.svelte.js +15 -17
  4. package/.turbo/turbo-build.log +64 -64
  5. package/CHANGELOG.md +14 -0
  6. package/dist/{chunk-JPWM4CS2.js → chunk-PT7FCV26.js} +145 -77
  7. package/dist/chunk-PT7FCV26.js.map +1 -0
  8. package/dist/index.js +14 -7
  9. package/dist/index.js.map +1 -1
  10. package/dist/inspector/{custom-element-3JAYHXWQ.js → custom-element-P76EIWEV.js} +301 -142
  11. package/dist/inspector/{custom-element-3JAYHXWQ.js.map → custom-element-P76EIWEV.js.map} +1 -1
  12. package/dist/inspector/index.js +281 -122
  13. package/dist/inspector/index.js.map +1 -1
  14. package/dist/inspector/register-custom-element.js +1 -1
  15. package/dist/inspector/tests/viewer/co-plain-text-view.test.d.ts +2 -0
  16. package/dist/inspector/tests/viewer/co-plain-text-view.test.d.ts.map +1 -0
  17. package/dist/inspector/utils/history.d.ts +5 -1
  18. package/dist/inspector/utils/history.d.ts.map +1 -1
  19. package/dist/inspector/viewer/co-plain-text-view.d.ts +4 -2
  20. package/dist/inspector/viewer/co-plain-text-view.d.ts.map +1 -1
  21. package/dist/inspector/viewer/page.d.ts.map +1 -1
  22. package/dist/inspector/viewer/use-resolve-covalue.d.ts +0 -1
  23. package/dist/inspector/viewer/use-resolve-covalue.d.ts.map +1 -1
  24. package/dist/react-core/hooks.d.ts.map +1 -1
  25. package/dist/react-core/index.js +4 -17
  26. package/dist/react-core/index.js.map +1 -1
  27. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  28. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  29. package/dist/svelte/jazz.class.svelte.js +15 -17
  30. package/dist/testing.js +1 -1
  31. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  32. package/dist/tools/coValues/group.d.ts.map +1 -1
  33. package/dist/tools/coValues/interfaces.d.ts +7 -6
  34. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  35. package/dist/tools/coValues/promise.d.ts +9 -0
  36. package/dist/tools/coValues/promise.d.ts.map +1 -0
  37. package/dist/tools/coValues/request.d.ts.map +1 -1
  38. package/dist/tools/exports.d.ts +1 -1
  39. package/dist/tools/exports.d.ts.map +1 -1
  40. package/dist/tools/implementation/refs.d.ts +1 -1
  41. package/dist/tools/implementation/refs.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +3 -1
  43. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -1
  44. package/dist/tools/subscribe/SubscriptionScope.d.ts +5 -2
  45. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  46. package/dist/tools/subscribe/index.d.ts +1 -1
  47. package/dist/tools/subscribe/index.d.ts.map +1 -1
  48. package/dist/tools/subscribe/types.d.ts +2 -1
  49. package/dist/tools/subscribe/types.d.ts.map +1 -1
  50. package/dist/tools/tests/SubscriptionScope.test.d.ts +2 -0
  51. package/dist/tools/tests/SubscriptionScope.test.d.ts.map +1 -0
  52. package/package.json +4 -4
  53. package/src/inspector/tests/utils/history.test.ts +233 -2
  54. package/src/inspector/tests/viewer/co-plain-text-view.test.tsx +125 -0
  55. package/src/inspector/tests/viewer/history-view.test.tsx +134 -2
  56. package/src/inspector/utils/history.ts +168 -1
  57. package/src/inspector/viewer/co-plain-text-view.tsx +102 -3
  58. package/src/inspector/viewer/history-view.tsx +5 -25
  59. package/src/inspector/viewer/page.tsx +8 -1
  60. package/src/inspector/viewer/use-resolve-covalue.ts +2 -6
  61. package/src/react-core/hooks.ts +5 -29
  62. package/src/svelte/jazz.class.svelte.ts +16 -34
  63. package/src/tools/coValues/coFeed.ts +10 -7
  64. package/src/tools/coValues/coMap.ts +10 -7
  65. package/src/tools/coValues/group.ts +6 -2
  66. package/src/tools/coValues/interfaces.ts +48 -28
  67. package/src/tools/coValues/promise.ts +34 -0
  68. package/src/tools/coValues/request.ts +12 -8
  69. package/src/tools/exports.ts +1 -0
  70. package/src/tools/implementation/refs.ts +9 -17
  71. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +62 -30
  72. package/src/tools/subscribe/SubscriptionScope.ts +45 -2
  73. package/src/tools/subscribe/index.ts +28 -13
  74. package/src/tools/subscribe/types.ts +5 -2
  75. package/src/tools/tests/SubscriptionScope.test.ts +397 -0
  76. package/src/tools/tests/deepLoading.test.ts +22 -0
  77. package/src/tools/tests/subscribe.test.ts +69 -0
  78. package/dist/chunk-JPWM4CS2.js.map +0 -1
@@ -847,10 +847,13 @@ function getEditFromRaw(target, rawEdit, descriptor, key) {
847
847
  target
848
848
  ) : void 0,
849
849
  get by() {
850
- return rawEdit.by && accessChildById(target, rawEdit.by, {
850
+ if (!rawEdit.by) return null;
851
+ const account = accessChildById(target, rawEdit.by, {
851
852
  ref: Account,
852
853
  optional: false
853
854
  });
855
+ if (!account.$isLoaded) return null;
856
+ return account;
854
857
  },
855
858
  madeAt: rawEdit.at,
856
859
  key
@@ -1792,10 +1795,13 @@ function entryFromRawEntry(accessFrom, rawEntry, loadedAs, accountID, itemField)
1792
1795
  }
1793
1796
  },
1794
1797
  get by() {
1795
- return accountID && accessChildById(accessFrom, accountID, {
1798
+ if (!accountID) return null;
1799
+ const account = accessChildById(accessFrom, accountID, {
1796
1800
  ref: Account,
1797
1801
  optional: false
1798
1802
  });
1803
+ if (!account.$isLoaded) return null;
1804
+ return account;
1799
1805
  },
1800
1806
  madeAt: rawEntry.at,
1801
1807
  tx: rawEntry.tx
@@ -2602,7 +2608,11 @@ var _Group = class _Group extends CoValueBase {
2602
2608
  role,
2603
2609
  ref: ref2,
2604
2610
  get account() {
2605
- return accessChildById(group, accountID, refEncodedAccountSchema);
2611
+ return accessChildById(
2612
+ group,
2613
+ accountID,
2614
+ refEncodedAccountSchema
2615
+ );
2606
2616
  }
2607
2617
  });
2608
2618
  }
@@ -2758,7 +2768,7 @@ function getCoValueOwner(coValue) {
2758
2768
  ref: RegisteredSchemas["Group"],
2759
2769
  optional: false
2760
2770
  });
2761
- if (!group) {
2771
+ if (!group.$isLoaded) {
2762
2772
  throw new Error("CoValue has no owner");
2763
2773
  }
2764
2774
  return group;
@@ -3695,27 +3705,18 @@ var Ref = class {
3695
3705
  node = subscriptionScope.childNodes.get(this.id);
3696
3706
  }
3697
3707
  if (!node) {
3698
- return createUnloadedCoValue(this.id, CoValueLoadingState.LOADING);
3708
+ return createUnloadedCoValue(this.id, CoValueLoadingState.UNAVAILABLE);
3699
3709
  }
3700
- const value = node.value;
3701
- if (value?.type === CoValueLoadingState.LOADED) {
3702
- return value.value;
3710
+ const value = node.getCurrentValue();
3711
+ if (value.$isLoaded) {
3712
+ return value;
3703
3713
  } else {
3704
3714
  return new Promise((resolve) => {
3705
3715
  const unsubscribe = node.subscribe((value2) => {
3706
- if (value2?.type === CoValueLoadingState.LOADED) {
3707
- unsubscribe();
3708
- resolve(value2.value);
3709
- } else if (value2?.type === CoValueLoadingState.UNAVAILABLE) {
3710
- unsubscribe();
3711
- resolve(
3712
- createUnloadedCoValue(this.id, CoValueLoadingState.UNAVAILABLE)
3713
- );
3714
- } else if (value2?.type === CoValueLoadingState.UNAUTHORIZED) {
3716
+ const currentValue = node.getCurrentValue();
3717
+ if (currentValue.$jazz.loadingState !== CoValueLoadingState.LOADING) {
3715
3718
  unsubscribe();
3716
- resolve(
3717
- createUnloadedCoValue(this.id, CoValueLoadingState.UNAUTHORIZED)
3718
- );
3719
+ resolve(currentValue);
3719
3720
  }
3720
3721
  if (subscriptionScope.closed) {
3721
3722
  node.destroy();
@@ -4334,7 +4335,30 @@ var SubscriptionScope = class _SubscriptionScope {
4334
4335
  if (this.value.type !== CoValueLoadingState.LOADED) return true;
4335
4336
  return this.pendingLoadedChildren.size === 0;
4336
4337
  }
4338
+ getUnloadedValue(reason) {
4339
+ if (this.unloadedValue?.$jazz.loadingState === reason) {
4340
+ return this.unloadedValue;
4341
+ }
4342
+ const unloadedValue = {
4343
+ $jazz: {
4344
+ id: this.id,
4345
+ loadingState: reason,
4346
+ // @ts-expect-error - This is a private property
4347
+ _subscriptionScope: this
4348
+ },
4349
+ $isLoaded: false
4350
+ };
4351
+ this.unloadedValue = unloadedValue;
4352
+ return unloadedValue;
4353
+ }
4337
4354
  getCurrentValue() {
4355
+ const rawValue = this.getCurrentRawValue();
4356
+ if (rawValue === CoValueLoadingState.UNAUTHORIZED || rawValue === CoValueLoadingState.UNAVAILABLE || rawValue === CoValueLoadingState.LOADING) {
4357
+ return this.getUnloadedValue(rawValue);
4358
+ }
4359
+ return rawValue;
4360
+ }
4361
+ getCurrentRawValue() {
4338
4362
  if (this.value.type === CoValueLoadingState.UNAUTHORIZED || this.value.type === CoValueLoadingState.UNAVAILABLE) {
4339
4363
  console.error(this.value.toString());
4340
4364
  return this.value.type;
@@ -4414,7 +4438,7 @@ var SubscriptionScope = class _SubscriptionScope {
4414
4438
  return;
4415
4439
  }
4416
4440
  this.subscription.pullValue();
4417
- const value = this.getCurrentValue();
4441
+ const value = this.getCurrentRawValue();
4418
4442
  if (typeof value !== "string") {
4419
4443
  listener({
4420
4444
  type: CoValueLoadingState.LOADED,
@@ -4542,6 +4566,10 @@ var SubscriptionScope = class _SubscriptionScope {
4542
4566
  if (key === "$onError") {
4543
4567
  return void 0;
4544
4568
  }
4569
+ const skipInvalid = typeof depth === "object" && depth.$onError === "catch";
4570
+ if (skipInvalid) {
4571
+ this.skipInvalidKeys.add(key);
4572
+ }
4545
4573
  const id = map.$jazz.raw.get(key);
4546
4574
  const descriptor = map.$jazz.getDescriptor(key);
4547
4575
  if (!descriptor) {
@@ -4677,12 +4705,12 @@ function accessChildByKey(parent, childId, key) {
4677
4705
  const value = subscriptionScope.childValues.get(childId);
4678
4706
  if (value?.type === CoValueLoadingState.LOADED) {
4679
4707
  return value.value;
4680
- } else {
4681
- return createUnloadedCoValue(
4682
- childId,
4683
- value?.type ?? CoValueLoadingState.LOADING
4684
- );
4685
4708
  }
4709
+ const childNode = subscriptionScope.childNodes.get(childId);
4710
+ if (!childNode) {
4711
+ return createUnloadedCoValue(childId, CoValueLoadingState.UNAVAILABLE);
4712
+ }
4713
+ return childNode.getCurrentValue();
4686
4714
  }
4687
4715
  function accessChildById(parent, childId, schema) {
4688
4716
  const subscriptionScope = getSubscriptionScope(parent);
@@ -4690,12 +4718,12 @@ function accessChildById(parent, childId, schema) {
4690
4718
  const value = subscriptionScope.childValues.get(childId);
4691
4719
  if (value?.type === CoValueLoadingState.LOADED) {
4692
4720
  return value.value;
4693
- } else {
4694
- return createUnloadedCoValue(
4695
- childId,
4696
- value?.type ?? CoValueLoadingState.LOADING
4697
- );
4698
4721
  }
4722
+ const childNode = subscriptionScope.childNodes.get(childId);
4723
+ if (!childNode) {
4724
+ return createUnloadedCoValue(childId, CoValueLoadingState.UNAVAILABLE);
4725
+ }
4726
+ return childNode.getCurrentValue();
4699
4727
  }
4700
4728
 
4701
4729
  // src/tools/implementation/createContext.ts
@@ -5848,16 +5876,28 @@ function makeCodecCoField(codec2) {
5848
5876
  }
5849
5877
  });
5850
5878
  }
5879
+ var schemaFieldCache = /* @__PURE__ */ new WeakMap();
5880
+ function cacheSchemaField(schema, value) {
5881
+ schemaFieldCache.set(schema, value);
5882
+ return value;
5883
+ }
5851
5884
  function schemaFieldToCoFieldDef(schema) {
5885
+ const cachedCoFieldDef = schemaFieldCache.get(schema);
5886
+ if (cachedCoFieldDef !== void 0) {
5887
+ return cachedCoFieldDef;
5888
+ }
5852
5889
  if (isCoValueClass(schema)) {
5853
- return coField.ref(schema);
5890
+ return cacheSchemaField(schema, coField.ref(schema));
5854
5891
  } else if (isCoValueSchema(schema)) {
5855
5892
  if (schema.builtin === "CoOptional") {
5856
- return coField.ref(schema.getCoValueClass(), {
5857
- optional: true
5858
- });
5893
+ return cacheSchemaField(
5894
+ schema,
5895
+ coField.ref(schema.getCoValueClass(), {
5896
+ optional: true
5897
+ })
5898
+ );
5859
5899
  }
5860
- return coField.ref(schema.getCoValueClass());
5900
+ return cacheSchemaField(schema, coField.ref(schema.getCoValueClass()));
5861
5901
  } else {
5862
5902
  if ("_zod" in schema) {
5863
5903
  const zodSchemaDef = schema._zod.def;
@@ -5867,35 +5907,44 @@ function schemaFieldToCoFieldDef(schema) {
5867
5907
  if (zodSchemaDef.type === "nullable" && coFieldDef === coField.optional.Date) {
5868
5908
  throw new Error("Nullable z.date() is not supported");
5869
5909
  }
5870
- return coFieldDef;
5910
+ return cacheSchemaField(schema, coFieldDef);
5871
5911
  } else if (zodSchemaDef.type === "string") {
5872
- return coField.string;
5912
+ return cacheSchemaField(schema, coField.string);
5873
5913
  } else if (zodSchemaDef.type === "number") {
5874
- return coField.number;
5914
+ return cacheSchemaField(schema, coField.number);
5875
5915
  } else if (zodSchemaDef.type === "boolean") {
5876
- return coField.boolean;
5916
+ return cacheSchemaField(schema, coField.boolean);
5877
5917
  } else if (zodSchemaDef.type === "null") {
5878
- return coField.null;
5918
+ return cacheSchemaField(schema, coField.null);
5879
5919
  } else if (zodSchemaDef.type === "enum") {
5880
- return coField.string;
5920
+ return cacheSchemaField(schema, coField.string);
5881
5921
  } else if (zodSchemaDef.type === "readonly") {
5882
- return schemaFieldToCoFieldDef(
5883
- schema.def.innerType
5922
+ return cacheSchemaField(
5923
+ schema,
5924
+ schemaFieldToCoFieldDef(
5925
+ schema.def.innerType
5926
+ )
5884
5927
  );
5885
5928
  } else if (zodSchemaDef.type === "date") {
5886
- return coField.optional.Date;
5929
+ return cacheSchemaField(schema, coField.optional.Date);
5887
5930
  } else if (zodSchemaDef.type === "template_literal") {
5888
- return coField.string;
5931
+ return cacheSchemaField(schema, coField.string);
5889
5932
  } else if (zodSchemaDef.type === "lazy") {
5890
- return schemaFieldToCoFieldDef(
5891
- schema.unwrap()
5933
+ return cacheSchemaField(
5934
+ schema,
5935
+ schemaFieldToCoFieldDef(
5936
+ schema.unwrap()
5937
+ )
5892
5938
  );
5893
5939
  } else if (zodSchemaDef.type === "default" || zodSchemaDef.type === "catch") {
5894
5940
  console.warn(
5895
5941
  "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored."
5896
5942
  );
5897
- return schemaFieldToCoFieldDef(
5898
- schema.def.innerType
5943
+ return cacheSchemaField(
5944
+ schema,
5945
+ schemaFieldToCoFieldDef(
5946
+ schema.def.innerType
5947
+ )
5899
5948
  );
5900
5949
  } else if (zodSchemaDef.type === "literal") {
5901
5950
  if (zodSchemaDef.values.some((literal2) => typeof literal2 === "undefined")) {
@@ -5907,14 +5956,17 @@ function schemaFieldToCoFieldDef(schema) {
5907
5956
  if (zodSchemaDef.values.some((literal2) => typeof literal2 === "bigint")) {
5908
5957
  throw new Error("z.literal() with bigint is not supported");
5909
5958
  }
5910
- return coField.literal(
5911
- ...zodSchemaDef.values
5959
+ return cacheSchemaField(
5960
+ schema,
5961
+ coField.literal(
5962
+ ...zodSchemaDef.values
5963
+ )
5912
5964
  );
5913
5965
  } else if (zodSchemaDef.type === "object" || zodSchemaDef.type === "record" || zodSchemaDef.type === "array" || zodSchemaDef.type === "tuple" || zodSchemaDef.type === "intersection") {
5914
- return coField.json();
5966
+ return cacheSchemaField(schema, coField.json());
5915
5967
  } else if (zodSchemaDef.type === "union") {
5916
5968
  if (isUnionOfPrimitivesDeeply(schema)) {
5917
- return coField.json();
5969
+ return cacheSchemaField(schema, coField.json());
5918
5970
  } else {
5919
5971
  throw new Error(
5920
5972
  "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead."
@@ -5935,8 +5987,11 @@ function schemaFieldToCoFieldDef(schema) {
5935
5987
  }
5936
5988
  throw error;
5937
5989
  }
5938
- return makeCodecCoField(
5939
- schema
5990
+ return cacheSchemaField(
5991
+ schema,
5992
+ makeCodecCoField(
5993
+ schema
5994
+ )
5940
5995
  );
5941
5996
  } else {
5942
5997
  throw new Error(
@@ -6540,6 +6595,16 @@ globalThis.devtoolsFormatters = [
6540
6595
  function isCoValueClass(value) {
6541
6596
  return typeof value === "function" && value.fromRaw !== void 0;
6542
6597
  }
6598
+ var unloadedCoValueStates = /* @__PURE__ */ new Map();
6599
+ function getUnloadedCoValueWithoutId(loadingState) {
6600
+ const value = unloadedCoValueStates.get(loadingState);
6601
+ if (value) {
6602
+ return value;
6603
+ }
6604
+ const newValue = createUnloadedCoValue("", loadingState);
6605
+ unloadedCoValueStates.set(loadingState, newValue);
6606
+ return newValue;
6607
+ }
6543
6608
  function createUnloadedCoValue(id, loadingState) {
6544
6609
  return {
6545
6610
  $jazz: { id, loadingState },
@@ -6563,12 +6628,8 @@ function loadCoValue(cls, id, options) {
6563
6628
  loadAs: options.loadAs,
6564
6629
  syncResolution: true,
6565
6630
  skipRetry: options.skipRetry,
6566
- onUnavailable: () => {
6567
- resolve(createUnloadedCoValue(id, CoValueLoadingState.UNAVAILABLE));
6568
- },
6569
- onUnauthorized: () => {
6570
- resolve(createUnloadedCoValue(id, CoValueLoadingState.UNAUTHORIZED));
6571
- },
6631
+ onUnavailable: resolve,
6632
+ onUnauthorized: resolve,
6572
6633
  unstable_branch: options.unstable_branch
6573
6634
  },
6574
6635
  (value, unsubscribe) => {
@@ -6645,29 +6706,35 @@ function subscribeToCoValue(cls, id, options, listener) {
6645
6706
  false,
6646
6707
  options.unstable_branch
6647
6708
  );
6648
- const handleUpdate = (value) => {
6709
+ const handleUpdate = () => {
6649
6710
  if (unsubscribed) return;
6650
- if (value.type === CoValueLoadingState.UNAVAILABLE) {
6651
- options.onUnavailable?.();
6652
- if (!options.skipRetry) {
6711
+ const value = rootNode.getCurrentValue();
6712
+ if (value.$isLoaded) {
6713
+ listener(value, unsubscribe);
6714
+ return;
6715
+ }
6716
+ switch (value.$jazz.loadingState) {
6717
+ case CoValueLoadingState.UNAVAILABLE:
6718
+ options.onUnavailable?.(value);
6719
+ if (!options.skipRetry) {
6720
+ console.error(value.toString());
6721
+ }
6722
+ break;
6723
+ case CoValueLoadingState.UNAUTHORIZED:
6724
+ options.onUnauthorized?.(value);
6653
6725
  console.error(value.toString());
6654
- }
6655
- } else if (value.type === CoValueLoadingState.UNAUTHORIZED) {
6656
- options.onUnauthorized?.();
6657
- console.error(value.toString());
6658
- } else if (value.type === CoValueLoadingState.LOADED) {
6659
- listener(value.value, unsubscribe);
6726
+ break;
6660
6727
  }
6661
6728
  };
6662
6729
  let shouldDefer = !options.syncResolution;
6663
- rootNode.setListener((value) => {
6730
+ rootNode.setListener(() => {
6664
6731
  if (shouldDefer) {
6665
6732
  shouldDefer = false;
6666
6733
  Promise.resolve().then(() => {
6667
- handleUpdate(value);
6734
+ handleUpdate();
6668
6735
  });
6669
6736
  } else {
6670
- handleUpdate(value);
6737
+ handleUpdate();
6671
6738
  }
6672
6739
  });
6673
6740
  function unsubscribe() {
@@ -6825,7 +6892,7 @@ function loadContentPiecesFromSubscription(subscription, valuesExported, content
6825
6892
  }
6826
6893
  valuesExported.add(subscription.id);
6827
6894
  const currentValue = subscription.getCurrentValue();
6828
- if (typeof currentValue !== "string") {
6895
+ if (currentValue.$isLoaded) {
6829
6896
  const core2 = currentValue.$jazz.raw.core;
6830
6897
  loadContentPiecesFromCoValue(core2, valuesExported, contentPieces);
6831
6898
  }
@@ -6903,6 +6970,7 @@ async function unstable_mergeBranchWithResolve(cls, id, options) {
6903
6970
 
6904
6971
  export {
6905
6972
  zodReExport_exports,
6973
+ getUnloadedCoValueWithoutId,
6906
6974
  createUnloadedCoValue,
6907
6975
  loadCoValue,
6908
6976
  subscribeToCoValue,
@@ -6948,4 +7016,4 @@ export {
6948
7016
  JazzContextManager
6949
7017
  };
6950
7018
  /* istanbul ignore file -- @preserve */
6951
- //# sourceMappingURL=chunk-JPWM4CS2.js.map
7019
+ //# sourceMappingURL=chunk-PT7FCV26.js.map