@plasmicapp/data-sources 0.1.201 → 0.1.203

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
@@ -76,6 +76,10 @@ __export(src_exports, {
76
76
  makeQueryCacheKey: () => makeQueryCacheKey,
77
77
  mkPlasmicUndefinedServerProxy: () => mkPlasmicUndefinedServerProxy,
78
78
  normalizeData: () => normalizeData,
79
+ unstable_createDollarQueries: () => createDollarQueries,
80
+ unstable_executePlasmicQueries: () => executePlasmicQueries,
81
+ unstable_usePlasmicQueries: () => usePlasmicQueries,
82
+ unstable_wrapDollarQueriesForMetadata: () => wrapDollarQueriesForMetadata,
79
83
  useDependencyAwareQuery: () => useDependencyAwareQuery,
80
84
  useNormalizedData: () => useNormalizedData,
81
85
  usePlasmicDataConfig: () => import_query5.usePlasmicDataConfig,
@@ -85,15 +89,10 @@ __export(src_exports, {
85
89
  usePlasmicServerQuery: () => usePlasmicServerQuery
86
90
  });
87
91
  module.exports = __toCommonJS(src_exports);
88
- var import_query5 = require("@plasmicapp/query");
89
-
90
- // src/components/Fetcher.tsx
91
- var import_react2 = __toESM(require("react"));
92
92
 
93
- // src/hooks/usePlasmicDataOp.tsx
94
- var import_data_sources_context = require("@plasmicapp/data-sources-context");
95
- var import_query3 = require("@plasmicapp/query");
96
- var React2 = __toESM(require("react"));
93
+ // src/serverQueries/client.ts
94
+ var import_query2 = require("@plasmicapp/query");
95
+ var React3 = __toESM(require("react"));
97
96
 
98
97
  // src/common.ts
99
98
  var import_query = require("@plasmicapp/query");
@@ -102,6 +101,14 @@ var import_react = __toESM(require("react"));
102
101
  function isPlasmicUndefinedDataErrorPromise(x) {
103
102
  return !!x && typeof x === "object" && (x == null ? void 0 : x.plasmicType) === "PlasmicUndefinedDataError";
104
103
  }
104
+ function tagPlasmicUndefinedDataErrorPromise(promise) {
105
+ promise.plasmicType = "PlasmicUndefinedDataError";
106
+ promise.message = "Query is not done";
107
+ }
108
+ function untagPlasmicUndefinedDataErrorPromise(promise) {
109
+ delete promise.plasmicType;
110
+ delete promise.message;
111
+ }
105
112
  function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
106
113
  let fetchAndUpdatePromise = void 0;
107
114
  return new Proxy(
@@ -271,9 +278,692 @@ function getConfig(key, defaultValue) {
271
278
  }
272
279
  }
273
280
 
281
+ // src/utils.ts
282
+ function noopFn() {
283
+ }
284
+ function swallow(f) {
285
+ try {
286
+ return f();
287
+ } catch (e) {
288
+ return void 0;
289
+ }
290
+ }
291
+ function pick(obj, ...keys) {
292
+ const res = {};
293
+ for (const key of keys) {
294
+ if (key in obj) {
295
+ res[key] = obj[key];
296
+ }
297
+ }
298
+ return res;
299
+ }
300
+ var tuple = (...args) => args;
301
+ function mkIdMap(xs) {
302
+ return new Map(xs.map((x) => tuple(x.id, x)));
303
+ }
304
+ function notNil(x) {
305
+ return x !== null && x !== void 0;
306
+ }
307
+ function withoutNils(xs) {
308
+ return xs.filter(notNil);
309
+ }
310
+ function mapRecordEntries(callback, record1, record2, record3) {
311
+ return Object.entries(record1).map(([k, v1]) => {
312
+ const v2 = record2 == null ? void 0 : record2[k];
313
+ const v3 = record3 == null ? void 0 : record3[k];
314
+ return callback(
315
+ k,
316
+ v1,
317
+ v2,
318
+ v3
319
+ );
320
+ });
321
+ }
322
+ function mapRecords(callback, record1, record2, record3) {
323
+ return Object.fromEntries(
324
+ mapRecordEntries(
325
+ (k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
326
+ record1,
327
+ record2,
328
+ record3
329
+ )
330
+ );
331
+ }
332
+
333
+ // src/serverQueries/common.ts
334
+ var import_react2 = __toESM(require("react"));
335
+ function createDollarQueries(queryNames) {
336
+ return Object.fromEntries(
337
+ queryNames.map((queryName) => {
338
+ return [queryName, new StatefulQueryResult()];
339
+ })
340
+ );
341
+ }
342
+ var StatefulQueryResult = class {
343
+ constructor() {
344
+ this.listeners = /* @__PURE__ */ new Set();
345
+ this.current = { state: "initial", key: null };
346
+ this.settable = new SettablePromise();
347
+ tagPlasmicUndefinedDataErrorPromise(this.settable.promise);
348
+ }
349
+ transitionState(next) {
350
+ const prev = this.current;
351
+ if (prev.state === next.state && prev.key === next.key && ("data" in prev ? prev.data : void 0) === ("data" in next ? next.data : void 0) && ("error" in prev ? prev.error : void 0) === ("error" in next ? next.error : void 0)) {
352
+ return;
353
+ }
354
+ if (prev.key !== null && // the promise was already bound to a key AND
355
+ // NOT the loading -> done transition with the same key
356
+ !(prev.key === next.key && prev.state === "loading" && next.state === "done")) {
357
+ this.settable = new SettablePromise();
358
+ tagPlasmicUndefinedDataErrorPromise(this.settable.promise);
359
+ }
360
+ this.current = next;
361
+ this.notifyListeners(prev);
362
+ }
363
+ addListener(listener) {
364
+ this.listeners.add(listener);
365
+ }
366
+ removeListener(listener) {
367
+ this.listeners.delete(listener);
368
+ }
369
+ notifyListeners(prev) {
370
+ for (const listener of this.listeners) {
371
+ listener(this.current, prev);
372
+ }
373
+ }
374
+ reset() {
375
+ this.transitionState({
376
+ state: "initial",
377
+ key: null
378
+ });
379
+ }
380
+ loadingPromise(key, promise) {
381
+ this.transitionState({
382
+ state: "loading",
383
+ key
384
+ });
385
+ promise.then(
386
+ (value) => this.resolvePromise(key, value),
387
+ (err) => this.rejectPromise(key, err)
388
+ );
389
+ }
390
+ /**
391
+ * Resolve is allowed if:
392
+ * 1) no key / state is initial, which means we are resolving from cache
393
+ * 2) key / state is loading, which means we need to check the keys match
394
+ */
395
+ resolvePromise(key, data) {
396
+ if (this.current.key === null || this.current.key === key) {
397
+ this.transitionState({
398
+ state: "done",
399
+ key,
400
+ data
401
+ });
402
+ this.settable.resolve(data);
403
+ untagPlasmicUndefinedDataErrorPromise(this.settable.promise);
404
+ }
405
+ }
406
+ /**
407
+ * Reject is allowed if:
408
+ * 1) no key / state is initial, which means params resolution failed
409
+ * 2) key / state is loading, which means we need to check the keys match
410
+ */
411
+ rejectPromise(key, error) {
412
+ if (this.current.key === null || this.current.key === key) {
413
+ this.transitionState({
414
+ state: "done",
415
+ key,
416
+ error
417
+ });
418
+ this.settable.promise.catch(noopFn);
419
+ this.settable.reject(error);
420
+ untagPlasmicUndefinedDataErrorPromise(this.settable.promise);
421
+ }
422
+ }
423
+ get key() {
424
+ return this.current.key;
425
+ }
426
+ get data() {
427
+ if (this.current.state === "done") {
428
+ if ("data" in this.current) {
429
+ return this.current.data;
430
+ } else {
431
+ throw this.current.error;
432
+ }
433
+ } else {
434
+ throw this.settable.promise;
435
+ }
436
+ }
437
+ get isLoading() {
438
+ return this.current.state === "initial" || this.current.state === "loading";
439
+ }
440
+ getDoneResult() {
441
+ return __async(this, null, function* () {
442
+ yield this.settable.promise;
443
+ return this;
444
+ });
445
+ }
446
+ };
447
+ function safeExec(tryData, ifUndefined, ifError) {
448
+ try {
449
+ return tryData();
450
+ } catch (err) {
451
+ if (isPlasmicUndefinedDataErrorPromise(err)) {
452
+ return ifUndefined(err);
453
+ } else {
454
+ return ifError(err);
455
+ }
456
+ }
457
+ }
458
+ function resolveParams(params) {
459
+ return safeExec(
460
+ () => ({
461
+ status: "ready",
462
+ resolvedParams: params()
463
+ }),
464
+ (promise) => ({
465
+ status: "blocked",
466
+ promise
467
+ }),
468
+ (err) => ({
469
+ status: "error",
470
+ error: new Error("Error resolving function params", { cause: err })
471
+ })
472
+ );
473
+ }
474
+ function wrapDollarQueriesForMetadata($queries, ifUndefined, ifError) {
475
+ return wrapDollarQueriesWithFallbacks(
476
+ $queries,
477
+ ifUndefined != null ? ifUndefined : () => "\u2026",
478
+ ifError != null ? ifError : () => "[ERROR]"
479
+ );
480
+ }
481
+ function wrapDollarQueriesWithFallbacks($queries, ifUndefined, ifError) {
482
+ return mapRecords(
483
+ (_queryName, $query) => new FallbackQueryResult($query, ifUndefined, ifError),
484
+ $queries
485
+ );
486
+ }
487
+ var FallbackQueryResult = class {
488
+ constructor($query, ifUndefined, ifError) {
489
+ this.$query = $query;
490
+ this.ifUndefined = ifUndefined;
491
+ this.ifError = ifError;
492
+ }
493
+ get key() {
494
+ return this.$query.key;
495
+ }
496
+ get data() {
497
+ return safeExec(
498
+ () => this.$query.data,
499
+ (promise) => createConstantProxy(this.ifUndefined(promise)),
500
+ (err) => createConstantProxy(this.ifError(err))
501
+ );
502
+ }
503
+ get isLoading() {
504
+ return this.$query.isLoading;
505
+ }
506
+ };
507
+ function createConstantProxy(constant) {
508
+ const constantProxy = new Proxy(
509
+ {},
510
+ {
511
+ get(_target, prop) {
512
+ return prop === Symbol.toPrimitive ? () => constant : constantProxy;
513
+ }
514
+ }
515
+ );
516
+ return constantProxy;
517
+ }
518
+ var SyncPromise = class {
519
+ constructor(promise) {
520
+ this.promise = promise;
521
+ this.result = void 0;
522
+ promise.then(
523
+ (value) => {
524
+ this.result = {
525
+ state: "resolved",
526
+ value
527
+ };
528
+ },
529
+ (error) => {
530
+ this.result = {
531
+ state: "rejected",
532
+ error
533
+ };
534
+ }
535
+ );
536
+ }
537
+ };
538
+ var SettablePromise = class {
539
+ constructor() {
540
+ this.promise = new Promise((resolve, reject) => {
541
+ this._resolve = resolve;
542
+ this._reject = reject;
543
+ });
544
+ }
545
+ resolve(value) {
546
+ this._resolve(value);
547
+ }
548
+ reject(error) {
549
+ this._reject(error);
550
+ }
551
+ };
552
+ function useRenderEffect(effect, deps) {
553
+ const ref = import_react2.default.useRef({ deps: void 0, cleanup: void 0 });
554
+ const depsChanged = ref.current.deps === void 0 || deps.length !== ref.current.deps.length || deps.some((dep, i) => !Object.is(dep, ref.current.deps[i]));
555
+ if (depsChanged) {
556
+ if (ref.current.cleanup) {
557
+ ref.current.cleanup();
558
+ }
559
+ const prevDeps = ref.current.deps;
560
+ ref.current.cleanup = effect(prevDeps);
561
+ ref.current.deps = deps;
562
+ }
563
+ import_react2.default.useEffect(() => {
564
+ return () => {
565
+ if (ref.current.cleanup) {
566
+ ref.current.cleanup();
567
+ }
568
+ };
569
+ }, []);
570
+ }
571
+
572
+ // src/serverQueries/makeQueryCacheKey.ts
573
+ function makeQueryCacheKey(id, params) {
574
+ return `${id}:${safeStableStringify(params)}`;
575
+ }
576
+ var shortPlasmicPrefix = "\u03C1";
577
+ function safeStableStringify(unstableValue) {
578
+ const stableValue = sortObjectsDeep(unstableValue, /* @__PURE__ */ new Map());
579
+ const visitedPaths = /* @__PURE__ */ new Map();
580
+ return JSON.stringify(stableValue, function(key, value) {
581
+ switch (typeof value) {
582
+ case "undefined":
583
+ return `${shortPlasmicPrefix}:UNDEFINED`;
584
+ case "function":
585
+ return `${shortPlasmicPrefix}:FUNCTION:${value.name}`;
586
+ case "symbol":
587
+ return `${shortPlasmicPrefix}:SYMBOL:${value.description}`;
588
+ case "bigint":
589
+ return value.toString();
590
+ case "object":
591
+ if (value !== null) {
592
+ const cyclePath = visitedPaths.get(value);
593
+ if (cyclePath) {
594
+ return `${shortPlasmicPrefix}:REF:${cyclePath}`;
595
+ }
596
+ const parentPath = visitedPaths.get(this);
597
+ const valuePath = parentPath === void 0 ? "$" : `${parentPath}.${key}`;
598
+ visitedPaths.set(value, valuePath);
599
+ }
600
+ return value;
601
+ default:
602
+ return value;
603
+ }
604
+ });
605
+ }
606
+ function sortObjectsDeep(value, visitedObjects) {
607
+ if (typeof value !== "object" || value === null) {
608
+ return value;
609
+ }
610
+ const visitedValue = visitedObjects.get(value);
611
+ if (visitedValue) {
612
+ return visitedValue;
613
+ }
614
+ if (Array.isArray(value)) {
615
+ const newArr = [];
616
+ visitedObjects.set(value, newArr);
617
+ value.forEach((item, key) => {
618
+ newArr[key] = sortObjectsDeep(item, visitedObjects);
619
+ });
620
+ return newArr;
621
+ } else {
622
+ const newObj = {};
623
+ visitedObjects.set(value, newObj);
624
+ Object.keys(value).sort().forEach((key) => {
625
+ newObj[key] = sortObjectsDeep(value[key], visitedObjects);
626
+ });
627
+ return newObj;
628
+ }
629
+ }
630
+
631
+ // src/serverQueries/client.ts
632
+ var GLOBAL_CACHE = /* @__PURE__ */ new Map();
633
+ function usePlasmicQueries($queries, queries) {
634
+ const wrappedQueries = React3.useMemo(() => wrapQueries(queries), [queries]);
635
+ const $queryStates = $queries;
636
+ const { fallback: prefetchedCache, cache: swrCache } = (0, import_query2.usePlasmicDataConfig)();
637
+ const [settledCount, setSettledCount] = React3.useState(0);
638
+ React3.useEffect(() => {
639
+ let cleanup = false;
640
+ const resultListener = (next, prev) => {
641
+ if (cleanup) {
642
+ return;
643
+ }
644
+ if (prev.state === "done" || next.state === "done") {
645
+ queueMicrotask(() => setSettledCount((v) => v + 1));
646
+ }
647
+ };
648
+ mapRecords((_queryName, $query) => {
649
+ $query.addListener(resultListener);
650
+ }, $queryStates);
651
+ return () => {
652
+ cleanup = true;
653
+ mapRecords((_queryName, $query) => {
654
+ $query.removeListener(resultListener);
655
+ }, $queryStates);
656
+ };
657
+ }, [$queryStates]);
658
+ useRenderEffect(
659
+ (prevDeps) => {
660
+ if (prevDeps) {
661
+ const prevWrappedQueries = prevDeps[0];
662
+ if (!Object.is(prevWrappedQueries, wrappedQueries)) {
663
+ mapRecords((_queryName, $query) => {
664
+ $query.reset();
665
+ }, $queryStates);
666
+ }
667
+ }
668
+ let cleanup = false;
669
+ const loop = () => __async(this, null, function* () {
670
+ while (true) {
671
+ initPlasmicQueriesSync(
672
+ $queryStates,
673
+ wrappedQueries,
674
+ prefetchedCache,
675
+ swrCache
676
+ );
677
+ const loadingQueries = mapRecordEntries((_queryName, $query) => {
678
+ if ($query.isLoading) {
679
+ return $query.getDoneResult();
680
+ } else {
681
+ return null;
682
+ }
683
+ }, $queryStates).filter(notNil);
684
+ if (loadingQueries.length === 0) {
685
+ break;
686
+ }
687
+ yield Promise.race(loadingQueries);
688
+ if (cleanup) {
689
+ break;
690
+ }
691
+ }
692
+ });
693
+ loop().catch(noopFn);
694
+ return () => {
695
+ cleanup = true;
696
+ };
697
+ },
698
+ [wrappedQueries, $queryStates, settledCount]
699
+ );
700
+ mapRecordEntries(
701
+ (_queryName, $query, query) => {
702
+ usePlasmicServerQuery(
703
+ query,
704
+ void 0,
705
+ {
706
+ settledCount,
707
+ onStarted: $query.loadingPromise.bind($query),
708
+ onResolved: $query.resolvePromise.bind($query),
709
+ onRejected: $query.rejectPromise.bind($query)
710
+ }
711
+ );
712
+ },
713
+ $queryStates,
714
+ wrappedQueries
715
+ );
716
+ }
717
+ function wrapQueries(queries) {
718
+ return mapRecords((_queryName, query) => {
719
+ const wrappedFn = (...args) => {
720
+ const cacheKey = makeQueryCacheKey(query.id, args);
721
+ const cached = GLOBAL_CACHE.get(cacheKey);
722
+ if (cached) {
723
+ return cached.promise;
724
+ }
725
+ const wrapStudioCache = getConfig(
726
+ "EXECUTE_SERVER_QUERY",
727
+ (_, fn, ...args2) => fn(...args2)
728
+ );
729
+ const promise = (0, import_query2.wrapLoadingFetcher)(wrapStudioCache)(
730
+ query.id,
731
+ query.fn,
732
+ ...args
733
+ );
734
+ GLOBAL_CACHE.set(cacheKey, new SyncPromise(promise));
735
+ return promise;
736
+ };
737
+ return {
738
+ id: query.id,
739
+ fn: wrappedFn,
740
+ execParams: query.execParams
741
+ };
742
+ }, queries);
743
+ }
744
+ function initPlasmicQueriesSync($queries, queries, prefetchedCache, clientCache) {
745
+ let anySettled;
746
+ do {
747
+ anySettled = false;
748
+ mapRecords(
749
+ (_queryName, $query, query) => {
750
+ if ($query.current.state !== "initial") {
751
+ return;
752
+ }
753
+ const paramsResult = resolveParams(query.execParams);
754
+ if (paramsResult.status === "error") {
755
+ $query.rejectPromise(null, paramsResult.error);
756
+ anySettled = true;
757
+ return;
758
+ } else if (paramsResult.status === "blocked") {
759
+ return;
760
+ }
761
+ const cacheKey = makeQueryCacheKey(
762
+ query.id,
763
+ paramsResult.resolvedParams
764
+ );
765
+ if (cacheKey in prefetchedCache) {
766
+ $query.resolvePromise(cacheKey, prefetchedCache[cacheKey]);
767
+ anySettled = true;
768
+ return;
769
+ }
770
+ const clientCacheData = clientCache.get(cacheKey);
771
+ if (clientCacheData !== void 0) {
772
+ $query.resolvePromise(cacheKey, clientCacheData);
773
+ anySettled = true;
774
+ return;
775
+ }
776
+ const clientCachedPromise = GLOBAL_CACHE.get(cacheKey);
777
+ if (clientCachedPromise == null ? void 0 : clientCachedPromise.result) {
778
+ if (clientCachedPromise.result.state === "resolved") {
779
+ $query.resolvePromise(cacheKey, clientCachedPromise.result.value);
780
+ } else {
781
+ $query.rejectPromise(cacheKey, clientCachedPromise.result.error);
782
+ }
783
+ anySettled = true;
784
+ return;
785
+ }
786
+ $query.loadingPromise(
787
+ cacheKey,
788
+ query.fn(...paramsResult.resolvedParams)
789
+ );
790
+ },
791
+ $queries,
792
+ queries
793
+ );
794
+ } while (anySettled);
795
+ }
796
+ function usePlasmicServerQuery(query, fallbackData, opts) {
797
+ var _a, _b;
798
+ const paramsResult = React3.useMemo(() => {
799
+ return resolveParams(query.execParams);
800
+ }, [query.execParams, opts == null ? void 0 : opts.settledCount]);
801
+ const { key, fetcher } = React3.useMemo(() => {
802
+ switch (paramsResult.status) {
803
+ case "blocked":
804
+ case "error": {
805
+ return {
806
+ key: null,
807
+ fetcher: () => {
808
+ throw new Error("fetcher unexpectedly invoked");
809
+ }
810
+ };
811
+ }
812
+ case "ready": {
813
+ const cacheKey = makeQueryCacheKey(
814
+ query.id,
815
+ paramsResult.resolvedParams
816
+ );
817
+ return {
818
+ key: cacheKey,
819
+ fetcher: () => {
820
+ var _a2;
821
+ const promise = query.fn(...paramsResult.resolvedParams);
822
+ (_a2 = opts == null ? void 0 : opts.onStarted) == null ? void 0 : _a2.call(opts, cacheKey, promise);
823
+ return promise.finally(() => {
824
+ GLOBAL_CACHE.delete(cacheKey);
825
+ });
826
+ }
827
+ };
828
+ }
829
+ }
830
+ }, [query, paramsResult]);
831
+ const result = (0, import_query2.useMutablePlasmicQueryData)(key, fetcher, {
832
+ // If revalidateIfStale is true, then if there's a cache entry with a key,
833
+ // but no mounted hook with that key yet, and when the hook mounts with the key,
834
+ // swr will revalidate. This may be reasonable behavior, but for us, this
835
+ // happens all the time -- we prepopulate the cache with proxy-invoked fetch,
836
+ // sometimes before swr had a chance to run the effect. So we turn off
837
+ // revalidateIfStale here, and just let the user manage invalidation.
838
+ revalidateIfStale: false,
839
+ // TODO: Remove per-hook fallbackData
840
+ // Only used in older server query implementation.
841
+ // New implementation should use prefetchedCache instead.
842
+ fallbackData
843
+ });
844
+ if (!result.isLoading) {
845
+ if (result.error) {
846
+ (_a = opts == null ? void 0 : opts.onRejected) == null ? void 0 : _a.call(opts, key, result.error);
847
+ } else if (key && result.data !== void 0) {
848
+ (_b = opts == null ? void 0 : opts.onResolved) == null ? void 0 : _b.call(opts, key, result.data);
849
+ }
850
+ }
851
+ return result;
852
+ }
853
+
854
+ // src/serverQueries/server.ts
855
+ function executePlasmicQueries($queries, queries) {
856
+ return __async(this, null, function* () {
857
+ const doneQueryResults = yield Promise.all(
858
+ mapRecordEntries(
859
+ (_queryName, $query, query) => {
860
+ return executePlasmicQuery($query, query);
861
+ },
862
+ $queries,
863
+ queries
864
+ )
865
+ );
866
+ return Object.fromEntries(
867
+ Object.values(doneQueryResults).map(($query) => [$query.key, $query.data])
868
+ );
869
+ });
870
+ }
871
+ function executePlasmicQuery($query, query) {
872
+ return __async(this, null, function* () {
873
+ if ($query.current.state === "loading" || $query.current.state === "done") {
874
+ return $query.getDoneResult();
875
+ }
876
+ do {
877
+ const paramsResult = resolveParams(query.execParams);
878
+ switch (paramsResult.status) {
879
+ case "blocked": {
880
+ try {
881
+ yield paramsResult.promise;
882
+ } catch (e) {
883
+ }
884
+ continue;
885
+ }
886
+ case "ready": {
887
+ const cacheKey = makeQueryCacheKey(
888
+ query.id,
889
+ paramsResult.resolvedParams
890
+ );
891
+ $query.loadingPromise(
892
+ cacheKey,
893
+ query.fn(...paramsResult.resolvedParams)
894
+ );
895
+ return $query.getDoneResult();
896
+ }
897
+ case "error": {
898
+ $query.rejectPromise(null, paramsResult.error);
899
+ throw paramsResult.error;
900
+ }
901
+ }
902
+ } while (true);
903
+ });
904
+ }
905
+ var PlasmicUndefinedServerError = class extends Error {
906
+ constructor(msg) {
907
+ super(msg);
908
+ this.plasmicType = "PlasmicUndefinedServerError";
909
+ }
910
+ };
911
+ function isPlasmicUndefinedServerError(x) {
912
+ return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedServerError";
913
+ }
914
+ function mkPlasmicUndefinedServerProxy() {
915
+ return {
916
+ data: new Proxy(
917
+ {},
918
+ {
919
+ get: (_, prop) => {
920
+ if (prop === "isUndefinedServerProxy") {
921
+ return true;
922
+ } else if (prop === "then") {
923
+ return void 0;
924
+ }
925
+ throw new PlasmicUndefinedServerError("Data is not available yet");
926
+ }
927
+ }
928
+ ),
929
+ isLoading: true
930
+ };
931
+ }
932
+ function executeServerQuery(query) {
933
+ return __async(this, null, function* () {
934
+ const resolvedParams = resolveServerParams(query.execParams);
935
+ if (isPlasmicUndefinedServerError(resolvedParams)) {
936
+ return mkPlasmicUndefinedServerProxy();
937
+ }
938
+ return { data: yield query.fn(...resolvedParams), isLoading: false };
939
+ });
940
+ }
941
+ function resolveServerParams(params) {
942
+ try {
943
+ return params();
944
+ } catch (err) {
945
+ if (isPlasmicUndefinedServerError(err)) {
946
+ return err;
947
+ } else {
948
+ throw err;
949
+ }
950
+ }
951
+ }
952
+
953
+ // src/index.tsx
954
+ var import_query5 = require("@plasmicapp/query");
955
+
956
+ // src/components/Fetcher.tsx
957
+ var import_react3 = __toESM(require("react"));
958
+
959
+ // src/hooks/usePlasmicDataOp.tsx
960
+ var import_data_sources_context = require("@plasmicapp/data-sources-context");
961
+ var import_query4 = require("@plasmicapp/query");
962
+ var React4 = __toESM(require("react"));
963
+
274
964
  // src/executor.tsx
275
965
  var import_isomorphic_unfetch = __toESM(require("@plasmicapp/isomorphic-unfetch"));
276
- var import_query2 = require("@plasmicapp/query");
966
+ var import_query3 = require("@plasmicapp/query");
277
967
  var import_fast_stringify = __toESM(require("fast-stringify"));
278
968
 
279
969
  // src/placeholders.ts
@@ -301,7 +991,7 @@ function executePlasmicDataOp(op, opts) {
301
991
  _executePlasmicDataOp
302
992
  );
303
993
  op.userArgs = addPlaceholdersToUserArgs(op.userArgs);
304
- const res = yield (0, import_query2.wrapLoadingFetcher)(func)(op, opts);
994
+ const res = yield (0, import_query3.wrapLoadingFetcher)(func)(op, opts);
305
995
  return res;
306
996
  });
307
997
  }
@@ -345,31 +1035,6 @@ function getConfig2(key, defaultValue) {
345
1035
  }
346
1036
  }
347
1037
 
348
- // src/utils.ts
349
- function swallow(f) {
350
- try {
351
- return f();
352
- } catch (e) {
353
- return void 0;
354
- }
355
- }
356
- function pick(obj, ...keys) {
357
- const res = {};
358
- for (const key of keys) {
359
- if (key in obj) {
360
- res[key] = obj[key];
361
- }
362
- }
363
- return res;
364
- }
365
- var tuple = (...args) => args;
366
- function mkIdMap(xs) {
367
- return new Map(xs.map((x) => tuple(x.id, x)));
368
- }
369
- function withoutNils(xs) {
370
- return xs.filter((x) => x != null);
371
- }
372
-
373
1038
  // src/hooks/usePlasmicDataOp.tsx
374
1039
  function makeCacheKey(dataOp, opts) {
375
1040
  const queryDependencies = JSON.stringify({
@@ -382,7 +1047,7 @@ function makeCacheKey(dataOp, opts) {
382
1047
  return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
383
1048
  }
384
1049
  function usePlasmicInvalidate() {
385
- const { cache, fallback, mutate } = (0, import_query3.usePlasmicDataConfig)();
1050
+ const { cache, fallback, mutate } = (0, import_query4.usePlasmicDataConfig)();
386
1051
  return (invalidatedKeys) => __async(this, null, function* () {
387
1052
  const getKeysToInvalidate = () => {
388
1053
  var _a, _b;
@@ -477,7 +1142,7 @@ function usePlasmicDataOp(dataOp, opts) {
477
1142
  function usePlasmicDataMutationOp(dataOp) {
478
1143
  const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
479
1144
  const userToken = ctx == null ? void 0 : ctx.userAuthToken;
480
- const getRealDataOp = React2.useCallback(() => __async(this, null, function* () {
1145
+ const getRealDataOp = React4.useCallback(() => __async(this, null, function* () {
481
1146
  const tryGetRealDataOp = () => __async(this, null, function* () {
482
1147
  const resolved = resolveDataOp(dataOp);
483
1148
  if (!resolved) {
@@ -491,7 +1156,7 @@ function usePlasmicDataMutationOp(dataOp) {
491
1156
  });
492
1157
  return yield tryGetRealDataOp();
493
1158
  }), [dataOp]);
494
- return React2.useCallback(() => __async(this, null, function* () {
1159
+ return React4.useCallback(() => __async(this, null, function* () {
495
1160
  var _a;
496
1161
  const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
497
1162
  if (!sourceId || !opId) {
@@ -514,7 +1179,7 @@ function Fetcher(props) {
514
1179
  const data = usePlasmicDataOp(dataOp, __spreadValues({}, !!pageIndex && !!pageSize && {
515
1180
  paginate: { pageIndex, pageSize }
516
1181
  }));
517
- const $queries = import_react2.default.useMemo(
1182
+ const $queries = import_react3.default.useMemo(
518
1183
  () => __spreadProps(__spreadValues({}, props.queries), { [name != null ? name : "data"]: data }),
519
1184
  [props.queries, name, data]
520
1185
  );
@@ -556,7 +1221,7 @@ var FetcherMeta = {
556
1221
  };
557
1222
 
558
1223
  // src/helpers.ts
559
- var import_react3 = require("react");
1224
+ var import_react4 = require("react");
560
1225
  function normalizeData(rawData) {
561
1226
  var _a;
562
1227
  if (!rawData) {
@@ -573,7 +1238,7 @@ function normalizeData(rawData) {
573
1238
  return { data: dataArray, schema };
574
1239
  }
575
1240
  function useNormalizedData(rawData) {
576
- return (0, import_react3.useMemo)(() => normalizeData(rawData), [rawData]);
1241
+ return (0, import_react4.useMemo)(() => normalizeData(rawData), [rawData]);
577
1242
  }
578
1243
  function tryGetDataArray(rawData) {
579
1244
  if (rawData == null || typeof rawData !== "object") {
@@ -667,10 +1332,10 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema, makeDefaultConfig) {
667
1332
  }
668
1333
 
669
1334
  // src/hooks/useDependencyAwareQuery.tsx
670
- var import_react4 = __toESM(require("react"));
1335
+ var import_react5 = __toESM(require("react"));
671
1336
  function usePrevious(value) {
672
- const prevValue = import_react4.default.useRef(void 0);
673
- import_react4.default.useEffect(() => {
1337
+ const prevValue = import_react5.default.useRef(void 0);
1338
+ import_react5.default.useEffect(() => {
674
1339
  prevValue.current = value;
675
1340
  return () => {
676
1341
  prevValue.current = void 0;
@@ -691,7 +1356,7 @@ function useDependencyAwareQuery({
691
1356
  }));
692
1357
  const finalName = name != null ? name : "data";
693
1358
  const prevName = usePrevious(finalName);
694
- import_react4.default.useEffect(() => {
1359
+ import_react5.default.useEffect(() => {
695
1360
  if (!(finalName in $queries) || $queries[finalName] !== data) {
696
1361
  const $queries2 = __spreadProps(__spreadValues({}, $queries), {
697
1362
  [finalName]: data
@@ -703,97 +1368,4 @@ function useDependencyAwareQuery({
703
1368
  }
704
1369
  }, [finalName, prevName, data, $queries, setDollarQueries]);
705
1370
  }
706
-
707
- // src/serverQueries/client.ts
708
- var import_query4 = require("@plasmicapp/query");
709
-
710
- // src/serverQueries/common.ts
711
- function resolveParams(params, errorFn) {
712
- try {
713
- return params();
714
- } catch (err) {
715
- return errorFn(err);
716
- }
717
- }
718
-
719
- // src/serverQueries/client.ts
720
- function makeQueryCacheKey(id, params) {
721
- return `${id}:${JSON.stringify(params)}`;
722
- }
723
- function usePlasmicServerQuery(serverQuery, fallbackData, opts) {
724
- const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
725
- if (isPlasmicUndefinedDataErrorPromise(err)) {
726
- return err;
727
- }
728
- return null;
729
- });
730
- const key = !resolvedParams || isPlasmicUndefinedDataErrorPromise(resolvedParams) ? null : makeQueryCacheKey(serverQuery.id, resolvedParams);
731
- const wrapStudioCache = getConfig(
732
- "EXECUTE_SERVER_QUERY",
733
- (_, fn, ...args) => fn(...args)
734
- );
735
- const fetcher = (params) => {
736
- return (0, import_query4.wrapLoadingFetcher)(wrapStudioCache)(
737
- serverQuery.id,
738
- serverQuery.fn,
739
- ...params
740
- );
741
- };
742
- const resultMapper = (result) => {
743
- return __spreadValues({}, pick(result, "data", "error", "isLoading"));
744
- };
745
- return usePlasmicFetch(
746
- key,
747
- resolvedParams,
748
- fetcher,
749
- resultMapper,
750
- ["data", "error"],
751
- __spreadValues({
752
- fallbackData
753
- }, opts)
754
- );
755
- }
756
-
757
- // src/serverQueries/server.ts
758
- var PlasmicUndefinedServerError = class extends Error {
759
- constructor(msg) {
760
- super(msg);
761
- this.plasmicType = "PlasmicUndefinedServerError";
762
- }
763
- };
764
- function isPlasmicUndefinedServerError(x) {
765
- return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedServerError";
766
- }
767
- function mkPlasmicUndefinedServerProxy() {
768
- return {
769
- data: new Proxy(
770
- {},
771
- {
772
- get: (_, prop) => {
773
- if (prop === "isUndefinedServerProxy") {
774
- return true;
775
- } else if (prop === "then") {
776
- return void 0;
777
- }
778
- throw new PlasmicUndefinedServerError("Data is not available yet");
779
- }
780
- }
781
- ),
782
- isLoading: true
783
- };
784
- }
785
- function executeServerQuery(serverQuery) {
786
- return __async(this, null, function* () {
787
- const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
788
- if (isPlasmicUndefinedServerError(err)) {
789
- return err;
790
- }
791
- throw err;
792
- });
793
- if (isPlasmicUndefinedServerError(resolvedParams)) {
794
- return mkPlasmicUndefinedServerProxy();
795
- }
796
- return { data: yield serverQuery.fn(...resolvedParams), isLoading: false };
797
- });
798
- }
799
1371
  //# sourceMappingURL=index.js.map