@plasmicapp/data-sources 1.0.0 → 1.0.2
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.d.ts +93 -97
- package/dist/index.esm.js +306 -19
- package/dist/index.esm.js.map +2 -2
- package/dist/index.js +306 -19
- package/dist/index.js.map +2 -2
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -75,7 +75,6 @@ __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,
|
|
@@ -429,16 +428,29 @@ var StatefulQueryResult = class {
|
|
|
429
428
|
}
|
|
430
429
|
};
|
|
431
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) {
|
|
432
441
|
try {
|
|
433
|
-
return tryData();
|
|
442
|
+
return { data: tryData() };
|
|
434
443
|
} catch (err) {
|
|
435
444
|
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
436
|
-
return
|
|
445
|
+
return { promise: err };
|
|
437
446
|
} else {
|
|
438
|
-
return
|
|
447
|
+
return { error: err };
|
|
439
448
|
}
|
|
440
449
|
}
|
|
441
450
|
}
|
|
451
|
+
function assertUnexpectedNodeType(x) {
|
|
452
|
+
throw new Error(`Unexpected node type: ${x}`);
|
|
453
|
+
}
|
|
442
454
|
function resolveParams(params) {
|
|
443
455
|
return safeExec(
|
|
444
456
|
() => ({
|
|
@@ -519,6 +531,19 @@ var SyncPromise = class {
|
|
|
519
531
|
);
|
|
520
532
|
}
|
|
521
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
|
+
}
|
|
522
547
|
var SettablePromise = class {
|
|
523
548
|
constructor() {
|
|
524
549
|
this.promise = new Promise((resolve, reject) => {
|
|
@@ -614,7 +639,31 @@ function sortObjectsDeep(value, visitedObjects) {
|
|
|
614
639
|
|
|
615
640
|
// src/serverQueries/client.ts
|
|
616
641
|
var GLOBAL_CACHE = /* @__PURE__ */ new Map();
|
|
617
|
-
function usePlasmicQueries($
|
|
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]);
|
|
618
667
|
const wrappedQueries = React3.useMemo(() => wrapQueries(queries), [queries]);
|
|
619
668
|
const $queryStates = $queries;
|
|
620
669
|
const { fallback: prefetchedCache, cache: swrCache } = (0, import_query2.usePlasmicDataConfig)();
|
|
@@ -681,13 +730,14 @@ function usePlasmicQueries($queries, queries) {
|
|
|
681
730
|
},
|
|
682
731
|
[wrappedQueries, $queryStates, settledCount]
|
|
683
732
|
);
|
|
684
|
-
|
|
733
|
+
mapRecords(
|
|
685
734
|
(_queryName, $query, query) => {
|
|
686
|
-
|
|
735
|
+
usePlasmicQuery($query, query, settledCount);
|
|
687
736
|
},
|
|
688
737
|
$queryStates,
|
|
689
738
|
wrappedQueries
|
|
690
739
|
);
|
|
740
|
+
return $queries;
|
|
691
741
|
}
|
|
692
742
|
function wrapQueries(queries) {
|
|
693
743
|
return mapRecords((_queryName, query) => {
|
|
@@ -812,24 +862,261 @@ function usePlasmicQuery($query, query, settledCount) {
|
|
|
812
862
|
}
|
|
813
863
|
return result;
|
|
814
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
|
+
}
|
|
815
872
|
|
|
816
873
|
// src/serverQueries/server.ts
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
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
|
+
})
|
|
827
971
|
);
|
|
828
|
-
|
|
829
|
-
|
|
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
|
+
})
|
|
830
1073
|
);
|
|
831
1074
|
});
|
|
832
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
|
+
}
|
|
833
1120
|
function executePlasmicQuery($query, query) {
|
|
834
1121
|
return __async(this, null, function* () {
|
|
835
1122
|
if ($query.current.state === "loading" || $query.current.state === "done") {
|