@plasmicapp/data-sources 0.1.209 → 1.0.1

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.
package/dist/index.js CHANGED
@@ -75,11 +75,9 @@ __export(src_exports, {
75
75
  makeCacheKey: () => makeCacheKey,
76
76
  makeQueryCacheKey: () => makeQueryCacheKey,
77
77
  normalizeData: () => normalizeData,
78
- unstable_createDollarQueries: () => createDollarQueries,
79
78
  unstable_executePlasmicQueries: () => executePlasmicQueries,
80
79
  unstable_usePlasmicQueries: () => usePlasmicQueries,
81
80
  unstable_wrapDollarQueriesForMetadata: () => wrapDollarQueriesForMetadata,
82
- useDependencyAwareQuery: () => useDependencyAwareQuery,
83
81
  useNormalizedData: () => useNormalizedData,
84
82
  usePlasmicDataConfig: () => import_query5.usePlasmicDataConfig,
85
83
  usePlasmicDataMutationOp: () => usePlasmicDataMutationOp,
@@ -95,13 +93,6 @@ var React3 = __toESM(require("react"));
95
93
  // src/utils.ts
96
94
  function noopFn() {
97
95
  }
98
- function swallow(f) {
99
- try {
100
- return f();
101
- } catch (e) {
102
- return void 0;
103
- }
104
- }
105
96
  function pick(obj, ...keys) {
106
97
  const res = {};
107
98
  for (const key of keys) {
@@ -437,16 +428,29 @@ var StatefulQueryResult = class {
437
428
  }
438
429
  };
439
430
  function safeExec(tryData, ifUndefined, ifError) {
431
+ const result = safeExecResult(tryData);
432
+ if ("promise" in result) {
433
+ return ifUndefined(result.promise);
434
+ } else if ("error" in result) {
435
+ return ifError(result.error);
436
+ } else {
437
+ return result.data;
438
+ }
439
+ }
440
+ function safeExecResult(tryData) {
440
441
  try {
441
- return tryData();
442
+ return { data: tryData() };
442
443
  } catch (err) {
443
444
  if (isPlasmicUndefinedDataErrorPromise(err)) {
444
- return ifUndefined(err);
445
+ return { promise: err };
445
446
  } else {
446
- return ifError(err);
447
+ return { error: err };
447
448
  }
448
449
  }
449
450
  }
451
+ function assertUnexpectedNodeType(x) {
452
+ throw new Error(`Unexpected node type: ${x}`);
453
+ }
450
454
  function resolveParams(params) {
451
455
  return safeExec(
452
456
  () => ({
@@ -527,6 +531,19 @@ var SyncPromise = class {
527
531
  );
528
532
  }
529
533
  };
534
+ function shallowEqualRecords(a, b) {
535
+ if (Object.is(a, b)) {
536
+ return true;
537
+ }
538
+ const aKeys = Object.keys(a);
539
+ const bKeys = Object.keys(b);
540
+ if (aKeys.length !== bKeys.length) {
541
+ return false;
542
+ }
543
+ return aKeys.every(
544
+ (key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key])
545
+ );
546
+ }
530
547
  var SettablePromise = class {
531
548
  constructor() {
532
549
  this.promise = new Promise((resolve, reject) => {
@@ -622,7 +639,31 @@ function sortObjectsDeep(value, visitedObjects) {
622
639
 
623
640
  // src/serverQueries/client.ts
624
641
  var GLOBAL_CACHE = /* @__PURE__ */ new Map();
625
- function usePlasmicQueries($queries, queries) {
642
+ function usePlasmicQueries(tree, $props, $ctx, $state) {
643
+ const stableProps = useShallowStableRecord($props);
644
+ const stableCtx = useShallowStableRecord($ctx);
645
+ const $stateRef = React3.useRef($state != null ? $state : {});
646
+ $stateRef.current = $state != null ? $state : {};
647
+ const $queries = React3.useMemo(
648
+ () => createDollarQueries(Object.keys(tree.queries)),
649
+ [tree]
650
+ );
651
+ const queries = React3.useMemo(() => {
652
+ return mapRecords(
653
+ (_name, q) => ({
654
+ id: q.id,
655
+ fn: q.fn,
656
+ execParams: () => q.args({
657
+ $q: $queries,
658
+ $props: stableProps,
659
+ $ctx: stableCtx,
660
+ $state: $stateRef.current,
661
+ $scopedItemVars: {}
662
+ })
663
+ }),
664
+ tree.queries
665
+ );
666
+ }, [$queries, stableCtx, stableProps, tree]);
626
667
  const wrappedQueries = React3.useMemo(() => wrapQueries(queries), [queries]);
627
668
  const $queryStates = $queries;
628
669
  const { fallback: prefetchedCache, cache: swrCache } = (0, import_query2.usePlasmicDataConfig)();
@@ -689,13 +730,14 @@ function usePlasmicQueries($queries, queries) {
689
730
  },
690
731
  [wrappedQueries, $queryStates, settledCount]
691
732
  );
692
- return mapRecords(
733
+ mapRecords(
693
734
  (_queryName, $query, query) => {
694
- return usePlasmicQuery($query, query, settledCount);
735
+ usePlasmicQuery($query, query, settledCount);
695
736
  },
696
737
  $queryStates,
697
738
  wrappedQueries
698
739
  );
740
+ return $queries;
699
741
  }
700
742
  function wrapQueries(queries) {
701
743
  return mapRecords((_queryName, query) => {
@@ -820,24 +862,261 @@ function usePlasmicQuery($query, query, settledCount) {
820
862
  }
821
863
  return result;
822
864
  }
865
+ function useShallowStableRecord(value) {
866
+ const ref = React3.useRef(value);
867
+ if (!shallowEqualRecords(ref.current, value)) {
868
+ ref.current = value;
869
+ }
870
+ return ref.current;
871
+ }
823
872
 
824
873
  // src/serverQueries/server.ts
825
- function executePlasmicQueries($queries, queries) {
826
- return __async(this, null, function* () {
827
- const doneQueryResults = yield Promise.all(
828
- mapRecordEntries(
829
- (_queryName, $query, query) => {
830
- return executePlasmicQuery($query, query);
831
- },
832
- $queries,
833
- queries
834
- )
874
+ var ROOT_COMPONENT_KEY_PATH = "root";
875
+ function appendKeyPath(currentKeyPath, currentInput) {
876
+ return currentKeyPath ? `${currentKeyPath}/${currentInput}` : currentInput;
877
+ }
878
+ function executeQueryTree(rootNode, options, queriesByComponent) {
879
+ const { $props, $ctx } = options;
880
+ const initialContext = {
881
+ $props,
882
+ $ctx,
883
+ $state: {},
884
+ $q: {},
885
+ $scopedItemVars: {}
886
+ };
887
+ return executeComponentNode(rootNode, {
888
+ context: initialContext,
889
+ parentKeyPath: "",
890
+ childIndex: 0,
891
+ queriesByComponent
892
+ });
893
+ }
894
+ function executeNode(node, params) {
895
+ const p = { node, params, type: node.type };
896
+ switch (p.type) {
897
+ case "component":
898
+ return executeComponentNode(p.node, p.params);
899
+ case "codeComponent":
900
+ return executeCodeComponentNode(p.node, p.params);
901
+ case "dataProvider":
902
+ return executeDataProviderNode(p.node, p.params);
903
+ case "visibility":
904
+ return executeVisibilityNode(p.node, p.params);
905
+ case "repeated":
906
+ return executeRepeatedNode(p.node, p.params);
907
+ default:
908
+ assertUnexpectedNodeType(p);
909
+ }
910
+ }
911
+ function executeComponentNode(node, params) {
912
+ const {
913
+ context: parentContext,
914
+ parentKeyPath,
915
+ childIndex,
916
+ queriesByComponent
917
+ } = params;
918
+ const isRoot = parentKeyPath === "";
919
+ let componentKeyPath;
920
+ let evaluatedProps;
921
+ if (isRoot) {
922
+ componentKeyPath = ROOT_COMPONENT_KEY_PATH;
923
+ evaluatedProps = parentContext.$props;
924
+ } else {
925
+ componentKeyPath = appendKeyPath(parentKeyPath, `c${childIndex}`);
926
+ evaluatedProps = {};
927
+ for (const [propName, propFn] of Object.entries(node.propsContext)) {
928
+ const result = safeExecResult(() => propFn(parentContext));
929
+ if (!("data" in result)) {
930
+ return [];
931
+ }
932
+ evaluatedProps[propName] = result.data;
933
+ }
934
+ }
935
+ let componentQueries = queriesByComponent.get(componentKeyPath);
936
+ if (!componentQueries) {
937
+ componentQueries = {};
938
+ queriesByComponent.set(componentKeyPath, componentQueries);
939
+ }
940
+ const componentContext = {
941
+ $props: evaluatedProps,
942
+ $ctx: parentContext.$ctx,
943
+ $state: parentContext.$state,
944
+ $q: componentQueries,
945
+ $scopedItemVars: parentContext.$scopedItemVars
946
+ };
947
+ const discovered = [];
948
+ for (const [queryName, query] of Object.entries(node.queries)) {
949
+ if (componentQueries[queryName]) {
950
+ continue;
951
+ }
952
+ const $query = new StatefulQueryResult();
953
+ componentQueries[queryName] = $query;
954
+ const capturedContext = componentContext;
955
+ const capturedArgsFn = query.args;
956
+ const plasmicQuery = {
957
+ id: query.id,
958
+ fn: query.fn,
959
+ execParams: () => capturedArgsFn(capturedContext)
960
+ };
961
+ discovered.push({ $query, query: plasmicQuery });
962
+ }
963
+ node.children.forEach((child, idx) => {
964
+ discovered.push(
965
+ ...executeNode(child, {
966
+ context: componentContext,
967
+ parentKeyPath: componentKeyPath,
968
+ childIndex: idx,
969
+ queriesByComponent
970
+ })
835
971
  );
836
- return Object.fromEntries(
837
- Object.values(doneQueryResults).map(($query) => [$query.key, $query.data])
972
+ });
973
+ return discovered;
974
+ }
975
+ function executeCodeComponentNode(node, params) {
976
+ if (node.serverRenderingConfig === false) {
977
+ return [];
978
+ }
979
+ const { context, parentKeyPath, childIndex, queriesByComponent } = params;
980
+ const nodeKeyPath = appendKeyPath(parentKeyPath, `cc${childIndex}`);
981
+ const evaluatedProps = {};
982
+ for (const [propName, propFn] of Object.entries(node.propsContext)) {
983
+ const result = safeExecResult(() => propFn(context));
984
+ if ("data" in result) {
985
+ evaluatedProps[propName] = result.data;
986
+ } else {
987
+ return [];
988
+ }
989
+ }
990
+ const childContext = {
991
+ $props: evaluatedProps,
992
+ $ctx: context.$ctx,
993
+ $state: context.$state,
994
+ $q: context.$q,
995
+ $scopedItemVars: context.$scopedItemVars
996
+ };
997
+ return node.children.flatMap(
998
+ (child, idx) => executeNode(child, {
999
+ context: childContext,
1000
+ parentKeyPath: nodeKeyPath,
1001
+ childIndex: idx,
1002
+ queriesByComponent
1003
+ })
1004
+ );
1005
+ }
1006
+ function executeDataProviderNode(node, params) {
1007
+ const { context, parentKeyPath, childIndex, queriesByComponent } = params;
1008
+ const nodeKeyPath = appendKeyPath(parentKeyPath, `dp${childIndex}`);
1009
+ const dataResult = safeExecResult(() => node.data(context));
1010
+ if (!("data" in dataResult)) {
1011
+ return [];
1012
+ }
1013
+ const childContext = {
1014
+ $props: context.$props,
1015
+ $ctx: __spreadProps(__spreadValues({}, context.$ctx), {
1016
+ [node.name]: dataResult.data
1017
+ }),
1018
+ $state: context.$state,
1019
+ $q: context.$q,
1020
+ $scopedItemVars: context.$scopedItemVars
1021
+ };
1022
+ return node.children.flatMap(
1023
+ (child, idx) => executeNode(child, {
1024
+ context: childContext,
1025
+ parentKeyPath: nodeKeyPath,
1026
+ childIndex: idx,
1027
+ queriesByComponent
1028
+ })
1029
+ );
1030
+ }
1031
+ function executeVisibilityNode(node, params) {
1032
+ const { context, parentKeyPath, childIndex, queriesByComponent } = params;
1033
+ const nodeKeyPath = appendKeyPath(parentKeyPath, `v${childIndex}`);
1034
+ const visibilityResult = safeExecResult(() => node.visibilityExpr(context));
1035
+ if (!("data" in visibilityResult) || !visibilityResult.data) {
1036
+ return [];
1037
+ }
1038
+ return node.children.flatMap(
1039
+ (child, idx) => executeNode(child, {
1040
+ context,
1041
+ parentKeyPath: nodeKeyPath,
1042
+ childIndex: idx,
1043
+ queriesByComponent
1044
+ })
1045
+ );
1046
+ }
1047
+ function executeRepeatedNode(node, params) {
1048
+ const { context, parentKeyPath, childIndex, queriesByComponent } = params;
1049
+ const nodeKeyPath = appendKeyPath(parentKeyPath, `r${childIndex}`);
1050
+ const collectionResult = safeExecResult(() => node.collectionExpr(context));
1051
+ if (!("data" in collectionResult) || !Array.isArray(collectionResult.data)) {
1052
+ return [];
1053
+ }
1054
+ return collectionResult.data.flatMap((item, index) => {
1055
+ const itemContext = {
1056
+ $props: context.$props,
1057
+ $ctx: context.$ctx,
1058
+ $state: context.$state,
1059
+ $q: context.$q,
1060
+ $scopedItemVars: __spreadProps(__spreadValues({}, context.$scopedItemVars), {
1061
+ [node.itemName]: item,
1062
+ [node.indexName]: index
1063
+ })
1064
+ };
1065
+ const itemKeyPath = appendKeyPath(nodeKeyPath, `i${index}`);
1066
+ return node.children.flatMap(
1067
+ (child, idx) => executeNode(child, {
1068
+ context: itemContext,
1069
+ parentKeyPath: itemKeyPath,
1070
+ childIndex: idx,
1071
+ queriesByComponent
1072
+ })
838
1073
  );
839
1074
  });
840
1075
  }
1076
+ function executePlasmicQueries(rootNode, options) {
1077
+ return __async(this, null, function* () {
1078
+ const queriesByComponent = /* @__PURE__ */ new Map();
1079
+ const discoveredQueries = [];
1080
+ while (true) {
1081
+ const newQueries = executeQueryTree(rootNode, options, queriesByComponent);
1082
+ if (newQueries.length === 0) {
1083
+ break;
1084
+ }
1085
+ discoveredQueries.push(...newQueries);
1086
+ yield Promise.all(
1087
+ newQueries.map(
1088
+ (d) => executePlasmicQuery(d.$query, d.query).catch(() => {
1089
+ })
1090
+ )
1091
+ );
1092
+ }
1093
+ const cache = {};
1094
+ for (const d of discoveredQueries) {
1095
+ if ("data" in d.$query.current) {
1096
+ cache[d.$query.current.key] = d.$query.current.data;
1097
+ }
1098
+ }
1099
+ const queries = {};
1100
+ const rootQueries = queriesByComponent.get(ROOT_COMPONENT_KEY_PATH);
1101
+ if (rootQueries) {
1102
+ for (const [name, $query] of Object.entries(rootQueries)) {
1103
+ if ("data" in $query.current) {
1104
+ queries[name] = {
1105
+ key: $query.key,
1106
+ data: $query.current.data,
1107
+ isLoading: false
1108
+ };
1109
+ }
1110
+ }
1111
+ }
1112
+ for (const d of discoveredQueries) {
1113
+ if ("error" in d.$query.current) {
1114
+ throw d.$query.current.error;
1115
+ }
1116
+ }
1117
+ return { cache, queries };
1118
+ });
1119
+ }
841
1120
  function executePlasmicQuery($query, query) {
842
1121
  return __async(this, null, function* () {
843
1122
  if ($query.current.state === "loading" || $query.current.state === "done") {
@@ -1253,42 +1532,4 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema, makeDefaultConfig) {
1253
1532
  ];
1254
1533
  return { mergedFields, minimalFullLengthFields };
1255
1534
  }
1256
-
1257
- // src/hooks/useDependencyAwareQuery.tsx
1258
- var import_react5 = __toESM(require("react"));
1259
- function usePrevious(value) {
1260
- const prevValue = import_react5.default.useRef(void 0);
1261
- import_react5.default.useEffect(() => {
1262
- prevValue.current = value;
1263
- return () => {
1264
- prevValue.current = void 0;
1265
- };
1266
- });
1267
- return prevValue.current;
1268
- }
1269
- function useDependencyAwareQuery({
1270
- $queries,
1271
- getDataOp,
1272
- setDollarQueries,
1273
- name,
1274
- pageIndex,
1275
- pageSize
1276
- }) {
1277
- const data = usePlasmicDataOp(swallow(getDataOp), __spreadValues({}, !!pageIndex && !!pageSize && {
1278
- paginate: { pageIndex, pageSize }
1279
- }));
1280
- const finalName = name != null ? name : "data";
1281
- const prevName = usePrevious(finalName);
1282
- import_react5.default.useEffect(() => {
1283
- if (!(finalName in $queries) || $queries[finalName] !== data) {
1284
- const $queries2 = __spreadProps(__spreadValues({}, $queries), {
1285
- [finalName]: data
1286
- });
1287
- if (prevName && finalName !== prevName && prevName in $queries) {
1288
- delete $queries2[prevName];
1289
- }
1290
- setDollarQueries($queries2);
1291
- }
1292
- }, [finalName, prevName, data, $queries, setDollarQueries]);
1293
- }
1294
1535
  //# sourceMappingURL=index.js.map