@plasmicapp/data-sources 1.0.0 → 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.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 +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { CodeComponentMeta } from '@plasmicapp/host/registerComponent';
|
|
2
2
|
import { PlasmicDataSourceContextValue } from '@plasmicapp/data-sources-context';
|
|
3
3
|
import { default as React_2 } from 'react';
|
|
4
|
-
import { SWRResponse } from '@plasmicapp/query';
|
|
5
4
|
import { usePlasmicDataConfig } from '@plasmicapp/query';
|
|
6
5
|
|
|
7
6
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
@@ -44,6 +43,12 @@ export declare interface ClientQueryResult<T = any> {
|
|
|
44
43
|
isLoading?: boolean;
|
|
45
44
|
}
|
|
46
45
|
|
|
46
|
+
/**
|
|
47
|
+
* A function that takes the execution context and returns a value.
|
|
48
|
+
* Used for all dynamic expressions in the query tree (args, collectionExpr, visibilityExpr, data, propsContext values).
|
|
49
|
+
*/
|
|
50
|
+
declare type ContextFn<R> = (ctx: QueryExecutionContext) => R;
|
|
51
|
+
|
|
47
52
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
48
53
|
export declare interface DataOp {
|
|
49
54
|
sourceId: string;
|
|
@@ -81,6 +86,13 @@ declare interface ExecuteOpts {
|
|
|
81
86
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
82
87
|
export declare function executePlasmicDataOp<T extends SingleRowResult | ManyRowsResult>(op: DataOp, opts?: ExecuteOpts): Promise<T>;
|
|
83
88
|
|
|
89
|
+
declare interface ExecutePlasmicQueriesResult {
|
|
90
|
+
/** All queries, including nested, by query cache key hash. Passed to PlasmicRootProvider */
|
|
91
|
+
cache: Record<string, unknown>;
|
|
92
|
+
/** Root component query results keyed by query name. */
|
|
93
|
+
queries: Record<string, PlasmicQueryResult>;
|
|
94
|
+
}
|
|
95
|
+
|
|
84
96
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
85
97
|
export declare function Fetcher(props: FetcherProps): React_2.ReactElement | null;
|
|
86
98
|
|
|
@@ -153,18 +165,76 @@ declare interface PlasmicUndefinedDataErrorPromise extends Promise<any> {
|
|
|
153
165
|
message: string;
|
|
154
166
|
}
|
|
155
167
|
|
|
168
|
+
/** @internal */
|
|
169
|
+
declare interface QueryCodeComponentNode {
|
|
170
|
+
type: "codeComponent";
|
|
171
|
+
propsContext: Record<string, ContextFn<unknown>>;
|
|
172
|
+
serverRenderingConfig?: SerializedServerRenderingConfig;
|
|
173
|
+
children: QueryNode[];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** @internal */
|
|
177
|
+
export declare interface QueryComponentNode {
|
|
178
|
+
type: "component";
|
|
179
|
+
queries: Record<string, SerializedServerQuery>;
|
|
180
|
+
propsContext: Record<string, ContextFn<unknown>>;
|
|
181
|
+
children: QueryNode[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** @internal */
|
|
185
|
+
declare interface QueryDataProviderNode {
|
|
186
|
+
type: "dataProvider";
|
|
187
|
+
name: string;
|
|
188
|
+
data: ContextFn<unknown>;
|
|
189
|
+
children: QueryNode[];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
declare interface QueryExecutionContext {
|
|
193
|
+
$props: Record<string, unknown>;
|
|
194
|
+
$ctx: Record<string, unknown>;
|
|
195
|
+
$state: Record<string, unknown>;
|
|
196
|
+
$q: Record<string, PlasmicQueryResult>;
|
|
197
|
+
$scopedItemVars: Record<string, unknown>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** @internal */
|
|
201
|
+
declare type QueryExecutionInitialContext = Pick<QueryExecutionContext, "$props" | "$ctx">;
|
|
202
|
+
|
|
203
|
+
/** @internal */
|
|
204
|
+
declare type QueryNode = QueryComponentNode | QueryCodeComponentNode | QueryRepeatedNode | QueryDataProviderNode | QueryVisibilityNode;
|
|
205
|
+
|
|
206
|
+
/** @internal */
|
|
207
|
+
declare interface QueryRepeatedNode {
|
|
208
|
+
type: "repeated";
|
|
209
|
+
collectionExpr: ContextFn<unknown>;
|
|
210
|
+
itemName: string;
|
|
211
|
+
indexName: string;
|
|
212
|
+
children: QueryNode[];
|
|
213
|
+
}
|
|
214
|
+
|
|
156
215
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
157
216
|
export declare type QueryResult = Partial<ManyRowsResult<any>> & {
|
|
158
217
|
error?: any;
|
|
159
218
|
isLoading?: boolean;
|
|
160
219
|
};
|
|
161
220
|
|
|
221
|
+
/** @internal */
|
|
222
|
+
declare interface QueryVisibilityNode {
|
|
223
|
+
type: "visibility";
|
|
224
|
+
visibilityExpr: ContextFn<unknown>;
|
|
225
|
+
children: QueryNode[];
|
|
226
|
+
}
|
|
227
|
+
|
|
162
228
|
declare type ResolvableDataOp = DataOp | undefined | null | (() => DataOp | undefined | null);
|
|
163
229
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
230
|
+
declare interface SerializedServerQuery {
|
|
231
|
+
id: string;
|
|
232
|
+
fn: (...args: unknown[]) => Promise<unknown>;
|
|
233
|
+
args: ContextFn<unknown[]>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
declare type SerializedServerRenderingConfig = boolean;
|
|
237
|
+
|
|
168
238
|
declare class SettablePromise<T> {
|
|
169
239
|
readonly promise: Promise<T>;
|
|
170
240
|
private _resolve;
|
|
@@ -256,106 +326,41 @@ export declare interface TableSchema {
|
|
|
256
326
|
}
|
|
257
327
|
|
|
258
328
|
/**
|
|
259
|
-
*
|
|
260
|
-
* Creates a $queries variable that can be used before queries are completed.
|
|
261
|
-
*
|
|
262
|
-
* Each query gets a StatefulQueryResult which implements PlasmicQueryResult.
|
|
263
|
-
* If data is attempted to be accessed before query completion,
|
|
264
|
-
* PlasmicUndefinedDataErrorPromise is thrown. Dependent queries can catch this
|
|
265
|
-
* and delay its own execution until the dependent data is ready.
|
|
329
|
+
* Executes all queries from a serialized component tree and returns query results.
|
|
266
330
|
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
331
|
+
* 1. Walk the tree to discover queries, creating StatefulQueryResult + PlasmicQuery
|
|
332
|
+
* 2. Execute all newly discovered queries in parallel
|
|
333
|
+
* 3. After all settle, re-walk. Discover queries from newly expanded repeated nodes
|
|
334
|
+
* Continue until no new queries found.
|
|
269
335
|
*/
|
|
270
|
-
export declare function
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* @internal
|
|
274
|
-
* Executes all queries and returns the query data keyed by cache key.
|
|
275
|
-
*
|
|
276
|
-
* Example codegen:
|
|
277
|
-
*
|
|
278
|
-
* export function create$Queries() {
|
|
279
|
-
* return createDollarQueries(["result", "dep"]);
|
|
280
|
-
* }
|
|
281
|
-
* type QueryName = keyof ReturnType<typeof create$Queries>;
|
|
282
|
-
* export function createQueries(
|
|
283
|
-
* $ctx: Record<string, any>,
|
|
284
|
-
* $queries: Record<QueryName, PlasmicQueryResult>,
|
|
285
|
-
* ) {
|
|
286
|
-
* return {
|
|
287
|
-
* result: {
|
|
288
|
-
* id: "plus",
|
|
289
|
-
* fn: (a, b) => a + b,
|
|
290
|
-
* execParams: () => [1, $queries.dep.data],
|
|
291
|
-
* },
|
|
292
|
-
* dep: {
|
|
293
|
-
* id: "times",
|
|
294
|
-
* fn: (a, b) => a * b,
|
|
295
|
-
* execParams: () => [2, 3],
|
|
296
|
-
* },
|
|
297
|
-
* }
|
|
298
|
-
* }
|
|
299
|
-
*
|
|
300
|
-
* export async function ServerComponent() {
|
|
301
|
-
* const $ctx = useDataEnv();
|
|
302
|
-
* const $queries = createDollarQueries();
|
|
303
|
-
* const queries = createQueries($ctx, $queries);
|
|
304
|
-
* const prefetchedCache = await executePlasmicQueries($queries, queries);
|
|
305
|
-
* return (
|
|
306
|
-
* <PlasmicQueryDataProvider prefetchedCache={prefetchedCache}>
|
|
307
|
-
* <ClientComponent />
|
|
308
|
-
* </PlasmicQueryDataProvider>
|
|
309
|
-
* );
|
|
310
|
-
* }
|
|
311
|
-
*/
|
|
312
|
-
export declare function unstable_executePlasmicQueries<QueryName extends string>($queries: Record<QueryName, PlasmicQueryResult>, queries: Record<QueryName, PlasmicQuery>): Promise<{
|
|
313
|
-
[cacheKey: string]: unknown;
|
|
314
|
-
}>;
|
|
336
|
+
export declare function unstable_executePlasmicQueries(rootNode: QueryComponentNode, options: QueryExecutionInitialContext): Promise<ExecutePlasmicQueriesResult>;
|
|
315
337
|
|
|
316
338
|
/**
|
|
317
339
|
* @internal
|
|
318
340
|
* This hook's job is to execute queries and re-render when query state changes.
|
|
319
341
|
* Data caching can be controlled via @plasmicapp/query.
|
|
320
342
|
*
|
|
321
|
-
* The actual results will be available in $queries
|
|
322
|
-
* createDollarQueries.
|
|
343
|
+
* The actual results will be available in the returned $queries object.
|
|
323
344
|
*
|
|
324
345
|
* Prefetched query data can be passed to
|
|
325
346
|
* <PlasmicQueryDataProvider prefetchedCache={...}>.
|
|
326
347
|
*
|
|
327
348
|
* Example codegen:
|
|
328
349
|
*
|
|
329
|
-
* export
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* ) {
|
|
337
|
-
* return {
|
|
338
|
-
* result: {
|
|
339
|
-
* id: "plus",
|
|
340
|
-
* fn: (a, b) => a + b,
|
|
341
|
-
* execParams: () => [1, $queries.dep.data],
|
|
342
|
-
* },
|
|
343
|
-
* dep: {
|
|
344
|
-
* id: "times",
|
|
345
|
-
* fn: (a, b) => a * b,
|
|
346
|
-
* execParams: () => [2, 3],
|
|
347
|
-
* },
|
|
348
|
-
* }
|
|
349
|
-
* }
|
|
350
|
+
* export const serverQueryTree = {
|
|
351
|
+
* type: "component",
|
|
352
|
+
* queries: {
|
|
353
|
+
* films: { fn: $$.fetch, id: "fetch", args: ({ $q, $props, $ctx }) => [...] }
|
|
354
|
+
* },
|
|
355
|
+
* propsContext: {},
|
|
356
|
+
* };
|
|
350
357
|
*
|
|
351
|
-
* export function ClientComponent() {
|
|
352
|
-
* const $
|
|
353
|
-
*
|
|
354
|
-
* usePlasmicQueries($queries, queries);
|
|
355
|
-
* return <div>{$queries.result.data}</div> // $queries.result.data may suspend
|
|
358
|
+
* export function ClientComponent($props, $ctx) {
|
|
359
|
+
* const $q = usePlasmicQueries(serverQueryTree, $props, $ctx);
|
|
360
|
+
* return <div>{$q.films.data}</div>
|
|
356
361
|
* }
|
|
357
362
|
*/
|
|
358
|
-
export declare function unstable_usePlasmicQueries
|
|
363
|
+
export declare function unstable_usePlasmicQueries(tree: QueryComponentNode, $props: Record<string, unknown>, $ctx: Record<string, unknown>, $state?: Record<string, unknown>): Record<string, PlasmicQueryResult>;
|
|
359
364
|
|
|
360
365
|
/**
|
|
361
366
|
* @internal
|
|
@@ -381,13 +386,4 @@ export declare function usePlasmicDataOp<T extends SingleRowResult | ManyRowsRes
|
|
|
381
386
|
/** @deprecated See https://docs.plasmic.app/learn/integrations */
|
|
382
387
|
export declare function usePlasmicInvalidate(): (invalidatedKeys: string[] | null | undefined) => Promise<any[] | undefined>;
|
|
383
388
|
|
|
384
|
-
/**
|
|
385
|
-
* @deprecated
|
|
386
|
-
* This export will be deleted before RSC release.
|
|
387
|
-
* TODO: Rename to usePlasmicQuery
|
|
388
|
-
* TODO: Reference $query directly.
|
|
389
|
-
* TODO: Use paramsResult from usePlasmicQueries
|
|
390
|
-
*/
|
|
391
|
-
declare function usePlasmicQuery<T, F extends (...args: any[]) => Promise<T>>($query: PlasmicQueryResult<T>, query: PlasmicQuery<F>, settledCount?: number): SWRResponse<T, unknown>;
|
|
392
|
-
|
|
393
389
|
export { }
|
package/dist/index.esm.js
CHANGED
|
@@ -387,16 +387,29 @@ var StatefulQueryResult = class {
|
|
|
387
387
|
}
|
|
388
388
|
};
|
|
389
389
|
function safeExec(tryData, ifUndefined, ifError) {
|
|
390
|
+
const result = safeExecResult(tryData);
|
|
391
|
+
if ("promise" in result) {
|
|
392
|
+
return ifUndefined(result.promise);
|
|
393
|
+
} else if ("error" in result) {
|
|
394
|
+
return ifError(result.error);
|
|
395
|
+
} else {
|
|
396
|
+
return result.data;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function safeExecResult(tryData) {
|
|
390
400
|
try {
|
|
391
|
-
return tryData();
|
|
401
|
+
return { data: tryData() };
|
|
392
402
|
} catch (err) {
|
|
393
403
|
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
394
|
-
return
|
|
404
|
+
return { promise: err };
|
|
395
405
|
} else {
|
|
396
|
-
return
|
|
406
|
+
return { error: err };
|
|
397
407
|
}
|
|
398
408
|
}
|
|
399
409
|
}
|
|
410
|
+
function assertUnexpectedNodeType(x) {
|
|
411
|
+
throw new Error(`Unexpected node type: ${x}`);
|
|
412
|
+
}
|
|
400
413
|
function resolveParams(params) {
|
|
401
414
|
return safeExec(
|
|
402
415
|
() => ({
|
|
@@ -477,6 +490,19 @@ var SyncPromise = class {
|
|
|
477
490
|
);
|
|
478
491
|
}
|
|
479
492
|
};
|
|
493
|
+
function shallowEqualRecords(a, b) {
|
|
494
|
+
if (Object.is(a, b)) {
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
const aKeys = Object.keys(a);
|
|
498
|
+
const bKeys = Object.keys(b);
|
|
499
|
+
if (aKeys.length !== bKeys.length) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
return aKeys.every(
|
|
503
|
+
(key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key])
|
|
504
|
+
);
|
|
505
|
+
}
|
|
480
506
|
var SettablePromise = class {
|
|
481
507
|
constructor() {
|
|
482
508
|
this.promise = new Promise((resolve, reject) => {
|
|
@@ -572,7 +598,31 @@ function sortObjectsDeep(value, visitedObjects) {
|
|
|
572
598
|
|
|
573
599
|
// src/serverQueries/client.ts
|
|
574
600
|
var GLOBAL_CACHE = /* @__PURE__ */ new Map();
|
|
575
|
-
function usePlasmicQueries($
|
|
601
|
+
function usePlasmicQueries(tree, $props, $ctx, $state) {
|
|
602
|
+
const stableProps = useShallowStableRecord($props);
|
|
603
|
+
const stableCtx = useShallowStableRecord($ctx);
|
|
604
|
+
const $stateRef = React3.useRef($state != null ? $state : {});
|
|
605
|
+
$stateRef.current = $state != null ? $state : {};
|
|
606
|
+
const $queries = React3.useMemo(
|
|
607
|
+
() => createDollarQueries(Object.keys(tree.queries)),
|
|
608
|
+
[tree]
|
|
609
|
+
);
|
|
610
|
+
const queries = React3.useMemo(() => {
|
|
611
|
+
return mapRecords(
|
|
612
|
+
(_name, q) => ({
|
|
613
|
+
id: q.id,
|
|
614
|
+
fn: q.fn,
|
|
615
|
+
execParams: () => q.args({
|
|
616
|
+
$q: $queries,
|
|
617
|
+
$props: stableProps,
|
|
618
|
+
$ctx: stableCtx,
|
|
619
|
+
$state: $stateRef.current,
|
|
620
|
+
$scopedItemVars: {}
|
|
621
|
+
})
|
|
622
|
+
}),
|
|
623
|
+
tree.queries
|
|
624
|
+
);
|
|
625
|
+
}, [$queries, stableCtx, stableProps, tree]);
|
|
576
626
|
const wrappedQueries = React3.useMemo(() => wrapQueries(queries), [queries]);
|
|
577
627
|
const $queryStates = $queries;
|
|
578
628
|
const { fallback: prefetchedCache, cache: swrCache } = usePlasmicDataConfig2();
|
|
@@ -639,13 +689,14 @@ function usePlasmicQueries($queries, queries) {
|
|
|
639
689
|
},
|
|
640
690
|
[wrappedQueries, $queryStates, settledCount]
|
|
641
691
|
);
|
|
642
|
-
|
|
692
|
+
mapRecords(
|
|
643
693
|
(_queryName, $query, query) => {
|
|
644
|
-
|
|
694
|
+
usePlasmicQuery($query, query, settledCount);
|
|
645
695
|
},
|
|
646
696
|
$queryStates,
|
|
647
697
|
wrappedQueries
|
|
648
698
|
);
|
|
699
|
+
return $queries;
|
|
649
700
|
}
|
|
650
701
|
function wrapQueries(queries) {
|
|
651
702
|
return mapRecords((_queryName, query) => {
|
|
@@ -770,24 +821,261 @@ function usePlasmicQuery($query, query, settledCount) {
|
|
|
770
821
|
}
|
|
771
822
|
return result;
|
|
772
823
|
}
|
|
824
|
+
function useShallowStableRecord(value) {
|
|
825
|
+
const ref = React3.useRef(value);
|
|
826
|
+
if (!shallowEqualRecords(ref.current, value)) {
|
|
827
|
+
ref.current = value;
|
|
828
|
+
}
|
|
829
|
+
return ref.current;
|
|
830
|
+
}
|
|
773
831
|
|
|
774
832
|
// src/serverQueries/server.ts
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
833
|
+
var ROOT_COMPONENT_KEY_PATH = "root";
|
|
834
|
+
function appendKeyPath(currentKeyPath, currentInput) {
|
|
835
|
+
return currentKeyPath ? `${currentKeyPath}/${currentInput}` : currentInput;
|
|
836
|
+
}
|
|
837
|
+
function executeQueryTree(rootNode, options, queriesByComponent) {
|
|
838
|
+
const { $props, $ctx } = options;
|
|
839
|
+
const initialContext = {
|
|
840
|
+
$props,
|
|
841
|
+
$ctx,
|
|
842
|
+
$state: {},
|
|
843
|
+
$q: {},
|
|
844
|
+
$scopedItemVars: {}
|
|
845
|
+
};
|
|
846
|
+
return executeComponentNode(rootNode, {
|
|
847
|
+
context: initialContext,
|
|
848
|
+
parentKeyPath: "",
|
|
849
|
+
childIndex: 0,
|
|
850
|
+
queriesByComponent
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
function executeNode(node, params) {
|
|
854
|
+
const p = { node, params, type: node.type };
|
|
855
|
+
switch (p.type) {
|
|
856
|
+
case "component":
|
|
857
|
+
return executeComponentNode(p.node, p.params);
|
|
858
|
+
case "codeComponent":
|
|
859
|
+
return executeCodeComponentNode(p.node, p.params);
|
|
860
|
+
case "dataProvider":
|
|
861
|
+
return executeDataProviderNode(p.node, p.params);
|
|
862
|
+
case "visibility":
|
|
863
|
+
return executeVisibilityNode(p.node, p.params);
|
|
864
|
+
case "repeated":
|
|
865
|
+
return executeRepeatedNode(p.node, p.params);
|
|
866
|
+
default:
|
|
867
|
+
assertUnexpectedNodeType(p);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
function executeComponentNode(node, params) {
|
|
871
|
+
const {
|
|
872
|
+
context: parentContext,
|
|
873
|
+
parentKeyPath,
|
|
874
|
+
childIndex,
|
|
875
|
+
queriesByComponent
|
|
876
|
+
} = params;
|
|
877
|
+
const isRoot = parentKeyPath === "";
|
|
878
|
+
let componentKeyPath;
|
|
879
|
+
let evaluatedProps;
|
|
880
|
+
if (isRoot) {
|
|
881
|
+
componentKeyPath = ROOT_COMPONENT_KEY_PATH;
|
|
882
|
+
evaluatedProps = parentContext.$props;
|
|
883
|
+
} else {
|
|
884
|
+
componentKeyPath = appendKeyPath(parentKeyPath, `c${childIndex}`);
|
|
885
|
+
evaluatedProps = {};
|
|
886
|
+
for (const [propName, propFn] of Object.entries(node.propsContext)) {
|
|
887
|
+
const result = safeExecResult(() => propFn(parentContext));
|
|
888
|
+
if (!("data" in result)) {
|
|
889
|
+
return [];
|
|
890
|
+
}
|
|
891
|
+
evaluatedProps[propName] = result.data;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
let componentQueries = queriesByComponent.get(componentKeyPath);
|
|
895
|
+
if (!componentQueries) {
|
|
896
|
+
componentQueries = {};
|
|
897
|
+
queriesByComponent.set(componentKeyPath, componentQueries);
|
|
898
|
+
}
|
|
899
|
+
const componentContext = {
|
|
900
|
+
$props: evaluatedProps,
|
|
901
|
+
$ctx: parentContext.$ctx,
|
|
902
|
+
$state: parentContext.$state,
|
|
903
|
+
$q: componentQueries,
|
|
904
|
+
$scopedItemVars: parentContext.$scopedItemVars
|
|
905
|
+
};
|
|
906
|
+
const discovered = [];
|
|
907
|
+
for (const [queryName, query] of Object.entries(node.queries)) {
|
|
908
|
+
if (componentQueries[queryName]) {
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
const $query = new StatefulQueryResult();
|
|
912
|
+
componentQueries[queryName] = $query;
|
|
913
|
+
const capturedContext = componentContext;
|
|
914
|
+
const capturedArgsFn = query.args;
|
|
915
|
+
const plasmicQuery = {
|
|
916
|
+
id: query.id,
|
|
917
|
+
fn: query.fn,
|
|
918
|
+
execParams: () => capturedArgsFn(capturedContext)
|
|
919
|
+
};
|
|
920
|
+
discovered.push({ $query, query: plasmicQuery });
|
|
921
|
+
}
|
|
922
|
+
node.children.forEach((child, idx) => {
|
|
923
|
+
discovered.push(
|
|
924
|
+
...executeNode(child, {
|
|
925
|
+
context: componentContext,
|
|
926
|
+
parentKeyPath: componentKeyPath,
|
|
927
|
+
childIndex: idx,
|
|
928
|
+
queriesByComponent
|
|
929
|
+
})
|
|
785
930
|
);
|
|
786
|
-
|
|
787
|
-
|
|
931
|
+
});
|
|
932
|
+
return discovered;
|
|
933
|
+
}
|
|
934
|
+
function executeCodeComponentNode(node, params) {
|
|
935
|
+
if (node.serverRenderingConfig === false) {
|
|
936
|
+
return [];
|
|
937
|
+
}
|
|
938
|
+
const { context, parentKeyPath, childIndex, queriesByComponent } = params;
|
|
939
|
+
const nodeKeyPath = appendKeyPath(parentKeyPath, `cc${childIndex}`);
|
|
940
|
+
const evaluatedProps = {};
|
|
941
|
+
for (const [propName, propFn] of Object.entries(node.propsContext)) {
|
|
942
|
+
const result = safeExecResult(() => propFn(context));
|
|
943
|
+
if ("data" in result) {
|
|
944
|
+
evaluatedProps[propName] = result.data;
|
|
945
|
+
} else {
|
|
946
|
+
return [];
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
const childContext = {
|
|
950
|
+
$props: evaluatedProps,
|
|
951
|
+
$ctx: context.$ctx,
|
|
952
|
+
$state: context.$state,
|
|
953
|
+
$q: context.$q,
|
|
954
|
+
$scopedItemVars: context.$scopedItemVars
|
|
955
|
+
};
|
|
956
|
+
return node.children.flatMap(
|
|
957
|
+
(child, idx) => executeNode(child, {
|
|
958
|
+
context: childContext,
|
|
959
|
+
parentKeyPath: nodeKeyPath,
|
|
960
|
+
childIndex: idx,
|
|
961
|
+
queriesByComponent
|
|
962
|
+
})
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
function executeDataProviderNode(node, params) {
|
|
966
|
+
const { context, parentKeyPath, childIndex, queriesByComponent } = params;
|
|
967
|
+
const nodeKeyPath = appendKeyPath(parentKeyPath, `dp${childIndex}`);
|
|
968
|
+
const dataResult = safeExecResult(() => node.data(context));
|
|
969
|
+
if (!("data" in dataResult)) {
|
|
970
|
+
return [];
|
|
971
|
+
}
|
|
972
|
+
const childContext = {
|
|
973
|
+
$props: context.$props,
|
|
974
|
+
$ctx: __spreadProps(__spreadValues({}, context.$ctx), {
|
|
975
|
+
[node.name]: dataResult.data
|
|
976
|
+
}),
|
|
977
|
+
$state: context.$state,
|
|
978
|
+
$q: context.$q,
|
|
979
|
+
$scopedItemVars: context.$scopedItemVars
|
|
980
|
+
};
|
|
981
|
+
return node.children.flatMap(
|
|
982
|
+
(child, idx) => executeNode(child, {
|
|
983
|
+
context: childContext,
|
|
984
|
+
parentKeyPath: nodeKeyPath,
|
|
985
|
+
childIndex: idx,
|
|
986
|
+
queriesByComponent
|
|
987
|
+
})
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
function executeVisibilityNode(node, params) {
|
|
991
|
+
const { context, parentKeyPath, childIndex, queriesByComponent } = params;
|
|
992
|
+
const nodeKeyPath = appendKeyPath(parentKeyPath, `v${childIndex}`);
|
|
993
|
+
const visibilityResult = safeExecResult(() => node.visibilityExpr(context));
|
|
994
|
+
if (!("data" in visibilityResult) || !visibilityResult.data) {
|
|
995
|
+
return [];
|
|
996
|
+
}
|
|
997
|
+
return node.children.flatMap(
|
|
998
|
+
(child, idx) => executeNode(child, {
|
|
999
|
+
context,
|
|
1000
|
+
parentKeyPath: nodeKeyPath,
|
|
1001
|
+
childIndex: idx,
|
|
1002
|
+
queriesByComponent
|
|
1003
|
+
})
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
function executeRepeatedNode(node, params) {
|
|
1007
|
+
const { context, parentKeyPath, childIndex, queriesByComponent } = params;
|
|
1008
|
+
const nodeKeyPath = appendKeyPath(parentKeyPath, `r${childIndex}`);
|
|
1009
|
+
const collectionResult = safeExecResult(() => node.collectionExpr(context));
|
|
1010
|
+
if (!("data" in collectionResult) || !Array.isArray(collectionResult.data)) {
|
|
1011
|
+
return [];
|
|
1012
|
+
}
|
|
1013
|
+
return collectionResult.data.flatMap((item, index) => {
|
|
1014
|
+
const itemContext = {
|
|
1015
|
+
$props: context.$props,
|
|
1016
|
+
$ctx: context.$ctx,
|
|
1017
|
+
$state: context.$state,
|
|
1018
|
+
$q: context.$q,
|
|
1019
|
+
$scopedItemVars: __spreadProps(__spreadValues({}, context.$scopedItemVars), {
|
|
1020
|
+
[node.itemName]: item,
|
|
1021
|
+
[node.indexName]: index
|
|
1022
|
+
})
|
|
1023
|
+
};
|
|
1024
|
+
const itemKeyPath = appendKeyPath(nodeKeyPath, `i${index}`);
|
|
1025
|
+
return node.children.flatMap(
|
|
1026
|
+
(child, idx) => executeNode(child, {
|
|
1027
|
+
context: itemContext,
|
|
1028
|
+
parentKeyPath: itemKeyPath,
|
|
1029
|
+
childIndex: idx,
|
|
1030
|
+
queriesByComponent
|
|
1031
|
+
})
|
|
788
1032
|
);
|
|
789
1033
|
});
|
|
790
1034
|
}
|
|
1035
|
+
function executePlasmicQueries(rootNode, options) {
|
|
1036
|
+
return __async(this, null, function* () {
|
|
1037
|
+
const queriesByComponent = /* @__PURE__ */ new Map();
|
|
1038
|
+
const discoveredQueries = [];
|
|
1039
|
+
while (true) {
|
|
1040
|
+
const newQueries = executeQueryTree(rootNode, options, queriesByComponent);
|
|
1041
|
+
if (newQueries.length === 0) {
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
discoveredQueries.push(...newQueries);
|
|
1045
|
+
yield Promise.all(
|
|
1046
|
+
newQueries.map(
|
|
1047
|
+
(d) => executePlasmicQuery(d.$query, d.query).catch(() => {
|
|
1048
|
+
})
|
|
1049
|
+
)
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
const cache = {};
|
|
1053
|
+
for (const d of discoveredQueries) {
|
|
1054
|
+
if ("data" in d.$query.current) {
|
|
1055
|
+
cache[d.$query.current.key] = d.$query.current.data;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
const queries = {};
|
|
1059
|
+
const rootQueries = queriesByComponent.get(ROOT_COMPONENT_KEY_PATH);
|
|
1060
|
+
if (rootQueries) {
|
|
1061
|
+
for (const [name, $query] of Object.entries(rootQueries)) {
|
|
1062
|
+
if ("data" in $query.current) {
|
|
1063
|
+
queries[name] = {
|
|
1064
|
+
key: $query.key,
|
|
1065
|
+
data: $query.current.data,
|
|
1066
|
+
isLoading: false
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
for (const d of discoveredQueries) {
|
|
1072
|
+
if ("error" in d.$query.current) {
|
|
1073
|
+
throw d.$query.current.error;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
return { cache, queries };
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
791
1079
|
function executePlasmicQuery($query, query) {
|
|
792
1080
|
return __async(this, null, function* () {
|
|
793
1081
|
if ($query.current.state === "loading" || $query.current.state === "done") {
|
|
@@ -1214,7 +1502,6 @@ export {
|
|
|
1214
1502
|
makeCacheKey,
|
|
1215
1503
|
makeQueryCacheKey,
|
|
1216
1504
|
normalizeData,
|
|
1217
|
-
createDollarQueries as unstable_createDollarQueries,
|
|
1218
1505
|
executePlasmicQueries as unstable_executePlasmicQueries,
|
|
1219
1506
|
usePlasmicQueries as unstable_usePlasmicQueries,
|
|
1220
1507
|
wrapDollarQueriesForMetadata as unstable_wrapDollarQueriesForMetadata,
|