@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-DbzJUf5a.js";
1
2
  import { y as isThenable$1 } from "./resource-BZOBllE_.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));
@@ -362,11 +452,13 @@ var DefaultDataStore = class {
362
452
  this.recordWatchSource(entry, resource, args);
363
453
  this.revalidateIfStale(entry, resource, args);
364
454
  if (entry.status === "pending" && entry.pending === null) {
455
+ const seeded = entry.seed !== null;
365
456
  if (this.waitsOnWatch(resource)) this.beginWatchPending(entry);
366
457
  else this.startLoad(entry, resource, args, {
367
458
  lane: this.host.getLane(),
368
459
  refresh: false
369
460
  });
461
+ if (seeded && loaderReturnedThenable(entry)) throw new Error(`Data resource "${entry.canonicalKey}" was read in render with a transferred value, but its load returned a promise for it. Return the value synchronously when \`transferred\` is set (a load declared \`async\` always returns a promise).`);
370
462
  }
371
463
  const value = this.readCurrentValue(entry);
372
464
  if (reads !== void 0 && reads.get(entry.canonicalKey) === void 0) reads.set(entry.canonicalKey, { value });
@@ -408,6 +500,7 @@ var DefaultDataStore = class {
408
500
  if (current !== void 0) {
409
501
  if (resource.inactiveRetentionMs !== void 0) current.inactiveRetentionMs = resource.inactiveRetentionMs;
410
502
  diagnoseEntryDrift(current, resource, normalized.canonical, fingerprint);
503
+ this.adoptResource(current, resource);
411
504
  return current;
412
505
  }
413
506
  if (!create) return null;
@@ -425,6 +518,14 @@ var DefaultDataStore = class {
425
518
  refresh: true
426
519
  });
427
520
  }
521
+ adoptResource(entry, resource) {
522
+ entry.resource ??= resource;
523
+ if (!entry.transferred || typeof resource.transfer !== "function" || resource.load === void 0 || !entryHasValue(entry)) return;
524
+ entry.transferred = false;
525
+ entry.seed = { value: entry.value };
526
+ entry.value = void 0;
527
+ entry.status = "pending";
528
+ }
428
529
  entryForKey(key) {
429
530
  const normalized = normalizeKey(key);
430
531
  return this.entries.get(normalized.canonical) ?? null;
@@ -438,6 +539,9 @@ var DefaultDataStore = class {
438
539
  invalidationVersion: 0,
439
540
  inactiveRetentionMs: void 0,
440
541
  inactiveTimer: null,
542
+ transferred: false,
543
+ seed: null,
544
+ parkedLoad: null,
441
545
  key: normalized.key,
442
546
  lane: null,
443
547
  pending: null,
@@ -584,7 +688,7 @@ var DefaultDataStore = class {
584
688
  publish: (value) => {
585
689
  if (!live()) return;
586
690
  const equals = source.resource.equals ?? Object.is;
587
- if (entryHasValue(entry) && equals(entry.value, value)) {
691
+ if (!(entry.transferred && typeof source.resource.transfer === "function") && entryHasValue(entry) && equals(entry.value, value)) {
588
692
  const pending = entry.pending;
589
693
  entry.invalidationVersion = 0;
590
694
  entry.lane = null;
@@ -593,7 +697,7 @@ var DefaultDataStore = class {
593
697
  pending?.controller.abort("superseded");
594
698
  return;
595
699
  }
596
- this.hydrateEntry(entry, entry.key, value);
700
+ this.hydrateEntry(entry, entry.key, value, false);
597
701
  this.scheduleWatchReconcile(entry);
598
702
  },
599
703
  fail: (error) => {
@@ -631,7 +735,7 @@ var DefaultDataStore = class {
631
735
  this.publish(entry);
632
736
  pending?.controller.abort(error);
633
737
  }
634
- hydrateEntry(entry, key, value) {
738
+ hydrateEntry(entry, key, value, transferred) {
635
739
  this.clearInactiveTimer(entry);
636
740
  const valueController = entry.valueController;
637
741
  const pending = entry.pending;
@@ -647,6 +751,8 @@ var DefaultDataStore = class {
647
751
  entry.stale = false;
648
752
  entry.status = "fulfilled";
649
753
  entry.value = value;
754
+ entry.transferred = transferred;
755
+ entry.seed = null;
650
756
  pending?.resolve(abortedRefreshResult(entry, "superseded"));
651
757
  this.publish(entry);
652
758
  pending?.controller.abort("superseded");
@@ -664,6 +770,7 @@ var DefaultDataStore = class {
664
770
  this.notifyEntryChange(entry);
665
771
  return Promise.resolve(unsupportedRefreshResult(entry));
666
772
  }
773
+ if (this.awaitedForms.has(entry.canonicalKey)) return this.parkLoad(entry, resource, args, options);
667
774
  const previousPending = entry.pending;
668
775
  const controller = new AbortController();
669
776
  const valueErrors = /* @__PURE__ */ new WeakSet();
@@ -677,10 +784,14 @@ var DefaultDataStore = class {
677
784
  this.notifyEntryChange(entry);
678
785
  if (controller.signal.aborted) return pending.promise;
679
786
  let loaded;
787
+ let missingCapability;
788
+ let threw = false;
680
789
  try {
681
790
  loaded = load(...args, this.createLoadContext(entry, controller, valueErrors));
682
791
  } catch (error) {
792
+ if (isMissingCapability(error)) missingCapability = error;
683
793
  loaded = Promise.reject(error);
794
+ threw = true;
684
795
  }
685
796
  const settleAborted = () => {
686
797
  const result = abortedRefreshResult(entry, "superseded");
@@ -716,6 +827,7 @@ var DefaultDataStore = class {
716
827
  entry.stale = entry.invalidationVersion !== invalidationVersion;
717
828
  entry.status = "fulfilled";
718
829
  entry.value = value;
830
+ entry.transferred = false;
719
831
  const result = {
720
832
  status: "fulfilled",
721
833
  value
@@ -755,6 +867,11 @@ var DefaultDataStore = class {
755
867
  controller.abort(error);
756
868
  return result;
757
869
  };
870
+ if (missingCapability !== void 0) {
871
+ loaded.catch(() => void 0);
872
+ reject(missingCapability);
873
+ return pending.promise;
874
+ }
758
875
  let thenable;
759
876
  try {
760
877
  thenable = isThenable(loaded);
@@ -762,15 +879,32 @@ var DefaultDataStore = class {
762
879
  reject(error);
763
880
  return pending.promise;
764
881
  }
765
- if (thenable) Promise.resolve(loaded).then(fulfill, reject);
766
- else fulfill(loaded);
882
+ if (thenable) {
883
+ if (!threw) pending.returnedThenable = true;
884
+ Promise.resolve(loaded).then(fulfill, reject);
885
+ } else fulfill(loaded);
767
886
  return pending.promise;
768
887
  }
888
+ parkLoad(entry, resource, args, options) {
889
+ if (entry.pending !== null) return entry.pending.promise;
890
+ const parked = createPendingLoad(new AbortController());
891
+ entry.pending = parked;
892
+ entry.status = "pending";
893
+ entry.parkedLoad = () => {
894
+ if (entry.pending !== parked) return;
895
+ entry.pending = null;
896
+ this.startLoad(entry, resource, args, options).then(parked.resolve);
897
+ };
898
+ this.notifyEntryChange(entry);
899
+ return parked.promise;
900
+ }
769
901
  createLoadContext(entry, controller, valueErrors) {
770
902
  const context = {
771
903
  data: this.dependentHandle(controller.signal),
772
- signal: controller.signal
904
+ signal: controller.signal,
905
+ transferred: entry.seed?.value
773
906
  };
907
+ entry.seed = null;
774
908
  defineLoadContextCapabilities(context, {
775
909
  key: entry.key,
776
910
  attributeError: (error) => {
@@ -869,7 +1003,10 @@ var DefaultDataStore = class {
869
1003
  }
870
1004
  readCurrentValue(entry) {
871
1005
  if (entryHasValue(entry)) return entry.value;
872
- if (entry.status === "rejected") throwDataResourceError(entry);
1006
+ if (entry.status === "rejected") {
1007
+ if (isMissingCapability(entry.error)) readBrowser(entry.error.reason);
1008
+ throwDataResourceError(entry);
1009
+ }
873
1010
  if (entry.pending === null) throw new Error(`Data resource "${entry.canonicalKey}" has no pending load.`);
874
1011
  throw entry.pending.promise;
875
1012
  }
@@ -884,6 +1021,7 @@ var DefaultDataStore = class {
884
1021
  entry.valueController = null;
885
1022
  entry.valueErrors = /* @__PURE__ */ new WeakSet();
886
1023
  entry.value = void 0;
1024
+ entry.transferred = false;
887
1025
  entry.error = void 0;
888
1026
  entry.status = "pending";
889
1027
  } else if (entry.status === "rejected") {
@@ -915,6 +1053,18 @@ function canonicalKeyStartsWith(key, prefix) {
915
1053
  function isHeld(entry) {
916
1054
  return entry.subscribers.size > 0 || (entry.retainers?.size ?? 0) > 0;
917
1055
  }
1056
+ function transferValue(entry) {
1057
+ const transfer = entry.resource?.transfer;
1058
+ if (!entryHasValue(entry) || transfer === false) return void 0;
1059
+ if (entry.transferred || transfer === void 0) return { value: entry.value };
1060
+ const value = transfer(entry.value);
1061
+ return value === void 0 ? void 0 : { value };
1062
+ }
1063
+ function runParkedLoad(entry) {
1064
+ const run = entry.parkedLoad;
1065
+ entry.parkedLoad = null;
1066
+ run?.();
1067
+ }
918
1068
  function entryHasValue(entry) {
919
1069
  return entry.status === "fulfilled" || entry.status === "refreshing";
920
1070
  }
@@ -946,8 +1096,7 @@ function fingerprintFor(resource, args) {
946
1096
  }
947
1097
  }
948
1098
  function diagnoseEntryDrift(entry, resource, key, fingerprint) {
949
- if (entry.resource === null) entry.resource = resource;
950
- else if (entry.resource !== resource) warn(`Data resource key ${key} was read by multiple resource definitions.`);
1099
+ if (entry.resource !== null && entry.resource !== resource) warn(`Data resource key ${key} was read by multiple resource definitions.`);
951
1100
  if (entry.fingerprint !== null && fingerprint !== null && entry.fingerprint !== fingerprint) warn(`Data resource key ${key} was read with different argument fingerprints.`);
952
1101
  }
953
1102
  function warn(message) {
@@ -1020,6 +1169,9 @@ function untilAborted(promise, signal) {
1020
1169
  });
1021
1170
  });
1022
1171
  }
1172
+ function loaderReturnedThenable(entry) {
1173
+ return entry.pending?.returnedThenable === true;
1174
+ }
1023
1175
  function createPendingLoad(controller) {
1024
1176
  let resolve = () => void 0;
1025
1177
  return {
@@ -1092,6 +1244,6 @@ function setTransitionHandler(handler) {
1092
1244
  return previous;
1093
1245
  }
1094
1246
  //#endregion
1095
- 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 };
1247
+ 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 };
1096
1248
 
1097
- //# sourceMappingURL=transition-CWjp9qRO.js.map
1249
+ //# sourceMappingURL=transition-CzmheLDz.js.map