jazz-tools 0.19.3 → 0.19.5

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 (75) 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 +65 -65
  5. package/CHANGELOG.md +23 -0
  6. package/dist/{chunk-JPWM4CS2.js → chunk-DFFRRRRF.js} +137 -77
  7. package/dist/chunk-DFFRRRRF.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/request.d.ts.map +1 -1
  36. package/dist/tools/exports.d.ts +1 -1
  37. package/dist/tools/exports.d.ts.map +1 -1
  38. package/dist/tools/implementation/refs.d.ts +1 -1
  39. package/dist/tools/implementation/refs.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +3 -1
  41. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -1
  42. package/dist/tools/subscribe/SubscriptionScope.d.ts +5 -2
  43. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  44. package/dist/tools/subscribe/index.d.ts +1 -1
  45. package/dist/tools/subscribe/index.d.ts.map +1 -1
  46. package/dist/tools/subscribe/types.d.ts +2 -1
  47. package/dist/tools/subscribe/types.d.ts.map +1 -1
  48. package/dist/tools/tests/SubscriptionScope.test.d.ts +2 -0
  49. package/dist/tools/tests/SubscriptionScope.test.d.ts.map +1 -0
  50. package/package.json +4 -4
  51. package/src/inspector/tests/utils/history.test.ts +233 -2
  52. package/src/inspector/tests/viewer/co-plain-text-view.test.tsx +125 -0
  53. package/src/inspector/tests/viewer/history-view.test.tsx +134 -2
  54. package/src/inspector/utils/history.ts +168 -1
  55. package/src/inspector/viewer/co-plain-text-view.tsx +102 -3
  56. package/src/inspector/viewer/history-view.tsx +5 -25
  57. package/src/inspector/viewer/page.tsx +8 -1
  58. package/src/inspector/viewer/use-resolve-covalue.ts +2 -6
  59. package/src/react-core/hooks.ts +5 -29
  60. package/src/svelte/jazz.class.svelte.ts +16 -34
  61. package/src/tools/coValues/coFeed.ts +10 -7
  62. package/src/tools/coValues/coMap.ts +10 -7
  63. package/src/tools/coValues/group.ts +6 -2
  64. package/src/tools/coValues/interfaces.ts +48 -28
  65. package/src/tools/coValues/request.ts +12 -8
  66. package/src/tools/exports.ts +1 -0
  67. package/src/tools/implementation/refs.ts +9 -17
  68. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +62 -30
  69. package/src/tools/subscribe/SubscriptionScope.ts +38 -2
  70. package/src/tools/subscribe/index.ts +28 -13
  71. package/src/tools/subscribe/types.ts +5 -2
  72. package/src/tools/tests/SubscriptionScope.test.ts +397 -0
  73. package/src/tools/tests/deepLoading.test.ts +22 -0
  74. package/src/tools/tests/subscribe.test.ts +69 -0
  75. 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,22 @@ 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 = createUnloadedCoValue(this.id, reason);
4343
+ this.unloadedValue = unloadedValue;
4344
+ return unloadedValue;
4345
+ }
4337
4346
  getCurrentValue() {
4347
+ const rawValue = this.getCurrentRawValue();
4348
+ if (rawValue === CoValueLoadingState.UNAUTHORIZED || rawValue === CoValueLoadingState.UNAVAILABLE || rawValue === CoValueLoadingState.LOADING) {
4349
+ return this.getUnloadedValue(rawValue);
4350
+ }
4351
+ return rawValue;
4352
+ }
4353
+ getCurrentRawValue() {
4338
4354
  if (this.value.type === CoValueLoadingState.UNAUTHORIZED || this.value.type === CoValueLoadingState.UNAVAILABLE) {
4339
4355
  console.error(this.value.toString());
4340
4356
  return this.value.type;
@@ -4414,7 +4430,7 @@ var SubscriptionScope = class _SubscriptionScope {
4414
4430
  return;
4415
4431
  }
4416
4432
  this.subscription.pullValue();
4417
- const value = this.getCurrentValue();
4433
+ const value = this.getCurrentRawValue();
4418
4434
  if (typeof value !== "string") {
4419
4435
  listener({
4420
4436
  type: CoValueLoadingState.LOADED,
@@ -4542,6 +4558,10 @@ var SubscriptionScope = class _SubscriptionScope {
4542
4558
  if (key === "$onError") {
4543
4559
  return void 0;
4544
4560
  }
4561
+ const skipInvalid = typeof depth === "object" && depth.$onError === "catch";
4562
+ if (skipInvalid) {
4563
+ this.skipInvalidKeys.add(key);
4564
+ }
4545
4565
  const id = map.$jazz.raw.get(key);
4546
4566
  const descriptor = map.$jazz.getDescriptor(key);
4547
4567
  if (!descriptor) {
@@ -4677,12 +4697,12 @@ function accessChildByKey(parent, childId, key) {
4677
4697
  const value = subscriptionScope.childValues.get(childId);
4678
4698
  if (value?.type === CoValueLoadingState.LOADED) {
4679
4699
  return value.value;
4680
- } else {
4681
- return createUnloadedCoValue(
4682
- childId,
4683
- value?.type ?? CoValueLoadingState.LOADING
4684
- );
4685
4700
  }
4701
+ const childNode = subscriptionScope.childNodes.get(childId);
4702
+ if (!childNode) {
4703
+ return createUnloadedCoValue(childId, CoValueLoadingState.UNAVAILABLE);
4704
+ }
4705
+ return childNode.getCurrentValue();
4686
4706
  }
4687
4707
  function accessChildById(parent, childId, schema) {
4688
4708
  const subscriptionScope = getSubscriptionScope(parent);
@@ -4690,12 +4710,12 @@ function accessChildById(parent, childId, schema) {
4690
4710
  const value = subscriptionScope.childValues.get(childId);
4691
4711
  if (value?.type === CoValueLoadingState.LOADED) {
4692
4712
  return value.value;
4693
- } else {
4694
- return createUnloadedCoValue(
4695
- childId,
4696
- value?.type ?? CoValueLoadingState.LOADING
4697
- );
4698
4713
  }
4714
+ const childNode = subscriptionScope.childNodes.get(childId);
4715
+ if (!childNode) {
4716
+ return createUnloadedCoValue(childId, CoValueLoadingState.UNAVAILABLE);
4717
+ }
4718
+ return childNode.getCurrentValue();
4699
4719
  }
4700
4720
 
4701
4721
  // src/tools/implementation/createContext.ts
@@ -5848,16 +5868,28 @@ function makeCodecCoField(codec2) {
5848
5868
  }
5849
5869
  });
5850
5870
  }
5871
+ var schemaFieldCache = /* @__PURE__ */ new WeakMap();
5872
+ function cacheSchemaField(schema, value) {
5873
+ schemaFieldCache.set(schema, value);
5874
+ return value;
5875
+ }
5851
5876
  function schemaFieldToCoFieldDef(schema) {
5877
+ const cachedCoFieldDef = schemaFieldCache.get(schema);
5878
+ if (cachedCoFieldDef !== void 0) {
5879
+ return cachedCoFieldDef;
5880
+ }
5852
5881
  if (isCoValueClass(schema)) {
5853
- return coField.ref(schema);
5882
+ return cacheSchemaField(schema, coField.ref(schema));
5854
5883
  } else if (isCoValueSchema(schema)) {
5855
5884
  if (schema.builtin === "CoOptional") {
5856
- return coField.ref(schema.getCoValueClass(), {
5857
- optional: true
5858
- });
5885
+ return cacheSchemaField(
5886
+ schema,
5887
+ coField.ref(schema.getCoValueClass(), {
5888
+ optional: true
5889
+ })
5890
+ );
5859
5891
  }
5860
- return coField.ref(schema.getCoValueClass());
5892
+ return cacheSchemaField(schema, coField.ref(schema.getCoValueClass()));
5861
5893
  } else {
5862
5894
  if ("_zod" in schema) {
5863
5895
  const zodSchemaDef = schema._zod.def;
@@ -5867,35 +5899,44 @@ function schemaFieldToCoFieldDef(schema) {
5867
5899
  if (zodSchemaDef.type === "nullable" && coFieldDef === coField.optional.Date) {
5868
5900
  throw new Error("Nullable z.date() is not supported");
5869
5901
  }
5870
- return coFieldDef;
5902
+ return cacheSchemaField(schema, coFieldDef);
5871
5903
  } else if (zodSchemaDef.type === "string") {
5872
- return coField.string;
5904
+ return cacheSchemaField(schema, coField.string);
5873
5905
  } else if (zodSchemaDef.type === "number") {
5874
- return coField.number;
5906
+ return cacheSchemaField(schema, coField.number);
5875
5907
  } else if (zodSchemaDef.type === "boolean") {
5876
- return coField.boolean;
5908
+ return cacheSchemaField(schema, coField.boolean);
5877
5909
  } else if (zodSchemaDef.type === "null") {
5878
- return coField.null;
5910
+ return cacheSchemaField(schema, coField.null);
5879
5911
  } else if (zodSchemaDef.type === "enum") {
5880
- return coField.string;
5912
+ return cacheSchemaField(schema, coField.string);
5881
5913
  } else if (zodSchemaDef.type === "readonly") {
5882
- return schemaFieldToCoFieldDef(
5883
- schema.def.innerType
5914
+ return cacheSchemaField(
5915
+ schema,
5916
+ schemaFieldToCoFieldDef(
5917
+ schema.def.innerType
5918
+ )
5884
5919
  );
5885
5920
  } else if (zodSchemaDef.type === "date") {
5886
- return coField.optional.Date;
5921
+ return cacheSchemaField(schema, coField.optional.Date);
5887
5922
  } else if (zodSchemaDef.type === "template_literal") {
5888
- return coField.string;
5923
+ return cacheSchemaField(schema, coField.string);
5889
5924
  } else if (zodSchemaDef.type === "lazy") {
5890
- return schemaFieldToCoFieldDef(
5891
- schema.unwrap()
5925
+ return cacheSchemaField(
5926
+ schema,
5927
+ schemaFieldToCoFieldDef(
5928
+ schema.unwrap()
5929
+ )
5892
5930
  );
5893
5931
  } else if (zodSchemaDef.type === "default" || zodSchemaDef.type === "catch") {
5894
5932
  console.warn(
5895
5933
  "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored."
5896
5934
  );
5897
- return schemaFieldToCoFieldDef(
5898
- schema.def.innerType
5935
+ return cacheSchemaField(
5936
+ schema,
5937
+ schemaFieldToCoFieldDef(
5938
+ schema.def.innerType
5939
+ )
5899
5940
  );
5900
5941
  } else if (zodSchemaDef.type === "literal") {
5901
5942
  if (zodSchemaDef.values.some((literal2) => typeof literal2 === "undefined")) {
@@ -5907,14 +5948,17 @@ function schemaFieldToCoFieldDef(schema) {
5907
5948
  if (zodSchemaDef.values.some((literal2) => typeof literal2 === "bigint")) {
5908
5949
  throw new Error("z.literal() with bigint is not supported");
5909
5950
  }
5910
- return coField.literal(
5911
- ...zodSchemaDef.values
5951
+ return cacheSchemaField(
5952
+ schema,
5953
+ coField.literal(
5954
+ ...zodSchemaDef.values
5955
+ )
5912
5956
  );
5913
5957
  } else if (zodSchemaDef.type === "object" || zodSchemaDef.type === "record" || zodSchemaDef.type === "array" || zodSchemaDef.type === "tuple" || zodSchemaDef.type === "intersection") {
5914
- return coField.json();
5958
+ return cacheSchemaField(schema, coField.json());
5915
5959
  } else if (zodSchemaDef.type === "union") {
5916
5960
  if (isUnionOfPrimitivesDeeply(schema)) {
5917
- return coField.json();
5961
+ return cacheSchemaField(schema, coField.json());
5918
5962
  } else {
5919
5963
  throw new Error(
5920
5964
  "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead."
@@ -5935,8 +5979,11 @@ function schemaFieldToCoFieldDef(schema) {
5935
5979
  }
5936
5980
  throw error;
5937
5981
  }
5938
- return makeCodecCoField(
5939
- schema
5982
+ return cacheSchemaField(
5983
+ schema,
5984
+ makeCodecCoField(
5985
+ schema
5986
+ )
5940
5987
  );
5941
5988
  } else {
5942
5989
  throw new Error(
@@ -6540,6 +6587,16 @@ globalThis.devtoolsFormatters = [
6540
6587
  function isCoValueClass(value) {
6541
6588
  return typeof value === "function" && value.fromRaw !== void 0;
6542
6589
  }
6590
+ var unloadedCoValueStates = /* @__PURE__ */ new Map();
6591
+ function getUnloadedCoValueWithoutId(loadingState) {
6592
+ const value = unloadedCoValueStates.get(loadingState);
6593
+ if (value) {
6594
+ return value;
6595
+ }
6596
+ const newValue = createUnloadedCoValue("", loadingState);
6597
+ unloadedCoValueStates.set(loadingState, newValue);
6598
+ return newValue;
6599
+ }
6543
6600
  function createUnloadedCoValue(id, loadingState) {
6544
6601
  return {
6545
6602
  $jazz: { id, loadingState },
@@ -6563,12 +6620,8 @@ function loadCoValue(cls, id, options) {
6563
6620
  loadAs: options.loadAs,
6564
6621
  syncResolution: true,
6565
6622
  skipRetry: options.skipRetry,
6566
- onUnavailable: () => {
6567
- resolve(createUnloadedCoValue(id, CoValueLoadingState.UNAVAILABLE));
6568
- },
6569
- onUnauthorized: () => {
6570
- resolve(createUnloadedCoValue(id, CoValueLoadingState.UNAUTHORIZED));
6571
- },
6623
+ onUnavailable: resolve,
6624
+ onUnauthorized: resolve,
6572
6625
  unstable_branch: options.unstable_branch
6573
6626
  },
6574
6627
  (value, unsubscribe) => {
@@ -6645,29 +6698,35 @@ function subscribeToCoValue(cls, id, options, listener) {
6645
6698
  false,
6646
6699
  options.unstable_branch
6647
6700
  );
6648
- const handleUpdate = (value) => {
6701
+ const handleUpdate = () => {
6649
6702
  if (unsubscribed) return;
6650
- if (value.type === CoValueLoadingState.UNAVAILABLE) {
6651
- options.onUnavailable?.();
6652
- if (!options.skipRetry) {
6703
+ const value = rootNode.getCurrentValue();
6704
+ if (value.$isLoaded) {
6705
+ listener(value, unsubscribe);
6706
+ return;
6707
+ }
6708
+ switch (value.$jazz.loadingState) {
6709
+ case CoValueLoadingState.UNAVAILABLE:
6710
+ options.onUnavailable?.(value);
6711
+ if (!options.skipRetry) {
6712
+ console.error(value.toString());
6713
+ }
6714
+ break;
6715
+ case CoValueLoadingState.UNAUTHORIZED:
6716
+ options.onUnauthorized?.(value);
6653
6717
  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);
6718
+ break;
6660
6719
  }
6661
6720
  };
6662
6721
  let shouldDefer = !options.syncResolution;
6663
- rootNode.setListener((value) => {
6722
+ rootNode.setListener(() => {
6664
6723
  if (shouldDefer) {
6665
6724
  shouldDefer = false;
6666
6725
  Promise.resolve().then(() => {
6667
- handleUpdate(value);
6726
+ handleUpdate();
6668
6727
  });
6669
6728
  } else {
6670
- handleUpdate(value);
6729
+ handleUpdate();
6671
6730
  }
6672
6731
  });
6673
6732
  function unsubscribe() {
@@ -6825,7 +6884,7 @@ function loadContentPiecesFromSubscription(subscription, valuesExported, content
6825
6884
  }
6826
6885
  valuesExported.add(subscription.id);
6827
6886
  const currentValue = subscription.getCurrentValue();
6828
- if (typeof currentValue !== "string") {
6887
+ if (currentValue.$isLoaded) {
6829
6888
  const core2 = currentValue.$jazz.raw.core;
6830
6889
  loadContentPiecesFromCoValue(core2, valuesExported, contentPieces);
6831
6890
  }
@@ -6903,6 +6962,7 @@ async function unstable_mergeBranchWithResolve(cls, id, options) {
6903
6962
 
6904
6963
  export {
6905
6964
  zodReExport_exports,
6965
+ getUnloadedCoValueWithoutId,
6906
6966
  createUnloadedCoValue,
6907
6967
  loadCoValue,
6908
6968
  subscribeToCoValue,
@@ -6948,4 +7008,4 @@ export {
6948
7008
  JazzContextManager
6949
7009
  };
6950
7010
  /* istanbul ignore file -- @preserve */
6951
- //# sourceMappingURL=chunk-JPWM4CS2.js.map
7011
+ //# sourceMappingURL=chunk-DFFRRRRF.js.map