@plasmicapp/data-sources 1.0.6 → 1.0.8
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 +45 -22
- package/dist/index.esm.js +254 -212
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +254 -212
- package/dist/index.js.map +3 -3
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -73,6 +73,7 @@ __export(index_exports, {
|
|
|
73
73
|
_StatefulQueryResult: () => StatefulQueryResult,
|
|
74
74
|
deriveFieldConfigs: () => deriveFieldConfigs,
|
|
75
75
|
executePlasmicDataOp: () => executePlasmicDataOp,
|
|
76
|
+
executeServerQuery: () => executeServerQuery,
|
|
76
77
|
makeCacheKey: () => makeCacheKey,
|
|
77
78
|
makeQueryCacheKey: () => makeQueryCacheKey,
|
|
78
79
|
normalizeData: () => normalizeData,
|
|
@@ -320,6 +321,70 @@ function mapRecords(callback, record1, record2, record3) {
|
|
|
320
321
|
|
|
321
322
|
// src/serverQueries/common.ts
|
|
322
323
|
var import_react2 = __toESM(require("react"));
|
|
324
|
+
|
|
325
|
+
// src/serverQueries/makeQueryCacheKey.ts
|
|
326
|
+
function makeQueryCacheKey(id, params) {
|
|
327
|
+
return `${id}:${safeStableStringify(params)}`;
|
|
328
|
+
}
|
|
329
|
+
var shortPlasmicPrefix = "\u03C1";
|
|
330
|
+
function safeStableStringify(unstableValue) {
|
|
331
|
+
const stableValue = sortObjectsDeep(unstableValue, /* @__PURE__ */ new Map());
|
|
332
|
+
const visitedPaths = /* @__PURE__ */ new Map();
|
|
333
|
+
return JSON.stringify(stableValue, function(key, value) {
|
|
334
|
+
switch (typeof value) {
|
|
335
|
+
case "undefined":
|
|
336
|
+
return `${shortPlasmicPrefix}:UNDEFINED`;
|
|
337
|
+
case "function":
|
|
338
|
+
return `${shortPlasmicPrefix}:FUNCTION:${value.name}`;
|
|
339
|
+
case "symbol":
|
|
340
|
+
return `${shortPlasmicPrefix}:SYMBOL:${value.description}`;
|
|
341
|
+
case "bigint":
|
|
342
|
+
return value.toString();
|
|
343
|
+
case "object":
|
|
344
|
+
if (value !== null) {
|
|
345
|
+
const cyclePath = visitedPaths.get(value);
|
|
346
|
+
if (cyclePath) {
|
|
347
|
+
return `${shortPlasmicPrefix}:REF:${cyclePath}`;
|
|
348
|
+
}
|
|
349
|
+
const parentPath = visitedPaths.get(this);
|
|
350
|
+
const valuePath = parentPath === void 0 ? "$" : `${parentPath}.${key}`;
|
|
351
|
+
visitedPaths.set(value, valuePath);
|
|
352
|
+
}
|
|
353
|
+
return value;
|
|
354
|
+
default:
|
|
355
|
+
return value;
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
function sortObjectsDeep(value, visitedObjects) {
|
|
360
|
+
if (typeof value !== "object" || value === null) {
|
|
361
|
+
return value;
|
|
362
|
+
}
|
|
363
|
+
const visitedValue = visitedObjects.get(value);
|
|
364
|
+
if (visitedValue) {
|
|
365
|
+
return visitedValue;
|
|
366
|
+
}
|
|
367
|
+
if (typeof value.toJSON === "function") {
|
|
368
|
+
return sortObjectsDeep(value.toJSON(), visitedObjects);
|
|
369
|
+
}
|
|
370
|
+
if (Array.isArray(value)) {
|
|
371
|
+
const newArr = [];
|
|
372
|
+
visitedObjects.set(value, newArr);
|
|
373
|
+
value.forEach((item, key) => {
|
|
374
|
+
newArr[key] = sortObjectsDeep(item, visitedObjects);
|
|
375
|
+
});
|
|
376
|
+
return newArr;
|
|
377
|
+
} else {
|
|
378
|
+
const newObj = {};
|
|
379
|
+
visitedObjects.set(value, newObj);
|
|
380
|
+
Object.keys(value).sort().forEach((key) => {
|
|
381
|
+
newObj[key] = sortObjectsDeep(value[key], visitedObjects);
|
|
382
|
+
});
|
|
383
|
+
return newObj;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/serverQueries/common.ts
|
|
323
388
|
function createDollarQueries(queryNames) {
|
|
324
389
|
return Object.fromEntries(
|
|
325
390
|
queryNames.map((queryName) => {
|
|
@@ -377,11 +442,6 @@ var StatefulQueryResult = class {
|
|
|
377
442
|
(err) => this.rejectPromise(key, err)
|
|
378
443
|
);
|
|
379
444
|
}
|
|
380
|
-
/**
|
|
381
|
-
* Resolve is allowed if:
|
|
382
|
-
* 1) no key / state is initial, which means we are resolving from cache
|
|
383
|
-
* 2) key / state is loading, which means we need to check the keys match
|
|
384
|
-
*/
|
|
385
445
|
resolvePromise(key, data) {
|
|
386
446
|
if (this.current.key === null || this.current.key === key) {
|
|
387
447
|
this.transitionState({
|
|
@@ -410,6 +470,9 @@ var StatefulQueryResult = class {
|
|
|
410
470
|
untagPlasmicUndefinedDataErrorPromise(this.settable.promise);
|
|
411
471
|
}
|
|
412
472
|
}
|
|
473
|
+
toJSON() {
|
|
474
|
+
return this.current;
|
|
475
|
+
}
|
|
413
476
|
get key() {
|
|
414
477
|
return this.current.key;
|
|
415
478
|
}
|
|
@@ -458,12 +521,17 @@ function safeExecResult(tryData) {
|
|
|
458
521
|
function assertUnexpectedNodeType(x) {
|
|
459
522
|
throw new Error(`Unexpected node type: ${x}`);
|
|
460
523
|
}
|
|
461
|
-
function resolveParams(params) {
|
|
524
|
+
function resolveParams(queryId, params) {
|
|
462
525
|
return safeExec(
|
|
463
|
-
() =>
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
526
|
+
() => {
|
|
527
|
+
const resolvedParams = params();
|
|
528
|
+
const cacheKey = makeQueryCacheKey(queryId, resolvedParams);
|
|
529
|
+
return {
|
|
530
|
+
status: "ready",
|
|
531
|
+
resolvedParams,
|
|
532
|
+
cacheKey
|
|
533
|
+
};
|
|
534
|
+
},
|
|
467
535
|
(promise) => ({
|
|
468
536
|
status: "blocked",
|
|
469
537
|
promise
|
|
@@ -538,19 +606,6 @@ var SyncPromise = class {
|
|
|
538
606
|
);
|
|
539
607
|
}
|
|
540
608
|
};
|
|
541
|
-
function shallowEqualRecords(a, b) {
|
|
542
|
-
if (Object.is(a, b)) {
|
|
543
|
-
return true;
|
|
544
|
-
}
|
|
545
|
-
const aKeys = Object.keys(a);
|
|
546
|
-
const bKeys = Object.keys(b);
|
|
547
|
-
if (aKeys.length !== bKeys.length) {
|
|
548
|
-
return false;
|
|
549
|
-
}
|
|
550
|
-
return aKeys.every(
|
|
551
|
-
(key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key])
|
|
552
|
-
);
|
|
553
|
-
}
|
|
554
609
|
var SettablePromise = class {
|
|
555
610
|
constructor() {
|
|
556
611
|
__publicField(this, "promise");
|
|
@@ -568,181 +623,154 @@ var SettablePromise = class {
|
|
|
568
623
|
this._reject(error);
|
|
569
624
|
}
|
|
570
625
|
};
|
|
571
|
-
function
|
|
572
|
-
const ref = import_react2.default.useRef(
|
|
573
|
-
const
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
626
|
+
function usePrevious(value) {
|
|
627
|
+
const ref = import_react2.default.useRef(void 0);
|
|
628
|
+
const prev = ref.current;
|
|
629
|
+
ref.current = value;
|
|
630
|
+
return prev;
|
|
631
|
+
}
|
|
632
|
+
function createInitial$State($ctx, $props, $q, stateSpecs) {
|
|
633
|
+
const root = {};
|
|
634
|
+
for (const stateSpec of stateSpecs) {
|
|
635
|
+
if (stateSpec.path.includes("[]")) {
|
|
636
|
+
continue;
|
|
577
637
|
}
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
ref.current.cleanup();
|
|
638
|
+
const parts = stateSpec.path.split(".");
|
|
639
|
+
const parentPath = parts.slice(0, parts.length - 1);
|
|
640
|
+
const leaf = parts[parts.length - 1];
|
|
641
|
+
let parent = root;
|
|
642
|
+
for (const part of parentPath) {
|
|
643
|
+
if (!(part in parent)) {
|
|
644
|
+
parent[part] = {};
|
|
586
645
|
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
switch (typeof value) {
|
|
601
|
-
case "undefined":
|
|
602
|
-
return `${shortPlasmicPrefix}:UNDEFINED`;
|
|
603
|
-
case "function":
|
|
604
|
-
return `${shortPlasmicPrefix}:FUNCTION:${value.name}`;
|
|
605
|
-
case "symbol":
|
|
606
|
-
return `${shortPlasmicPrefix}:SYMBOL:${value.description}`;
|
|
607
|
-
case "bigint":
|
|
608
|
-
return value.toString();
|
|
609
|
-
case "object":
|
|
610
|
-
if (value !== null) {
|
|
611
|
-
const cyclePath = visitedPaths.get(value);
|
|
612
|
-
if (cyclePath) {
|
|
613
|
-
return `${shortPlasmicPrefix}:REF:${cyclePath}`;
|
|
646
|
+
parent = parent[part];
|
|
647
|
+
}
|
|
648
|
+
if (stateSpec.valueProp) {
|
|
649
|
+
parent[leaf] = $props[stateSpec.valueProp];
|
|
650
|
+
} else if ("initVal" in stateSpec) {
|
|
651
|
+
parent[leaf] = stateSpec.initVal;
|
|
652
|
+
} else if (stateSpec.initFunc) {
|
|
653
|
+
const initFunc = stateSpec.initFunc;
|
|
654
|
+
let cached;
|
|
655
|
+
Object.defineProperty(parent, leaf, {
|
|
656
|
+
get: () => {
|
|
657
|
+
if (cached) {
|
|
658
|
+
return cached.value;
|
|
614
659
|
}
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
660
|
+
const value = initFunc({
|
|
661
|
+
$ctx,
|
|
662
|
+
$props,
|
|
663
|
+
$q,
|
|
664
|
+
$state: root,
|
|
665
|
+
$refs: {},
|
|
666
|
+
$queries: {}
|
|
667
|
+
});
|
|
668
|
+
cached = { value };
|
|
669
|
+
return value;
|
|
670
|
+
},
|
|
671
|
+
enumerable: true,
|
|
672
|
+
configurable: true
|
|
673
|
+
});
|
|
622
674
|
}
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
function sortObjectsDeep(value, visitedObjects) {
|
|
626
|
-
if (typeof value !== "object" || value === null) {
|
|
627
|
-
return value;
|
|
628
|
-
}
|
|
629
|
-
const visitedValue = visitedObjects.get(value);
|
|
630
|
-
if (visitedValue) {
|
|
631
|
-
return visitedValue;
|
|
632
|
-
}
|
|
633
|
-
if (Array.isArray(value)) {
|
|
634
|
-
const newArr = [];
|
|
635
|
-
visitedObjects.set(value, newArr);
|
|
636
|
-
value.forEach((item, key) => {
|
|
637
|
-
newArr[key] = sortObjectsDeep(item, visitedObjects);
|
|
638
|
-
});
|
|
639
|
-
return newArr;
|
|
640
|
-
} else {
|
|
641
|
-
const newObj = {};
|
|
642
|
-
visitedObjects.set(value, newObj);
|
|
643
|
-
Object.keys(value).sort().forEach((key) => {
|
|
644
|
-
newObj[key] = sortObjectsDeep(value[key], visitedObjects);
|
|
645
|
-
});
|
|
646
|
-
return newObj;
|
|
647
675
|
}
|
|
676
|
+
return root;
|
|
648
677
|
}
|
|
649
678
|
|
|
650
679
|
// src/serverQueries/client.ts
|
|
651
680
|
var GLOBAL_CACHE = /* @__PURE__ */ new Map();
|
|
652
|
-
function usePlasmicQueries(tree, $
|
|
653
|
-
|
|
654
|
-
const
|
|
655
|
-
const $stateRef = React3.useRef($state != null ? $state : {});
|
|
656
|
-
$stateRef.current = $state != null ? $state : {};
|
|
681
|
+
function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
682
|
+
var _a;
|
|
683
|
+
const wrappedQueries = React3.useMemo(() => wrapQueries(tree.queries), [tree]);
|
|
657
684
|
const $queries = React3.useMemo(
|
|
658
685
|
() => createDollarQueries(Object.keys(tree.queries)),
|
|
659
686
|
[tree]
|
|
660
687
|
);
|
|
661
|
-
const queries = React3.useMemo(() => {
|
|
662
|
-
return mapRecords(
|
|
663
|
-
(_name, q) => ({
|
|
664
|
-
id: q.id,
|
|
665
|
-
fn: q.fn,
|
|
666
|
-
execParams: () => q.args({
|
|
667
|
-
$q: $queries,
|
|
668
|
-
$props: stableProps,
|
|
669
|
-
$ctx: stableCtx,
|
|
670
|
-
$state: $stateRef.current,
|
|
671
|
-
$scopedItemVars: {}
|
|
672
|
-
})
|
|
673
|
-
}),
|
|
674
|
-
tree.queries
|
|
675
|
-
);
|
|
676
|
-
}, [$queries, stableCtx, stableProps, tree]);
|
|
677
|
-
const wrappedQueries = React3.useMemo(() => wrapQueries(queries), [queries]);
|
|
678
688
|
const $queryStates = $queries;
|
|
689
|
+
if (!$state) {
|
|
690
|
+
$state = createInitial$State($ctx, $props, $queryStates, tree.stateSpecs);
|
|
691
|
+
}
|
|
679
692
|
const { fallback: prefetchedCache, cache: swrCache } = (0, import_query2.usePlasmicDataConfig)();
|
|
680
|
-
const
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
693
|
+
const paramsResults = {};
|
|
694
|
+
const executionCtx = {
|
|
695
|
+
$ctx,
|
|
696
|
+
$props,
|
|
697
|
+
$q: $queryStates,
|
|
698
|
+
$state
|
|
699
|
+
};
|
|
700
|
+
const prevWrappedQueries = usePrevious(wrappedQueries);
|
|
701
|
+
let consistent = prevWrappedQueries === void 0 || Object.is(prevWrappedQueries, wrappedQueries);
|
|
702
|
+
mapRecords(
|
|
703
|
+
(queryName, $query, query) => {
|
|
704
|
+
if (!consistent || $query.current.state === "initial") {
|
|
685
705
|
return;
|
|
686
706
|
}
|
|
687
|
-
|
|
688
|
-
|
|
707
|
+
const paramsResult = resolveParams(
|
|
708
|
+
query.id,
|
|
709
|
+
() => query.args(executionCtx)
|
|
710
|
+
);
|
|
711
|
+
paramsResults[queryName] = paramsResult;
|
|
712
|
+
if (paramsResult.status === "blocked") {
|
|
713
|
+
consistent = false;
|
|
714
|
+
} else if (paramsResult.status === "error" && $query.current.key !== null) {
|
|
715
|
+
consistent = false;
|
|
716
|
+
} else if (paramsResult.status === "ready" && paramsResult.cacheKey !== $query.current.key) {
|
|
717
|
+
consistent = false;
|
|
689
718
|
}
|
|
690
|
-
}
|
|
719
|
+
},
|
|
720
|
+
$queryStates,
|
|
721
|
+
wrappedQueries
|
|
722
|
+
);
|
|
723
|
+
if (!consistent) {
|
|
691
724
|
mapRecords((_queryName, $query) => {
|
|
692
|
-
$query.
|
|
725
|
+
$query.reset();
|
|
693
726
|
}, $queryStates);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
727
|
+
for (const k of Object.keys(paramsResults)) {
|
|
728
|
+
delete paramsResults[k];
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const stopRef = React3.useRef();
|
|
732
|
+
(_a = stopRef.current) == null ? void 0 : _a.call(stopRef);
|
|
733
|
+
let stopped = false;
|
|
734
|
+
const stop = new Promise((resolve) => {
|
|
735
|
+
stopRef.current = () => {
|
|
736
|
+
stopped = true;
|
|
737
|
+
resolve();
|
|
699
738
|
};
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
739
|
+
});
|
|
740
|
+
React3.useEffect(() => () => {
|
|
741
|
+
var _a2;
|
|
742
|
+
return (_a2 = stopRef.current) == null ? void 0 : _a2.call(stopRef);
|
|
743
|
+
}, []);
|
|
744
|
+
const loop = () => __async(null, null, function* () {
|
|
745
|
+
while (true) {
|
|
746
|
+
initPlasmicQueriesSync(
|
|
747
|
+
$queryStates,
|
|
748
|
+
wrappedQueries,
|
|
749
|
+
paramsResults,
|
|
750
|
+
executionCtx,
|
|
751
|
+
prefetchedCache,
|
|
752
|
+
swrCache
|
|
753
|
+
);
|
|
754
|
+
const loadingQueries = mapRecordEntries((_queryName, $query) => {
|
|
755
|
+
if ($query.isLoading) {
|
|
756
|
+
return $query.getDoneResult();
|
|
757
|
+
} else {
|
|
758
|
+
return null;
|
|
709
759
|
}
|
|
760
|
+
}, $queryStates).filter(notNil);
|
|
761
|
+
if (loadingQueries.length === 0) {
|
|
762
|
+
break;
|
|
710
763
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
swrCache
|
|
719
|
-
);
|
|
720
|
-
const loadingQueries = mapRecordEntries((_queryName, $query) => {
|
|
721
|
-
if ($query.isLoading) {
|
|
722
|
-
return $query.getDoneResult();
|
|
723
|
-
} else {
|
|
724
|
-
return null;
|
|
725
|
-
}
|
|
726
|
-
}, $queryStates).filter(notNil);
|
|
727
|
-
if (loadingQueries.length === 0) {
|
|
728
|
-
break;
|
|
729
|
-
}
|
|
730
|
-
yield Promise.race(loadingQueries);
|
|
731
|
-
if (cleanup) {
|
|
732
|
-
break;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
});
|
|
736
|
-
loop().catch(noopFn);
|
|
737
|
-
return () => {
|
|
738
|
-
cleanup = true;
|
|
739
|
-
};
|
|
740
|
-
},
|
|
741
|
-
[wrappedQueries, $queryStates, settledCount]
|
|
742
|
-
);
|
|
764
|
+
yield Promise.race([stop, ...loadingQueries]);
|
|
765
|
+
if (stopped) {
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
loop().catch(noopFn);
|
|
743
771
|
mapRecords(
|
|
744
|
-
(
|
|
745
|
-
usePlasmicQuery($query, query,
|
|
772
|
+
(queryName, $query, query) => {
|
|
773
|
+
usePlasmicQuery($query, query, paramsResults[queryName]);
|
|
746
774
|
},
|
|
747
775
|
$queryStates,
|
|
748
776
|
wrappedQueries
|
|
@@ -764,20 +792,24 @@ function wrapQueries(queries) {
|
|
|
764
792
|
return {
|
|
765
793
|
id: query.id,
|
|
766
794
|
fn: wrappedFn,
|
|
767
|
-
|
|
795
|
+
args: query.args
|
|
768
796
|
};
|
|
769
797
|
}, queries);
|
|
770
798
|
}
|
|
771
|
-
function initPlasmicQueriesSync($queries, queries, prefetchedCache, clientCache) {
|
|
799
|
+
function initPlasmicQueriesSync($queries, queries, paramsResults, executionCtx, prefetchedCache, clientCache) {
|
|
772
800
|
let anySettled;
|
|
773
801
|
do {
|
|
774
802
|
anySettled = false;
|
|
775
803
|
mapRecords(
|
|
776
|
-
(
|
|
804
|
+
(queryName, $query, query) => {
|
|
777
805
|
if ($query.current.state !== "initial") {
|
|
778
806
|
return;
|
|
779
807
|
}
|
|
780
|
-
const paramsResult = resolveParams(
|
|
808
|
+
const paramsResult = resolveParams(
|
|
809
|
+
query.id,
|
|
810
|
+
() => query.args(executionCtx)
|
|
811
|
+
);
|
|
812
|
+
paramsResults[queryName] = paramsResult;
|
|
781
813
|
if (paramsResult.status === "error") {
|
|
782
814
|
$query.rejectPromise(null, paramsResult.error);
|
|
783
815
|
anySettled = true;
|
|
@@ -820,11 +852,8 @@ function initPlasmicQueriesSync($queries, queries, prefetchedCache, clientCache)
|
|
|
820
852
|
);
|
|
821
853
|
} while (anySettled);
|
|
822
854
|
}
|
|
823
|
-
function usePlasmicQuery($query, query,
|
|
855
|
+
function usePlasmicQuery($query, query, paramsResult) {
|
|
824
856
|
const $queryState = $query;
|
|
825
|
-
const paramsResult = React3.useMemo(() => {
|
|
826
|
-
return resolveParams(query.execParams);
|
|
827
|
-
}, [query.execParams, settledCount]);
|
|
828
857
|
const { key, fetcher } = React3.useMemo(() => {
|
|
829
858
|
switch (paramsResult.status) {
|
|
830
859
|
case "blocked":
|
|
@@ -844,11 +873,29 @@ function usePlasmicQuery($query, query, settledCount) {
|
|
|
844
873
|
return {
|
|
845
874
|
key: cacheKey,
|
|
846
875
|
fetcher: () => {
|
|
847
|
-
const
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
876
|
+
const clientCachedPromise = GLOBAL_CACHE.get(cacheKey);
|
|
877
|
+
if (clientCachedPromise == null ? void 0 : clientCachedPromise.result) {
|
|
878
|
+
if (clientCachedPromise.result.state === "resolved") {
|
|
879
|
+
$queryState.resolvePromise(
|
|
880
|
+
cacheKey,
|
|
881
|
+
clientCachedPromise.result.value
|
|
882
|
+
);
|
|
883
|
+
} else {
|
|
884
|
+
$queryState.rejectPromise(
|
|
885
|
+
cacheKey,
|
|
886
|
+
clientCachedPromise.result.error
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
return clientCachedPromise.promise.finally(() => {
|
|
890
|
+
GLOBAL_CACHE.delete(cacheKey);
|
|
891
|
+
});
|
|
892
|
+
} else {
|
|
893
|
+
const promise = query.fn(...paramsResult.resolvedParams);
|
|
894
|
+
$queryState.loadingPromise(cacheKey, promise);
|
|
895
|
+
return promise.finally(() => {
|
|
896
|
+
GLOBAL_CACHE.delete(cacheKey);
|
|
897
|
+
});
|
|
898
|
+
}
|
|
852
899
|
}
|
|
853
900
|
};
|
|
854
901
|
}
|
|
@@ -872,13 +919,6 @@ function usePlasmicQuery($query, query, settledCount) {
|
|
|
872
919
|
}
|
|
873
920
|
return result;
|
|
874
921
|
}
|
|
875
|
-
function useShallowStableRecord(value) {
|
|
876
|
-
const ref = React3.useRef(value);
|
|
877
|
-
if (!shallowEqualRecords(ref.current, value)) {
|
|
878
|
-
ref.current = value;
|
|
879
|
-
}
|
|
880
|
-
return ref.current;
|
|
881
|
-
}
|
|
882
922
|
|
|
883
923
|
// src/serverQueries/server.ts
|
|
884
924
|
var ROOT_COMPONENT_KEY_PATH = "root";
|
|
@@ -890,6 +930,8 @@ function executeQueryTree(rootNode, options, queriesByComponent) {
|
|
|
890
930
|
const initialContext = {
|
|
891
931
|
$props,
|
|
892
932
|
$ctx,
|
|
933
|
+
// Placeholder; executeComponentNode replaces this with an initial
|
|
934
|
+
// $state derived from the component's own stateSpecs.
|
|
893
935
|
$state: {},
|
|
894
936
|
$q: {},
|
|
895
937
|
$scopedItemVars: {}
|
|
@@ -947,10 +989,16 @@ function executeComponentNode(node, params) {
|
|
|
947
989
|
componentQueries = {};
|
|
948
990
|
queriesByComponent.set(componentKeyPath, componentQueries);
|
|
949
991
|
}
|
|
992
|
+
const $state = node.stateSpecs.length > 0 ? createInitial$State(
|
|
993
|
+
parentContext.$ctx,
|
|
994
|
+
evaluatedProps,
|
|
995
|
+
componentQueries,
|
|
996
|
+
node.stateSpecs
|
|
997
|
+
) : {};
|
|
950
998
|
const componentContext = {
|
|
951
999
|
$props: evaluatedProps,
|
|
952
1000
|
$ctx: parentContext.$ctx,
|
|
953
|
-
$state
|
|
1001
|
+
$state,
|
|
954
1002
|
$q: componentQueries,
|
|
955
1003
|
$scopedItemVars: parentContext.$scopedItemVars
|
|
956
1004
|
};
|
|
@@ -961,14 +1009,7 @@ function executeComponentNode(node, params) {
|
|
|
961
1009
|
}
|
|
962
1010
|
const $query = new StatefulQueryResult();
|
|
963
1011
|
componentQueries[queryName] = $query;
|
|
964
|
-
|
|
965
|
-
const capturedArgsFn = query.args;
|
|
966
|
-
const plasmicQuery = {
|
|
967
|
-
id: query.id,
|
|
968
|
-
fn: query.fn,
|
|
969
|
-
execParams: () => capturedArgsFn(capturedContext)
|
|
970
|
-
};
|
|
971
|
-
discovered.push({ $query, query: plasmicQuery });
|
|
1012
|
+
discovered.push({ $query, query, ctx: componentContext });
|
|
972
1013
|
}
|
|
973
1014
|
node.children.forEach((child, idx) => {
|
|
974
1015
|
discovered.push(
|
|
@@ -1095,7 +1136,7 @@ function executePlasmicQueries(rootNode, options) {
|
|
|
1095
1136
|
discoveredQueries.push(...newQueries);
|
|
1096
1137
|
yield Promise.all(
|
|
1097
1138
|
newQueries.map(
|
|
1098
|
-
(d) => executePlasmicQuery(d.$query, d.query).catch(() => {
|
|
1139
|
+
(d) => executePlasmicQuery(d.$query, d.query, d.ctx).catch(() => {
|
|
1099
1140
|
})
|
|
1100
1141
|
)
|
|
1101
1142
|
);
|
|
@@ -1127,13 +1168,13 @@ function executePlasmicQueries(rootNode, options) {
|
|
|
1127
1168
|
return { cache, queries };
|
|
1128
1169
|
});
|
|
1129
1170
|
}
|
|
1130
|
-
function executePlasmicQuery($query, query) {
|
|
1171
|
+
function executePlasmicQuery($query, query, ctx) {
|
|
1131
1172
|
return __async(this, null, function* () {
|
|
1132
1173
|
if ($query.current.state === "loading" || $query.current.state === "done") {
|
|
1133
1174
|
return $query.getDoneResult();
|
|
1134
1175
|
}
|
|
1135
1176
|
do {
|
|
1136
|
-
const paramsResult = resolveParams(query.
|
|
1177
|
+
const paramsResult = resolveParams(query.id, () => query.args(ctx));
|
|
1137
1178
|
switch (paramsResult.status) {
|
|
1138
1179
|
case "blocked": {
|
|
1139
1180
|
try {
|
|
@@ -1143,13 +1184,9 @@ function executePlasmicQuery($query, query) {
|
|
|
1143
1184
|
continue;
|
|
1144
1185
|
}
|
|
1145
1186
|
case "ready": {
|
|
1146
|
-
const cacheKey = makeQueryCacheKey(
|
|
1147
|
-
query.id,
|
|
1148
|
-
paramsResult.resolvedParams
|
|
1149
|
-
);
|
|
1150
1187
|
$query.loadingPromise(
|
|
1151
|
-
cacheKey,
|
|
1152
|
-
query.fn(...paramsResult.resolvedParams)
|
|
1188
|
+
paramsResult.cacheKey,
|
|
1189
|
+
Promise.resolve(query.fn(...paramsResult.resolvedParams))
|
|
1153
1190
|
);
|
|
1154
1191
|
return $query.getDoneResult();
|
|
1155
1192
|
}
|
|
@@ -1161,6 +1198,11 @@ function executePlasmicQuery($query, query) {
|
|
|
1161
1198
|
} while (true);
|
|
1162
1199
|
});
|
|
1163
1200
|
}
|
|
1201
|
+
function executeServerQuery(_query) {
|
|
1202
|
+
return __async(this, null, function* () {
|
|
1203
|
+
return { data: void 0, isLoading: false };
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1164
1206
|
|
|
1165
1207
|
// src/index.tsx
|
|
1166
1208
|
var import_query5 = require("@plasmicapp/query");
|