@bgub/fig 2.1.0 → 2.2.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.
@@ -1,3 +1,4 @@
1
+ import { P as readBrowser } from "./element-DpXdLgOZ.js";
1
2
  import { y as isThenable$1 } from "./resource-DEDmLSNk.js";
2
3
  //#region src/context.ts
3
4
  /** The Fig context symbol. */
@@ -68,6 +69,33 @@ function sameDataResourceKey(a, b) {
68
69
  function isAttributableError(value) {
69
70
  return (typeof value === "object" || typeof value === "function") && value !== null;
70
71
  }
72
+ const MissingCapabilityBrand = Symbol.for("fig.data-missing-capability");
73
+ /**
74
+ * Thrown by `requireCapability`: this load cannot run in the current
75
+ * environment. During server rendering, a read of the entry leaves the
76
+ * nearest Suspense fallback for the browser to render.
77
+ */
78
+ var MissingCapabilityError = class extends Error {
79
+ [MissingCapabilityBrand] = true;
80
+ reason;
81
+ constructor(reason) {
82
+ super("This data cannot be loaded here.");
83
+ this.name = "MissingCapabilityError";
84
+ this.reason = reason;
85
+ }
86
+ };
87
+ /**
88
+ * Asserts that a load's required capability is available. A false condition
89
+ * throws MissingCapabilityError; a true condition narrows it and lets the
90
+ * load continue in environments that provide the capability.
91
+ */
92
+ function requireCapability(condition, reason) {
93
+ if (!condition) throw new MissingCapabilityError(reason);
94
+ }
95
+ /** Checks whether a value is a missing-capability error. */
96
+ function isMissingCapability(value) {
97
+ return typeof value === "object" && value !== null && value[MissingCapabilityBrand] === true;
98
+ }
71
99
  //#endregion
72
100
  //#region src/data-store.ts
73
101
  const DataResourceSymbol = Symbol.for("fig.data-resource");
@@ -78,6 +106,7 @@ const DEFAULT_PRELOAD_RETENTION_MS = 3e4;
78
106
  const dataStoreFactory = createRendererDataStore;
79
107
  /** Data resource. */
80
108
  function dataResource(options) {
109
+ if (typeof options.transfer === "function" && options.load === void 0) throw new Error("A data resource with a transfer function requires a loader.");
81
110
  return {
82
111
  $$typeof: DataResourceSymbol,
83
112
  [DataStoreFactorySymbol]: dataStoreFactory,
@@ -86,6 +115,7 @@ function dataResource(options) {
86
115
  inactiveRetentionMs: options.inactiveRetentionMs,
87
116
  key: options.key,
88
117
  load: options.load,
118
+ transfer: options.transfer,
89
119
  watch: options.watch
90
120
  };
91
121
  }
@@ -131,6 +161,7 @@ function normalizeDataResourceKey(key) {
131
161
  var DefaultDataStore = class {
132
162
  host;
133
163
  entries = /* @__PURE__ */ new Map();
164
+ awaitedForms = /* @__PURE__ */ new Set();
134
165
  inactiveRetentionMs;
135
166
  ownerKeys = /* @__PURE__ */ new WeakMap();
136
167
  preloadRetentionMs;
@@ -218,12 +249,23 @@ var DefaultDataStore = class {
218
249
  }
219
250
  dispose() {
220
251
  this.disposed = true;
252
+ this.awaitedForms.clear();
253
+ let failure;
254
+ for (const entry of this.entries.values()) try {
255
+ const transferred = transferValue(entry);
256
+ if (transferred === void 0) continue;
257
+ entry.value = transferred.value;
258
+ entry.transferred = true;
259
+ } catch (error) {
260
+ failure ??= { error };
261
+ }
221
262
  for (const entry of this.entries.values()) {
222
263
  this.clearInactiveTimer(entry);
223
264
  this.clearPreloadTimer(entry);
224
265
  this.abortEntryGenerations(entry, "store-disposed");
225
266
  this.notifyEntryChange(entry);
226
267
  }
268
+ if (failure !== void 0) throw failure.error;
227
269
  }
228
270
  hydrate(entries) {
229
271
  this.hydrateEntries(entries, true);
@@ -238,28 +280,76 @@ var DefaultDataStore = class {
238
280
  const normalized = normalizeKey(hydrated.key);
239
281
  const storeKey = normalized.canonical;
240
282
  const current = this.entries.get(storeKey);
283
+ this.awaitedForms.delete(storeKey);
241
284
  if (current !== void 0) {
242
- if (replaceValues || !entryHasValue(current)) this.hydrateEntry(current, normalized.key, hydrated.value);
285
+ if (current.parkedLoad !== null) {
286
+ current.seed = { value: hydrated.value };
287
+ runParkedLoad(current);
288
+ continue;
289
+ }
290
+ if (replaceValues || !entryHasValue(current)) this.hydrateEntry(current, normalized.key, hydrated.value, true);
243
291
  continue;
244
292
  }
245
293
  const entry = this.createEntry(normalized, null, null, "fulfilled", hydrated.value);
294
+ entry.transferred = true;
246
295
  this.entries.set(storeKey, entry);
247
296
  this.notifyEntryChange(entry);
248
297
  }
249
298
  }
299
+ awaitTransferForms(keys) {
300
+ if (this.disposed) return;
301
+ for (const key of keys) {
302
+ const canonical = normalizeKey(key).canonical;
303
+ const entry = this.entries.get(canonical);
304
+ if (entry !== void 0 && (entryHasValue(entry) || entry.pending !== null)) continue;
305
+ this.awaitedForms.add(canonical);
306
+ }
307
+ }
308
+ releaseTransferForms() {
309
+ const keys = [...this.awaitedForms];
310
+ this.awaitedForms.clear();
311
+ for (const key of keys) {
312
+ const entry = this.entries.get(key);
313
+ if (entry !== void 0) runParkedLoad(entry);
314
+ }
315
+ }
250
316
  snapshot() {
251
317
  return this.snapshotKeys(this.entries.keys());
252
318
  }
253
- snapshotKeys(canonicalKeys) {
319
+ snapshotKeys(canonicalKeys, deferred) {
254
320
  const entries = [];
255
- for (const key of canonicalKeys) {
256
- const entry = this.entries.get(key);
257
- if (entry !== void 0 && entryHasValue(entry)) entries.push({
321
+ this.collectSnapshot(canonicalKeys, entries, deferred ?? entries);
322
+ return entries;
323
+ }
324
+ snapshotDependencies(canonicalKeys) {
325
+ const dependencies = [];
326
+ this.collectSnapshot(canonicalKeys, null, dependencies);
327
+ return dependencies;
328
+ }
329
+ collectSnapshot(canonicalKeys, requestedRows, dependencyRows) {
330
+ const sources = /* @__PURE__ */ new Map();
331
+ for (const entry of this.entries.values()) for (const retainer of entry.retainers ?? []) {
332
+ const retained = sources.get(retainer);
333
+ if (retained === void 0) sources.set(retainer, [entry]);
334
+ else retained.push(entry);
335
+ }
336
+ const visited = /* @__PURE__ */ new Set();
337
+ const requested = new Set(canonicalKeys);
338
+ const visit = (entry) => {
339
+ if (entry === void 0 || visited.has(entry)) return;
340
+ visited.add(entry);
341
+ const signal = entry.valueController?.signal;
342
+ if (signal !== void 0) sources.get(signal)?.forEach(visit);
343
+ const rows = requested.has(entry.canonicalKey) ? requestedRows : typeof entry.resource?.transfer === "function" ? dependencyRows : null;
344
+ if (rows === null) return;
345
+ const transferred = transferValue(entry);
346
+ if (transferred === void 0) return;
347
+ rows.push({
258
348
  key: entry.key,
259
- value: entry.value
349
+ value: transferred.value
260
350
  });
261
- }
262
- return entries;
351
+ };
352
+ for (const key of requested) visit(this.entries.get(key));
263
353
  }
264
354
  inspectDataEntries() {
265
355
  return Array.from(this.entries.values(), (entry) => this.snapshotEntry(entry));
@@ -400,6 +490,7 @@ var DefaultDataStore = class {
400
490
  const current = this.entries.get(key);
401
491
  if (current !== void 0) {
402
492
  if (resource.inactiveRetentionMs !== void 0) current.inactiveRetentionMs = resource.inactiveRetentionMs;
493
+ this.adoptResource(current, resource);
403
494
  return current;
404
495
  }
405
496
  if (!create) return null;
@@ -417,6 +508,14 @@ var DefaultDataStore = class {
417
508
  refresh: true
418
509
  });
419
510
  }
511
+ adoptResource(entry, resource) {
512
+ entry.resource ??= resource;
513
+ if (!entry.transferred || typeof resource.transfer !== "function" || resource.load === void 0 || !entryHasValue(entry)) return;
514
+ entry.transferred = false;
515
+ entry.seed = { value: entry.value };
516
+ entry.value = void 0;
517
+ entry.status = "pending";
518
+ }
420
519
  entryForKey(key) {
421
520
  const normalized = normalizeKey(key);
422
521
  return this.entries.get(normalized.canonical) ?? null;
@@ -430,6 +529,9 @@ var DefaultDataStore = class {
430
529
  invalidationVersion: 0,
431
530
  inactiveRetentionMs: void 0,
432
531
  inactiveTimer: null,
532
+ transferred: false,
533
+ seed: null,
534
+ parkedLoad: null,
433
535
  key: normalized.key,
434
536
  lane: null,
435
537
  pending: null,
@@ -576,7 +678,7 @@ var DefaultDataStore = class {
576
678
  publish: (value) => {
577
679
  if (!live()) return;
578
680
  const equals = source.resource.equals ?? Object.is;
579
- if (entryHasValue(entry) && equals(entry.value, value)) {
681
+ if (!(entry.transferred && typeof source.resource.transfer === "function") && entryHasValue(entry) && equals(entry.value, value)) {
580
682
  const pending = entry.pending;
581
683
  entry.invalidationVersion = 0;
582
684
  entry.lane = null;
@@ -585,7 +687,7 @@ var DefaultDataStore = class {
585
687
  pending?.controller.abort("superseded");
586
688
  return;
587
689
  }
588
- this.hydrateEntry(entry, entry.key, value);
690
+ this.hydrateEntry(entry, entry.key, value, false);
589
691
  this.scheduleWatchReconcile(entry);
590
692
  },
591
693
  fail: (error) => {
@@ -623,7 +725,7 @@ var DefaultDataStore = class {
623
725
  this.publish(entry);
624
726
  pending?.controller.abort(error);
625
727
  }
626
- hydrateEntry(entry, key, value) {
728
+ hydrateEntry(entry, key, value, transferred) {
627
729
  this.clearInactiveTimer(entry);
628
730
  const valueController = entry.valueController;
629
731
  const pending = entry.pending;
@@ -639,6 +741,8 @@ var DefaultDataStore = class {
639
741
  entry.stale = false;
640
742
  entry.status = "fulfilled";
641
743
  entry.value = value;
744
+ entry.transferred = transferred;
745
+ entry.seed = null;
642
746
  pending?.resolve(abortedRefreshResult(entry, "superseded"));
643
747
  this.publish(entry);
644
748
  pending?.controller.abort("superseded");
@@ -656,6 +760,7 @@ var DefaultDataStore = class {
656
760
  this.notifyEntryChange(entry);
657
761
  return Promise.resolve(unsupportedRefreshResult(entry));
658
762
  }
763
+ if (this.awaitedForms.has(entry.canonicalKey)) return this.parkLoad(entry, resource, args, options);
659
764
  const previousPending = entry.pending;
660
765
  const controller = new AbortController();
661
766
  const valueErrors = /* @__PURE__ */ new WeakSet();
@@ -669,10 +774,14 @@ var DefaultDataStore = class {
669
774
  this.notifyEntryChange(entry);
670
775
  if (controller.signal.aborted) return pending.promise;
671
776
  let loaded;
777
+ let missingCapability;
778
+ let threw = false;
672
779
  try {
673
780
  loaded = load(...args, this.createLoadContext(entry, controller, valueErrors));
674
781
  } catch (error) {
782
+ if (isMissingCapability(error)) missingCapability = error;
675
783
  loaded = Promise.reject(error);
784
+ threw = true;
676
785
  }
677
786
  const settleAborted = () => {
678
787
  const result = abortedRefreshResult(entry, "superseded");
@@ -708,6 +817,7 @@ var DefaultDataStore = class {
708
817
  entry.stale = entry.invalidationVersion !== invalidationVersion;
709
818
  entry.status = "fulfilled";
710
819
  entry.value = value;
820
+ entry.transferred = false;
711
821
  const result = {
712
822
  status: "fulfilled",
713
823
  value
@@ -747,6 +857,11 @@ var DefaultDataStore = class {
747
857
  controller.abort(error);
748
858
  return result;
749
859
  };
860
+ if (missingCapability !== void 0) {
861
+ loaded.catch(() => void 0);
862
+ reject(missingCapability);
863
+ return pending.promise;
864
+ }
750
865
  let thenable;
751
866
  try {
752
867
  thenable = isThenable(loaded);
@@ -754,15 +869,32 @@ var DefaultDataStore = class {
754
869
  reject(error);
755
870
  return pending.promise;
756
871
  }
757
- if (thenable) Promise.resolve(loaded).then(fulfill, reject);
758
- else fulfill(loaded);
872
+ if (thenable) {
873
+ if (!threw) pending.returnedThenable = true;
874
+ Promise.resolve(loaded).then(fulfill, reject);
875
+ } else fulfill(loaded);
759
876
  return pending.promise;
760
877
  }
878
+ parkLoad(entry, resource, args, options) {
879
+ if (entry.pending !== null) return entry.pending.promise;
880
+ const parked = createPendingLoad(new AbortController());
881
+ entry.pending = parked;
882
+ entry.status = "pending";
883
+ entry.parkedLoad = () => {
884
+ if (entry.pending !== parked) return;
885
+ entry.pending = null;
886
+ this.startLoad(entry, resource, args, options).then(parked.resolve);
887
+ };
888
+ this.notifyEntryChange(entry);
889
+ return parked.promise;
890
+ }
761
891
  createLoadContext(entry, controller, valueErrors) {
762
892
  const context = {
763
893
  data: this.dependentHandle(controller.signal),
764
- signal: controller.signal
894
+ signal: controller.signal,
895
+ transferred: entry.seed?.value
765
896
  };
897
+ entry.seed = null;
766
898
  defineLoadContextCapabilities(context, {
767
899
  key: entry.key,
768
900
  attributeError: (error) => {
@@ -860,7 +992,10 @@ var DefaultDataStore = class {
860
992
  }
861
993
  readCurrentValue(entry) {
862
994
  if (entryHasValue(entry)) return entry.value;
863
- if (entry.status === "rejected") throwDataResourceError(entry);
995
+ if (entry.status === "rejected") {
996
+ if (isMissingCapability(entry.error)) readBrowser(entry.error.reason);
997
+ throwDataResourceError(entry);
998
+ }
864
999
  if (entry.pending === null) throw new Error(`Data resource "${entry.canonicalKey}" has no pending load.`);
865
1000
  throw entry.pending.promise;
866
1001
  }
@@ -875,6 +1010,7 @@ var DefaultDataStore = class {
875
1010
  entry.valueController = null;
876
1011
  entry.valueErrors = /* @__PURE__ */ new WeakSet();
877
1012
  entry.value = void 0;
1013
+ entry.transferred = false;
878
1014
  entry.error = void 0;
879
1015
  entry.status = "pending";
880
1016
  } else if (entry.status === "rejected") {
@@ -906,6 +1042,18 @@ function canonicalKeyStartsWith(key, prefix) {
906
1042
  function isHeld(entry) {
907
1043
  return entry.subscribers.size > 0 || (entry.retainers?.size ?? 0) > 0;
908
1044
  }
1045
+ function transferValue(entry) {
1046
+ const transfer = entry.resource?.transfer;
1047
+ if (!entryHasValue(entry) || transfer === false) return void 0;
1048
+ if (entry.transferred || transfer === void 0) return { value: entry.value };
1049
+ const value = transfer(entry.value);
1050
+ return value === void 0 ? void 0 : { value };
1051
+ }
1052
+ function runParkedLoad(entry) {
1053
+ const run = entry.parkedLoad;
1054
+ entry.parkedLoad = null;
1055
+ run?.();
1056
+ }
909
1057
  function entryHasValue(entry) {
910
1058
  return entry.status === "fulfilled" || entry.status === "refreshing";
911
1059
  }
@@ -1066,6 +1214,6 @@ function setTransitionHandler(handler) {
1066
1214
  return previous;
1067
1215
  }
1068
1216
  //#endregion
1069
- export { resolveCurrentDataStore as _, createDataStore as a, createContext as b, dataResource as c, readDataStore as d, runWithDataStore as f, markDataResourceError as g, loadContextCapabilities as h, attachDataStore as i, isDataStoreController as l, defineLoadContextCapabilities as m, setTransitionHandler as n, createRendererDataStore as o, dataResourceKeysForError as p, transition as r, currentDataStore as s, runTransitionScope as t, normalizeDataResourceKey as u, setCurrentDataStore as v, isContext as x, FigContextSymbol as y };
1217
+ export { isContext as C, createContext as S, markDataResourceError as _, createDataStore as a, setCurrentDataStore as b, dataResource as c, readDataStore as d, runWithDataStore as f, loadContextCapabilities as g, defineLoadContextCapabilities as h, attachDataStore as i, isDataStoreController as l, dataResourceKeysForError as m, setTransitionHandler as n, createRendererDataStore as o, MissingCapabilityError as p, transition as r, currentDataStore as s, runTransitionScope as t, normalizeDataResourceKey as u, requireCapability as v, FigContextSymbol as x, resolveCurrentDataStore as y };
1070
1218
 
1071
- //# sourceMappingURL=transition-BFLGYE4q.js.map
1219
+ //# sourceMappingURL=transition-3xZO5ee7.js.map