@bgub/fig 2.0.0 → 2.2.0

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/README.md +21 -22
  3. package/dist/{element-B4LrR2ZB.d.ts → element-Ba2mDNM_.d.ts} +74 -17
  4. package/dist/{element-j7AKp0nu.js → element-DpXdLgOZ.js} +6 -68
  5. package/dist/element-DpXdLgOZ.js.map +1 -0
  6. package/dist/{hooks-QSWYd3Vl.d.ts → hooks-CUdLWnbQ.d.ts} +21 -23
  7. package/dist/index.d.ts +3 -3
  8. package/dist/index.js +4 -4
  9. package/dist/internal.d.ts +12 -9
  10. package/dist/internal.js +8 -4
  11. package/dist/internal.js.map +1 -1
  12. package/dist/jsx-runtime.d.ts +1 -1
  13. package/dist/jsx-runtime.js +1 -1
  14. package/dist/payload-format-B9UbNtxo.js.map +1 -1
  15. package/dist/payload.d.ts +1 -1
  16. package/dist/payload.js +2 -2
  17. package/{dist-development/resource-YO25HR7G.js → dist/resource-DEDmLSNk.js} +5 -18
  18. package/dist/resource-DEDmLSNk.js.map +1 -0
  19. package/dist/{transition-DJfG2ZSm.js → transition-C03gUimZ.js} +510 -83
  20. package/dist/transition-C03gUimZ.js.map +1 -0
  21. package/dist-development/{element-DzmNF67v.js → element-DbzJUf5a.js} +6 -68
  22. package/dist-development/element-DbzJUf5a.js.map +1 -0
  23. package/dist-development/index.js +4 -4
  24. package/dist-development/internal.js +8 -4
  25. package/dist-development/internal.js.map +1 -1
  26. package/dist-development/jsx-runtime.js +1 -1
  27. package/dist-development/payload-format-B9UbNtxo.js.map +1 -1
  28. package/dist-development/payload.js +2 -2
  29. package/{dist/resource-Lwjz_b4h.js → dist-development/resource-BZOBllE_.js} +5 -18
  30. package/dist-development/resource-BZOBllE_.js.map +1 -0
  31. package/dist-development/{transition-CrIF0Kz_.js → transition-BZTyeaMo.js} +513 -84
  32. package/dist-development/transition-BZTyeaMo.js.map +1 -0
  33. package/package.json +1 -1
  34. package/dist/element-j7AKp0nu.js.map +0 -1
  35. package/dist/resource-Lwjz_b4h.js.map +0 -1
  36. package/dist/transition-DJfG2ZSm.js.map +0 -1
  37. package/dist-development/element-DzmNF67v.js.map +0 -1
  38. package/dist-development/resource-YO25HR7G.js.map +0 -1
  39. package/dist-development/transition-CrIF0Kz_.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { $ as getCurrentDataStore, Q as defineLoadContextCapabilities, Z as dataResourceKeysForError, et as isAttributableError, it as setCurrentDataStore, nt as markDataResourceError, rt as resolveCurrentDataStore } from "./element-j7AKp0nu.js";
2
- import { y as isThenable$1 } from "./resource-Lwjz_b4h.js";
1
+ import { P as readBrowser } from "./element-DpXdLgOZ.js";
2
+ import { y as isThenable$1 } from "./resource-DEDmLSNk.js";
3
3
  //#region src/context.ts
4
4
  /** The Fig context symbol. */
5
5
  const FigContextSymbol = Symbol.for("fig.context");
@@ -15,6 +15,88 @@ function isContext(value) {
15
15
  return typeof value === "function" && "$$typeof" in value && value.$$typeof === FigContextSymbol;
16
16
  }
17
17
  //#endregion
18
+ //#region src/data.ts
19
+ const LoadContextCapabilitiesSymbol = Symbol.for("fig.data-load-context");
20
+ /** Define load context capabilities. */
21
+ function defineLoadContextCapabilities(context, capabilities) {
22
+ Object.defineProperty(context, LoadContextCapabilitiesSymbol, {
23
+ configurable: true,
24
+ enumerable: false,
25
+ value: capabilities
26
+ });
27
+ }
28
+ /** Load context capabilities. */
29
+ function loadContextCapabilities(context) {
30
+ return context[LoadContextCapabilitiesSymbol];
31
+ }
32
+ const objectDataErrors = /* @__PURE__ */ new WeakMap();
33
+ let currentDataStore$1 = null;
34
+ /** Captures an optional data store for synchronous scope re-entry. */
35
+ function getCurrentDataStore() {
36
+ return currentDataStore$1;
37
+ }
38
+ /** Resolves current data store. */
39
+ function resolveCurrentDataStore(message = "Data resource APIs require a Fig data store.") {
40
+ if (currentDataStore$1 === null) throw new Error(message);
41
+ return currentDataStore$1;
42
+ }
43
+ /** Sets current data store. */
44
+ function setCurrentDataStore(store) {
45
+ const previousStore = currentDataStore$1;
46
+ currentDataStore$1 = store;
47
+ return previousStore;
48
+ }
49
+ /** Marks data resource error. */
50
+ function markDataResourceError(error, key) {
51
+ if (!isAttributableError(error)) return;
52
+ let keys = objectDataErrors.get(error);
53
+ if (keys === void 0) {
54
+ keys = [];
55
+ objectDataErrors.set(error, keys);
56
+ }
57
+ if (keys.some((existing) => sameDataResourceKey(existing, key))) return;
58
+ keys.push(key);
59
+ }
60
+ /** Data resource keys for error. */
61
+ function dataResourceKeysForError(error) {
62
+ if (!isAttributableError(error)) return void 0;
63
+ const keys = objectDataErrors.get(error);
64
+ return keys === void 0 || keys.length === 0 ? void 0 : [...keys];
65
+ }
66
+ function sameDataResourceKey(a, b) {
67
+ return a.length === b.length && a.every((value, index) => Object.is(value, b[index]));
68
+ }
69
+ function isAttributableError(value) {
70
+ return (typeof value === "object" || typeof value === "function") && value !== null;
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
+ }
99
+ //#endregion
18
100
  //#region src/data-store.ts
19
101
  const DataResourceSymbol = Symbol.for("fig.data-resource");
20
102
  const DataStoreFactorySymbol = Symbol.for("fig.data-store-factory");
@@ -24,38 +106,19 @@ const DEFAULT_PRELOAD_RETENTION_MS = 3e4;
24
106
  const dataStoreFactory = createRendererDataStore;
25
107
  /** Data resource. */
26
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.");
27
110
  return {
28
111
  $$typeof: DataResourceSymbol,
29
112
  [DataStoreFactorySymbol]: dataStoreFactory,
30
113
  debugArgs: options.debugArgs,
114
+ equals: options.equals,
115
+ inactiveRetentionMs: options.inactiveRetentionMs,
31
116
  key: options.key,
32
- load: options.load
117
+ load: options.load,
118
+ transfer: options.transfer,
119
+ watch: options.watch
33
120
  };
34
121
  }
35
- /** Ensures data. */
36
- function ensureData(resource, ...args) {
37
- return resolveDataMutationStore("ensureData").ensureData(resource, ...args);
38
- }
39
- /** Invalidates data. */
40
- function invalidateData(resource, ...args) {
41
- resolveDataMutationStore("invalidateData").invalidateData(resource, ...args);
42
- }
43
- /** Invalidates data error. */
44
- function invalidateDataError(error) {
45
- return resolveDataMutationStore("invalidateDataError").invalidateDataError(error);
46
- }
47
- /** Invalidates data key. */
48
- function invalidateDataKey(key) {
49
- resolveDataMutationStore("invalidateDataKey").invalidateDataKey(key);
50
- }
51
- /** Invalidates data prefix. */
52
- function invalidateDataPrefix(prefix) {
53
- resolveDataMutationStore("invalidateDataPrefix").invalidateDataPrefix(prefix);
54
- }
55
- /** Refreshes data. */
56
- function refreshData(resource, ...args) {
57
- return resolveDataMutationStore("refreshData").refreshData(resource, ...args);
58
- }
59
122
  /** Reads data store. */
60
123
  function readDataStore() {
61
124
  return resolveCurrentDataStore("readDataStore() must be called synchronously while Fig is executing — during render, an event handler, an action, or an effect. Capture the handle there and use it after awaits.");
@@ -65,8 +128,10 @@ function createDataStore(options = {}) {
65
128
  const state = { host: null };
66
129
  const store = createRendererDataStore({
67
130
  getLane: () => state.host === null ? null : state.host.getLane(),
68
- partition: options.partition,
69
- schedule: (owner, lane) => state.host?.schedule(owner, lane)
131
+ schedule: (owner, lane) => state.host?.schedule(owner, lane),
132
+ get watchSources() {
133
+ return state.host?.watchSources === true;
134
+ }
70
135
  });
71
136
  Object.defineProperty(store, DataStoreControllerSymbol, { value: state });
72
137
  if (options.initialData !== void 0) store.hydrate(options.initialData);
@@ -78,16 +143,13 @@ function isDataStoreController(value) {
78
143
  }
79
144
  /** Attach data store. */
80
145
  function attachDataStore(controller, host, initialData) {
81
- if (host.partition !== void 0 || initialData !== void 0) throw new Error("Pass partition and initialData to createDataStore(), not the renderer, when adopting a data store.");
146
+ if (initialData !== void 0) throw new Error("Pass initialData to createDataStore(), not the renderer, when adopting a data store.");
82
147
  const state = controller[DataStoreControllerSymbol];
83
148
  if (state === void 0) throw new Error("dataStore must be created with createDataStore().");
84
149
  if (state.host !== null) throw new Error("A data store can only be adopted by one Fig renderer.");
85
150
  state.host = host;
86
151
  return controller;
87
152
  }
88
- function resolveDataMutationStore(name) {
89
- return resolveCurrentDataStore(`${name}() must be called synchronously while Fig is executing — during render, an event handler, an action, or an effect. Capture readDataStore() (or root.data) synchronously and call the handle instead.`);
90
- }
91
153
  /** Creates renderer data store. */
92
154
  function createRendererDataStore(host) {
93
155
  return new DefaultDataStore(host);
@@ -99,15 +161,15 @@ function normalizeDataResourceKey(key) {
99
161
  var DefaultDataStore = class {
100
162
  host;
101
163
  entries = /* @__PURE__ */ new Map();
164
+ awaitedForms = /* @__PURE__ */ new Set();
102
165
  inactiveRetentionMs;
103
166
  ownerKeys = /* @__PURE__ */ new WeakMap();
104
- partitionKey;
105
167
  preloadRetentionMs;
106
168
  disposed = false;
169
+ pendingWatchReconciles = null;
107
170
  constructor(host) {
108
171
  this.host = host;
109
172
  this.inactiveRetentionMs = host.inactiveRetentionMs ?? DEFAULT_INACTIVE_RETENTION_MS;
110
- this.partitionKey = host.partition === void 0 ? "" : encodeValue(host.partition, createEncodePath("partition"));
111
173
  this.preloadRetentionMs = host.preloadRetentionMs ?? DEFAULT_PRELOAD_RETENTION_MS;
112
174
  }
113
175
  areDataDependenciesConsistent(reads) {
@@ -135,11 +197,16 @@ var DefaultDataStore = class {
135
197
  if (entry !== void 0) {
136
198
  this.clearInactiveTimer(entry);
137
199
  entry.subscribers.add(owner);
200
+ if (reads !== void 0) entry.retainers?.delete(reads);
138
201
  this.notifyEntryChange(entry);
139
202
  }
140
203
  }
141
204
  }
142
205
  if (orphanCandidates !== null) for (const entry of orphanCandidates) this.abortOrphanedLoad(entry);
206
+ if (nextKeys !== null) for (const key of nextKeys) {
207
+ const entry = this.entries.get(key);
208
+ if (entry !== void 0) this.scheduleWatchReconcile(entry);
209
+ }
143
210
  const consistent = reads === void 0 || this.areDataDependenciesConsistent(reads);
144
211
  reads?.clear();
145
212
  return consistent;
@@ -158,7 +225,10 @@ var DefaultDataStore = class {
158
225
  if (entry === void 0) continue;
159
226
  entry.subscribers.delete(owner);
160
227
  this.notifyEntryChange(entry);
161
- if (retainedKeys?.has(key) !== true) this.scheduleInactiveCleanup(entry);
228
+ if (retainedKeys?.has(key) !== true) {
229
+ this.scheduleInactiveCleanup(entry);
230
+ this.scheduleWatchReconcile(entry);
231
+ }
162
232
  }
163
233
  }
164
234
  collectSubscribedEntries(keys, into) {
@@ -173,41 +243,104 @@ var DefaultDataStore = class {
173
243
  return into;
174
244
  }
175
245
  abortOrphanedLoad(entry) {
176
- if (this.entries.get(entry.storeKey) !== entry) return;
177
- if (entry.subscribers.size > 0 || entry.preloadTimer !== null || entry.ensureRetainers > 0 || entry.pending === null || entryHasValue(entry)) return;
246
+ if (this.entries.get(entry.canonicalKey) !== entry) return;
247
+ if (isHeld(entry) || entry.preloadTimer !== null || entry.pending === null || entryHasValue(entry)) return;
178
248
  this.evictEntry(entry, "evicted");
179
249
  }
180
250
  dispose() {
181
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
+ }
182
262
  for (const entry of this.entries.values()) {
183
263
  this.clearInactiveTimer(entry);
184
264
  this.clearPreloadTimer(entry);
185
265
  this.abortEntryGenerations(entry, "store-disposed");
186
266
  this.notifyEntryChange(entry);
187
267
  }
268
+ if (failure !== void 0) throw failure.error;
188
269
  }
189
270
  hydrate(entries) {
271
+ this.hydrateEntries(entries, true);
272
+ }
273
+ hydrateMissing(entries) {
274
+ this.hydrateEntries(entries, false);
275
+ }
276
+ hydrateEntries(entries, replaceValues) {
190
277
  if (this.disposed) return;
191
278
  for (const hydrated of entries) {
192
279
  if (this.disposed) return;
193
280
  const normalized = normalizeKey(hydrated.key);
194
- const storeKey = this.storeKey(normalized.canonical);
281
+ const storeKey = normalized.canonical;
195
282
  const current = this.entries.get(storeKey);
283
+ this.awaitedForms.delete(storeKey);
196
284
  if (current !== void 0) {
197
- 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);
198
291
  continue;
199
292
  }
200
- const entry = this.createEntry(normalized, storeKey, null, null, "fulfilled", hydrated.value);
293
+ const entry = this.createEntry(normalized, null, null, "fulfilled", hydrated.value);
294
+ entry.transferred = true;
201
295
  this.entries.set(storeKey, entry);
202
296
  this.notifyEntryChange(entry);
203
297
  }
204
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
+ }
205
316
  snapshot() {
317
+ return this.snapshotKeys(this.entries.keys());
318
+ }
319
+ snapshotKeys(canonicalKeys, deferred) {
320
+ const sources = /* @__PURE__ */ new Map();
321
+ for (const entry of this.entries.values()) for (const retainer of entry.retainers ?? []) {
322
+ const retained = sources.get(retainer);
323
+ if (retained === void 0) sources.set(retainer, [entry]);
324
+ else retained.push(entry);
325
+ }
206
326
  const entries = [];
207
- for (const entry of this.entries.values()) if (entryHasValue(entry)) entries.push({
208
- key: entry.key,
209
- value: entry.value
210
- });
327
+ const visited = /* @__PURE__ */ new Set();
328
+ const requested = new Set(canonicalKeys);
329
+ const visit = (entry) => {
330
+ if (entry === void 0 || visited.has(entry)) return;
331
+ visited.add(entry);
332
+ const signal = entry.valueController?.signal;
333
+ if (signal !== void 0) sources.get(signal)?.forEach(visit);
334
+ const isRequested = requested.has(entry.canonicalKey);
335
+ if (!isRequested && typeof entry.resource?.transfer !== "function") return;
336
+ const transferred = transferValue(entry);
337
+ if (transferred === void 0) return;
338
+ (isRequested ? entries : deferred ?? entries).push({
339
+ key: entry.key,
340
+ value: transferred.value
341
+ });
342
+ };
343
+ for (const key of requested) visit(this.entries.get(key));
211
344
  return entries;
212
345
  }
213
346
  inspectDataEntries() {
@@ -247,8 +380,12 @@ var DefaultDataStore = class {
247
380
  for (const entry of this.entries.values()) if (canonicalKeyStartsWith(entry.canonicalKey, prefixCanonical)) entries.push(entry);
248
381
  this.invalidateEntries(entries);
249
382
  }
250
- async ensureData(resource, ...args) {
383
+ ensureData(resource, ...args) {
384
+ return this.ensureDataUntil(resource, args, void 0);
385
+ }
386
+ async ensureDataUntil(resource, args, until) {
251
387
  for (;;) {
388
+ if (until?.aborted === true) throw until.reason;
252
389
  if (this.disposed) throw new Error("ensureData() requires a live data store; this store was disposed.");
253
390
  const entry = this.entryFor(resource, args, true);
254
391
  this.clearInactiveTimer(entry);
@@ -258,16 +395,24 @@ var DefaultDataStore = class {
258
395
  return entry.value;
259
396
  }
260
397
  if (entry.status === "rejected") throwDataResourceError(entry);
261
- entry.ensureRetainers += 1;
398
+ const retainer = {};
399
+ (entry.retainers ??= /* @__PURE__ */ new Set()).add(retainer);
262
400
  try {
263
- if (entry.pending !== null) await entry.pending.promise;
264
- else await this.startLoad(entry, resource, args, {
401
+ this.recordWatchSource(entry, resource, args);
402
+ let settled;
403
+ if (entry.pending !== null) settled = entry.pending.promise;
404
+ else if (this.waitsOnWatch(resource)) settled = this.beginWatchPending(entry).promise;
405
+ else settled = this.startLoad(entry, resource, args, {
265
406
  lane: this.host.getLane(),
266
407
  refresh: false
267
408
  });
409
+ await (until === void 0 ? settled : untilAborted(settled, until));
268
410
  } finally {
269
- entry.ensureRetainers -= 1;
270
- if (this.entries.get(entry.storeKey) === entry) this.retainPreload(entry);
411
+ entry.retainers?.delete(retainer);
412
+ if (this.entries.get(entry.canonicalKey) === entry) {
413
+ this.retainPreload(entry);
414
+ this.scheduleWatchReconcile(entry);
415
+ }
271
416
  }
272
417
  }
273
418
  }
@@ -287,14 +432,19 @@ var DefaultDataStore = class {
287
432
  const entry = this.entryFor(resource, args, true);
288
433
  this.clearInactiveTimer(entry);
289
434
  this.clearPreloadTimer(entry);
290
- if (reads !== void 0 && !reads.has(entry.storeKey)) reads.set(entry.storeKey, void 0);
435
+ if (reads !== void 0) (entry.retainers ??= /* @__PURE__ */ new Set()).add(reads);
436
+ if (reads !== void 0 && !reads.has(entry.canonicalKey)) reads.set(entry.canonicalKey, void 0);
437
+ this.recordWatchSource(entry, resource, args);
291
438
  this.revalidateIfStale(entry, resource, args);
292
- if (entry.status === "pending" && entry.pending === null) this.startLoad(entry, resource, args, {
293
- lane: this.host.getLane(),
294
- refresh: false
295
- });
439
+ if (entry.status === "pending" && entry.pending === null) {
440
+ if (this.waitsOnWatch(resource)) this.beginWatchPending(entry);
441
+ else this.startLoad(entry, resource, args, {
442
+ lane: this.host.getLane(),
443
+ refresh: false
444
+ });
445
+ }
296
446
  const value = this.readCurrentValue(entry);
297
- if (reads !== void 0 && reads.get(entry.storeKey) === void 0) reads.set(entry.storeKey, { value });
447
+ if (reads !== void 0 && reads.get(entry.canonicalKey) === void 0) reads.set(entry.canonicalKey, { value });
298
448
  return value;
299
449
  }
300
450
  refreshData(resource, ...args) {
@@ -328,11 +478,16 @@ var DefaultDataStore = class {
328
478
  entryFor(resource, args, create) {
329
479
  const normalized = normalizeKey(resource.key(...args));
330
480
  const fingerprint = null;
331
- const key = this.storeKey(normalized.canonical);
481
+ const key = normalized.canonical;
332
482
  const current = this.entries.get(key);
333
- if (current !== void 0) return current;
483
+ if (current !== void 0) {
484
+ if (resource.inactiveRetentionMs !== void 0) current.inactiveRetentionMs = resource.inactiveRetentionMs;
485
+ this.adoptResource(current, resource);
486
+ return current;
487
+ }
334
488
  if (!create) return null;
335
- const entry = this.createEntry(normalized, key, resource, fingerprint, "pending", void 0);
489
+ const entry = this.createEntry(normalized, resource, fingerprint, "pending", void 0);
490
+ entry.inactiveRetentionMs = resource.inactiveRetentionMs;
336
491
  if (this.disposed) return entry;
337
492
  this.entries.set(key, entry);
338
493
  this.notifyEntryChange(entry);
@@ -345,18 +500,30 @@ var DefaultDataStore = class {
345
500
  refresh: true
346
501
  });
347
502
  }
503
+ adoptResource(entry, resource) {
504
+ entry.resource ??= resource;
505
+ if (!entry.transferred || typeof resource.transfer !== "function" || resource.load === void 0 || !entryHasValue(entry)) return;
506
+ entry.transferred = false;
507
+ entry.seed = { value: entry.value };
508
+ entry.value = void 0;
509
+ entry.status = "pending";
510
+ }
348
511
  entryForKey(key) {
349
512
  const normalized = normalizeKey(key);
350
- return this.entries.get(this.storeKey(normalized.canonical)) ?? null;
513
+ return this.entries.get(normalized.canonical) ?? null;
351
514
  }
352
- createEntry(normalized, storeKey, resource, fingerprint, status, value) {
515
+ createEntry(normalized, resource, fingerprint, status, value) {
353
516
  return {
354
517
  canonicalKey: normalized.canonical,
355
- ensureRetainers: 0,
518
+ retainers: null,
356
519
  error: void 0,
357
520
  fingerprint,
358
521
  invalidationVersion: 0,
522
+ inactiveRetentionMs: void 0,
359
523
  inactiveTimer: null,
524
+ transferred: false,
525
+ seed: null,
526
+ parkedLoad: null,
360
527
  key: normalized.key,
361
528
  lane: null,
362
529
  pending: null,
@@ -366,14 +533,191 @@ var DefaultDataStore = class {
366
533
  resource,
367
534
  stale: false,
368
535
  status,
369
- storeKey,
370
536
  subscribers: /* @__PURE__ */ new Set(),
371
537
  value,
372
538
  valueController: null,
373
- valueErrors: /* @__PURE__ */ new WeakSet()
539
+ valueErrors: /* @__PURE__ */ new WeakSet(),
540
+ watchController: null,
541
+ watchSource: null
542
+ };
543
+ }
544
+ recordWatchSource(entry, resource, args) {
545
+ if (resource.watch === void 0 || entry.watchSource !== null) return;
546
+ entry.watchSource = {
547
+ args,
548
+ resource
549
+ };
550
+ }
551
+ waitsOnWatch(resource) {
552
+ return resource.load === void 0 && resource.watch !== void 0 && this.host.watchSources === true;
553
+ }
554
+ beginWatchPending(entry) {
555
+ if (entry.pending !== null) return entry.pending;
556
+ const pending = createPendingLoad(new AbortController());
557
+ pending.watch = true;
558
+ entry.pending = pending;
559
+ entry.status = "pending";
560
+ this.notifyEntryChange(entry);
561
+ this.scheduleWatchReconcile(entry);
562
+ return pending;
563
+ }
564
+ dependentHandle(signal) {
565
+ const retain = (entry) => {
566
+ if (signal.aborted || entry.retainers?.has(signal)) return;
567
+ (entry.retainers ??= /* @__PURE__ */ new Set()).add(signal);
568
+ this.clearInactiveTimer(entry);
569
+ signal.addEventListener("abort", () => this.releaseRetainer(entry, signal), { once: true });
570
+ };
571
+ return {
572
+ ensureData: (resource, ...args) => {
573
+ if (signal.aborted) return Promise.reject(signal.reason);
574
+ if (!this.disposed) {
575
+ const entry = this.entryFor(resource, args, true);
576
+ this.recordWatchSource(entry, resource, args);
577
+ retain(entry);
578
+ this.scheduleWatchReconcile(entry);
579
+ }
580
+ return this.ensureDataUntil(resource, args, signal);
581
+ },
582
+ hydrate: (entries) => this.hydrate(entries),
583
+ invalidateData: (resource, ...args) => this.invalidateData(resource, ...args),
584
+ invalidateDataError: (error) => this.invalidateDataError(error),
585
+ invalidateDataKey: (key) => this.invalidateDataKey(key),
586
+ invalidateDataPrefix: (prefix) => this.invalidateDataPrefix(prefix),
587
+ preloadData: (resource, ...args) => this.preloadData(resource, ...args),
588
+ refreshData: (resource, ...args) => this.refreshData(resource, ...args),
589
+ run: (callback) => this.run(callback)
374
590
  };
375
591
  }
376
- hydrateEntry(entry, key, value) {
592
+ keepEqualValue(entry, stale) {
593
+ entry.error = void 0;
594
+ entry.pending = null;
595
+ entry.refreshError = void 0;
596
+ entry.refreshFailed = false;
597
+ entry.stale = stale;
598
+ entry.status = "fulfilled";
599
+ this.notifyEntryChange(entry);
600
+ this.scheduleInactiveCleanup(entry);
601
+ }
602
+ scheduleWatchReconcile(entry) {
603
+ if (entry.watchSource === null && entry.watchController === null) return;
604
+ if (this.pendingWatchReconciles !== null) {
605
+ this.pendingWatchReconciles.add(entry);
606
+ return;
607
+ }
608
+ const entries = /* @__PURE__ */ new Set([entry]);
609
+ this.pendingWatchReconciles = entries;
610
+ queueMicrotask(() => {
611
+ this.pendingWatchReconciles = null;
612
+ for (const pending of entries) this.reconcileWatch(pending);
613
+ });
614
+ }
615
+ wantsWatch(entry) {
616
+ const source = entry.watchSource;
617
+ if (source === null || this.disposed || this.host.watchSources !== true || this.entries.get(entry.canonicalKey) !== entry) return false;
618
+ if (entry.subscribers.size > 0) return true;
619
+ return source.resource.load === void 0 && isHeld(entry);
620
+ }
621
+ reconcileWatch(entry) {
622
+ const wanted = this.wantsWatch(entry);
623
+ if (wanted && entry.watchController === null) this.startWatch(entry);
624
+ else if (!wanted && entry.watchController !== null) this.stopWatch(entry, "released");
625
+ if (!wanted && entry.pending?.watch === true && !isHeld(entry)) this.evictEntry(entry, "evicted");
626
+ }
627
+ holdDataRead(reads, canonicalKey) {
628
+ if (!reads.has(canonicalKey)) reads.set(canonicalKey, void 0);
629
+ const entry = this.entries.get(canonicalKey);
630
+ if (entry !== void 0) (entry.retainers ??= /* @__PURE__ */ new Set()).add(reads);
631
+ }
632
+ releaseDataReads(reads) {
633
+ for (const key of reads.keys()) {
634
+ const entry = this.entries.get(key);
635
+ if (entry !== void 0) this.releaseRetainer(entry, reads);
636
+ }
637
+ }
638
+ releaseRetainer(entry, owner) {
639
+ if (!entry.retainers?.delete(owner) || this.entries.get(entry.canonicalKey) !== entry) return;
640
+ this.scheduleInactiveCleanup(entry);
641
+ this.scheduleWatchReconcile(entry);
642
+ }
643
+ startWatch(entry) {
644
+ const source = entry.watchSource;
645
+ const watch = source?.resource.watch;
646
+ if (source === null || watch === void 0) return;
647
+ const controller = new AbortController();
648
+ entry.watchController = controller;
649
+ this.notifyEntryChange(entry);
650
+ const context = this.createWatchContext(entry, source, controller);
651
+ try {
652
+ watch(...source.args, context);
653
+ } catch (error) {
654
+ context.fail(error);
655
+ }
656
+ }
657
+ stopWatch(entry, reason) {
658
+ const controller = entry.watchController;
659
+ if (controller === null) return;
660
+ entry.watchController = null;
661
+ if (reason === "released" && entryHasValue(entry)) entry.stale = true;
662
+ this.notifyEntryChange(entry);
663
+ controller.abort(reason);
664
+ }
665
+ createWatchContext(entry, source, controller) {
666
+ const live = () => !this.disposed && !controller.signal.aborted && this.entries.get(entry.canonicalKey) === entry;
667
+ return {
668
+ data: this.dependentHandle(controller.signal),
669
+ signal: controller.signal,
670
+ publish: (value) => {
671
+ if (!live()) return;
672
+ const equals = source.resource.equals ?? Object.is;
673
+ if (!(entry.transferred && typeof source.resource.transfer === "function") && entryHasValue(entry) && equals(entry.value, value)) {
674
+ const pending = entry.pending;
675
+ entry.invalidationVersion = 0;
676
+ entry.lane = null;
677
+ this.keepEqualValue(entry, false);
678
+ pending?.resolve(abortedRefreshResult(entry, "superseded"));
679
+ pending?.controller.abort("superseded");
680
+ return;
681
+ }
682
+ this.hydrateEntry(entry, entry.key, value, false);
683
+ this.scheduleWatchReconcile(entry);
684
+ },
685
+ fail: (error) => {
686
+ if (!live()) return;
687
+ this.failWatchedEntry(entry, error);
688
+ },
689
+ refresh: () => {
690
+ if (live()) return this.refreshData(source.resource, ...source.args);
691
+ const reason = this.disposed ? "store-disposed" : controller.signal.reason === "released" ? "released" : "evicted";
692
+ return Promise.resolve(abortedRefreshResult(entry, reason));
693
+ },
694
+ invalidate: () => {
695
+ if (!live()) return;
696
+ this.invalidateEntry(entry, this.host.getLane());
697
+ }
698
+ };
699
+ }
700
+ failWatchedEntry(entry, error) {
701
+ if (entryHasValue(entry)) {
702
+ entry.refreshError = error;
703
+ entry.refreshFailed = true;
704
+ entry.stale = true;
705
+ this.publish(entry);
706
+ return;
707
+ }
708
+ const pending = entry.pending;
709
+ entry.pending = null;
710
+ entry.error = error;
711
+ entry.status = "rejected";
712
+ markDataResourceError(error, entry.key);
713
+ pending?.resolve({
714
+ error,
715
+ status: "rejected"
716
+ });
717
+ this.publish(entry);
718
+ pending?.controller.abort(error);
719
+ }
720
+ hydrateEntry(entry, key, value, transferred) {
377
721
  this.clearInactiveTimer(entry);
378
722
  const valueController = entry.valueController;
379
723
  const pending = entry.pending;
@@ -389,14 +733,13 @@ var DefaultDataStore = class {
389
733
  entry.stale = false;
390
734
  entry.status = "fulfilled";
391
735
  entry.value = value;
736
+ entry.transferred = transferred;
737
+ entry.seed = null;
392
738
  pending?.resolve(abortedRefreshResult(entry, "superseded"));
393
739
  this.publish(entry);
394
740
  pending?.controller.abort("superseded");
395
741
  valueController?.abort("superseded");
396
742
  }
397
- storeKey(canonicalKey) {
398
- return this.partitionKey === "" ? canonicalKey : `${this.partitionKey}:${canonicalKey}`;
399
- }
400
743
  startLoad(entry, resource, args, options) {
401
744
  const hadValue = entryHasValue(entry);
402
745
  const load = resource.load;
@@ -409,6 +752,7 @@ var DefaultDataStore = class {
409
752
  this.notifyEntryChange(entry);
410
753
  return Promise.resolve(unsupportedRefreshResult(entry));
411
754
  }
755
+ if (this.awaitedForms.has(entry.canonicalKey)) return this.parkLoad(entry, resource, args, options);
412
756
  const previousPending = entry.pending;
413
757
  const controller = new AbortController();
414
758
  const valueErrors = /* @__PURE__ */ new WeakSet();
@@ -422,10 +766,14 @@ var DefaultDataStore = class {
422
766
  this.notifyEntryChange(entry);
423
767
  if (controller.signal.aborted) return pending.promise;
424
768
  let loaded;
769
+ let missingCapability;
770
+ let threw = false;
425
771
  try {
426
772
  loaded = load(...args, this.createLoadContext(entry, controller, valueErrors));
427
773
  } catch (error) {
774
+ if (isMissingCapability(error)) missingCapability = error;
428
775
  loaded = Promise.reject(error);
776
+ threw = true;
429
777
  }
430
778
  const settleAborted = () => {
431
779
  const result = abortedRefreshResult(entry, "superseded");
@@ -434,6 +782,23 @@ var DefaultDataStore = class {
434
782
  };
435
783
  const fulfill = (value) => {
436
784
  if (entry.pending !== pending || controller.signal.aborted) return settleAborted();
785
+ const equals = resource.equals ?? Object.is;
786
+ let equal;
787
+ try {
788
+ equal = entryHasValue(entry) && equals(entry.value, value);
789
+ } catch (error) {
790
+ return reject(error);
791
+ }
792
+ if (equal) {
793
+ this.keepEqualValue(entry, entry.invalidationVersion !== invalidationVersion);
794
+ const result = {
795
+ status: "fulfilled",
796
+ value: entry.value
797
+ };
798
+ pending.resolve(result);
799
+ controller.abort("superseded");
800
+ return result;
801
+ }
437
802
  const superseded = entry.valueController;
438
803
  entry.valueController = controller;
439
804
  entry.valueErrors = valueErrors;
@@ -444,6 +809,7 @@ var DefaultDataStore = class {
444
809
  entry.stale = entry.invalidationVersion !== invalidationVersion;
445
810
  entry.status = "fulfilled";
446
811
  entry.value = value;
812
+ entry.transferred = false;
447
813
  const result = {
448
814
  status: "fulfilled",
449
815
  value
@@ -483,6 +849,11 @@ var DefaultDataStore = class {
483
849
  controller.abort(error);
484
850
  return result;
485
851
  };
852
+ if (missingCapability !== void 0) {
853
+ loaded.catch(() => void 0);
854
+ reject(missingCapability);
855
+ return pending.promise;
856
+ }
486
857
  let thenable;
487
858
  try {
488
859
  thenable = isThenable(loaded);
@@ -490,12 +861,32 @@ var DefaultDataStore = class {
490
861
  reject(error);
491
862
  return pending.promise;
492
863
  }
493
- if (thenable) Promise.resolve(loaded).then(fulfill, reject);
494
- else fulfill(loaded);
864
+ if (thenable) {
865
+ if (!threw) pending.returnedThenable = true;
866
+ Promise.resolve(loaded).then(fulfill, reject);
867
+ } else fulfill(loaded);
495
868
  return pending.promise;
496
869
  }
870
+ parkLoad(entry, resource, args, options) {
871
+ if (entry.pending !== null) return entry.pending.promise;
872
+ const parked = createPendingLoad(new AbortController());
873
+ entry.pending = parked;
874
+ entry.status = "pending";
875
+ entry.parkedLoad = () => {
876
+ if (entry.pending !== parked) return;
877
+ entry.pending = null;
878
+ this.startLoad(entry, resource, args, options).then(parked.resolve);
879
+ };
880
+ this.notifyEntryChange(entry);
881
+ return parked.promise;
882
+ }
497
883
  createLoadContext(entry, controller, valueErrors) {
498
- const context = { signal: controller.signal };
884
+ const context = {
885
+ data: this.dependentHandle(controller.signal),
886
+ signal: controller.signal,
887
+ transferred: entry.seed?.value
888
+ };
889
+ entry.seed = null;
499
890
  defineLoadContextCapabilities(context, {
500
891
  key: entry.key,
501
892
  attributeError: (error) => {
@@ -505,7 +896,7 @@ var DefaultDataStore = class {
505
896
  },
506
897
  hydrate: (entries) => {
507
898
  if (this.disposed || controller.signal.aborted) return;
508
- const foreign = entries.filter((hydrated) => this.storeKey(normalizeKey(hydrated.key).canonical) !== entry.storeKey);
899
+ const foreign = entries.filter((hydrated) => normalizeKey(hydrated.key).canonical !== entry.canonicalKey);
509
900
  for (const hydrated of foreign) {
510
901
  if (controller.signal.aborted) return;
511
902
  this.hydrate([hydrated]);
@@ -530,13 +921,14 @@ var DefaultDataStore = class {
530
921
  pending?.resolve(abortedRefreshResult(entry, reason));
531
922
  pending?.controller.abort(reason);
532
923
  valueController?.abort(reason);
924
+ this.stopWatch(entry, reason);
533
925
  }
534
926
  retainPreload(entry) {
535
927
  this.clearPreloadTimer(entry);
536
928
  if (this.disposed || !Number.isFinite(this.preloadRetentionMs)) return;
537
929
  entry.preloadTimer = scheduleStoreTimer(() => {
538
930
  entry.preloadTimer = null;
539
- if (entry.subscribers.size === 0 && entry.ensureRetainers === 0 && entry.pending !== null && !entryHasValue(entry)) {
931
+ if (!isHeld(entry) && entry.pending !== null && !entryHasValue(entry)) {
540
932
  this.evictEntry(entry, "evicted");
541
933
  return;
542
934
  }
@@ -544,19 +936,20 @@ var DefaultDataStore = class {
544
936
  }, Math.max(0, this.preloadRetentionMs));
545
937
  }
546
938
  scheduleInactiveCleanup(entry) {
547
- if (this.disposed || entry.subscribers.size > 0 || entry.pending !== null || entry.preloadTimer !== null || entry.ensureRetainers > 0 || !Number.isFinite(this.inactiveRetentionMs)) return;
939
+ const retention = entry.inactiveRetentionMs ?? this.inactiveRetentionMs;
940
+ if (this.disposed || isHeld(entry) || entry.pending !== null || entry.preloadTimer !== null || !Number.isFinite(retention)) return;
548
941
  this.clearInactiveTimer(entry);
549
942
  entry.inactiveTimer = scheduleStoreTimer(() => {
550
943
  entry.inactiveTimer = null;
551
- if (entry.subscribers.size > 0 || entry.pending !== null || entry.preloadTimer !== null || entry.ensureRetainers > 0) return;
944
+ if (isHeld(entry) || entry.pending !== null || entry.preloadTimer !== null) return;
552
945
  this.evictEntry(entry, "evicted");
553
- }, Math.max(0, this.inactiveRetentionMs));
946
+ }, Math.max(0, retention));
554
947
  }
555
948
  evictEntry(entry, reason) {
556
- if (this.entries.get(entry.storeKey) !== entry) return;
949
+ if (this.entries.get(entry.canonicalKey) !== entry) return;
557
950
  this.clearInactiveTimer(entry);
558
951
  this.clearPreloadTimer(entry);
559
- this.entries.delete(entry.storeKey);
952
+ this.entries.delete(entry.canonicalKey);
560
953
  this.abortEntryGenerations(entry, reason);
561
954
  this.host.onEntryEvict?.(this.snapshotEntry(entry));
562
955
  }
@@ -585,12 +978,16 @@ var DefaultDataStore = class {
585
978
  stale: entry.stale,
586
979
  status: entry.status,
587
980
  subscriberCount: entry.subscribers.size,
588
- value: hasValue ? entry.value : void 0
981
+ value: hasValue ? entry.value : void 0,
982
+ ...entry.watchController === null ? {} : { watching: true }
589
983
  };
590
984
  }
591
985
  readCurrentValue(entry) {
592
986
  if (entryHasValue(entry)) return entry.value;
593
- if (entry.status === "rejected") throwDataResourceError(entry);
987
+ if (entry.status === "rejected") {
988
+ if (isMissingCapability(entry.error)) readBrowser(entry.error.reason);
989
+ throwDataResourceError(entry);
990
+ }
594
991
  if (entry.pending === null) throw new Error(`Data resource "${entry.canonicalKey}" has no pending load.`);
595
992
  throw entry.pending.promise;
596
993
  }
@@ -605,6 +1002,7 @@ var DefaultDataStore = class {
605
1002
  entry.valueController = null;
606
1003
  entry.valueErrors = /* @__PURE__ */ new WeakSet();
607
1004
  entry.value = void 0;
1005
+ entry.transferred = false;
608
1006
  entry.error = void 0;
609
1007
  entry.status = "pending";
610
1008
  } else if (entry.status === "rejected") {
@@ -633,6 +1031,21 @@ function canonicalKeyStartsWith(key, prefix) {
633
1031
  const arrayPrefix = prefix.slice(0, -1);
634
1032
  return key.startsWith(arrayPrefix) && key[arrayPrefix.length] === ",";
635
1033
  }
1034
+ function isHeld(entry) {
1035
+ return entry.subscribers.size > 0 || (entry.retainers?.size ?? 0) > 0;
1036
+ }
1037
+ function transferValue(entry) {
1038
+ const transfer = entry.resource?.transfer;
1039
+ if (!entryHasValue(entry) || transfer === false) return void 0;
1040
+ if (entry.transferred || transfer === void 0) return { value: entry.value };
1041
+ const value = transfer(entry.value);
1042
+ return value === void 0 ? void 0 : { value };
1043
+ }
1044
+ function runParkedLoad(entry) {
1045
+ const run = entry.parkedLoad;
1046
+ entry.parkedLoad = null;
1047
+ run?.();
1048
+ }
636
1049
  function entryHasValue(entry) {
637
1050
  return entry.status === "fulfilled" || entry.status === "refreshing";
638
1051
  }
@@ -707,6 +1120,20 @@ function encodeValue(value, path) {
707
1120
  function isThenable(value) {
708
1121
  return (typeof value === "object" || typeof value === "function") && value !== null && "then" in value && typeof value.then === "function";
709
1122
  }
1123
+ function untilAborted(promise, signal) {
1124
+ if (signal.aborted) return Promise.reject(signal.reason);
1125
+ return new Promise((resolve, reject) => {
1126
+ const abort = () => reject(signal.reason);
1127
+ signal.addEventListener("abort", abort, { once: true });
1128
+ promise.then((value) => {
1129
+ signal.removeEventListener("abort", abort);
1130
+ resolve(value);
1131
+ }, (error) => {
1132
+ signal.removeEventListener("abort", abort);
1133
+ reject(error);
1134
+ });
1135
+ });
1136
+ }
710
1137
  function createPendingLoad(controller) {
711
1138
  let resolve = () => void 0;
712
1139
  return {
@@ -779,6 +1206,6 @@ function setTransitionHandler(handler) {
779
1206
  return previous;
780
1207
  }
781
1208
  //#endregion
782
- export { refreshData as _, createDataStore as a, createContext as b, dataResource as c, invalidateDataError as d, invalidateDataKey as f, readDataStore as g, normalizeDataResourceKey as h, attachDataStore as i, ensureData as l, isDataStoreController as m, setTransitionHandler as n, createRendererDataStore as o, invalidateDataPrefix as p, transition as r, currentDataStore as s, runTransitionScope as t, invalidateData as u, runWithDataStore as v, isContext as x, FigContextSymbol as y };
1209
+ 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 };
783
1210
 
784
- //# sourceMappingURL=transition-DJfG2ZSm.js.map
1211
+ //# sourceMappingURL=transition-C03gUimZ.js.map