@bgub/fig 2.0.0 → 2.1.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 +86 -0
  2. package/README.md +21 -22
  3. package/dist/{element-B4LrR2ZB.d.ts → element-DT9ziqWN.d.ts} +43 -14
  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-GeZ4mX7W.d.ts} +13 -19
  7. package/dist/index.d.ts +3 -3
  8. package/dist/index.js +4 -4
  9. package/dist/internal.d.ts +7 -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-BFLGYE4q.js} +366 -79
  20. package/dist/transition-BFLGYE4q.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-CWjp9qRO.js} +363 -78
  32. package/dist-development/transition-CWjp9qRO.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,4 @@
1
- import { $ as getCurrentDataStore, Q as defineLoadContextCapabilities, Z as dataResourceKeysForError, et as isAttributableError, it as setCurrentDataStore, nt as markDataResourceError, rt as resolveCurrentDataStore } from "./element-DzmNF67v.js";
2
- import { y as isThenable$1 } from "./resource-YO25HR7G.js";
1
+ import { y as isThenable$1 } from "./resource-BZOBllE_.js";
3
2
  //#region src/context.ts
4
3
  /** The Fig context symbol. */
5
4
  const FigContextSymbol = Symbol.for("fig.context");
@@ -15,6 +14,61 @@ function isContext(value) {
15
14
  return typeof value === "function" && "$$typeof" in value && value.$$typeof === FigContextSymbol;
16
15
  }
17
16
  //#endregion
17
+ //#region src/data.ts
18
+ const LoadContextCapabilitiesSymbol = Symbol.for("fig.data-load-context");
19
+ /** Define load context capabilities. */
20
+ function defineLoadContextCapabilities(context, capabilities) {
21
+ Object.defineProperty(context, LoadContextCapabilitiesSymbol, {
22
+ configurable: true,
23
+ enumerable: false,
24
+ value: capabilities
25
+ });
26
+ }
27
+ /** Load context capabilities. */
28
+ function loadContextCapabilities(context) {
29
+ return context[LoadContextCapabilitiesSymbol];
30
+ }
31
+ const objectDataErrors = /* @__PURE__ */ new WeakMap();
32
+ let currentDataStore$1 = null;
33
+ /** Captures an optional data store for synchronous scope re-entry. */
34
+ function getCurrentDataStore() {
35
+ return currentDataStore$1;
36
+ }
37
+ /** Resolves current data store. */
38
+ function resolveCurrentDataStore(message = "Data resource APIs require a Fig data store.") {
39
+ if (currentDataStore$1 === null) throw new Error(message);
40
+ return currentDataStore$1;
41
+ }
42
+ /** Sets current data store. */
43
+ function setCurrentDataStore(store) {
44
+ const previousStore = currentDataStore$1;
45
+ currentDataStore$1 = store;
46
+ return previousStore;
47
+ }
48
+ /** Marks data resource error. */
49
+ function markDataResourceError(error, key) {
50
+ if (!isAttributableError(error)) return;
51
+ let keys = objectDataErrors.get(error);
52
+ if (keys === void 0) {
53
+ keys = [];
54
+ objectDataErrors.set(error, keys);
55
+ }
56
+ if (keys.some((existing) => sameDataResourceKey(existing, key))) return;
57
+ keys.push(key);
58
+ }
59
+ /** Data resource keys for error. */
60
+ function dataResourceKeysForError(error) {
61
+ if (!isAttributableError(error)) return void 0;
62
+ const keys = objectDataErrors.get(error);
63
+ return keys === void 0 || keys.length === 0 ? void 0 : [...keys];
64
+ }
65
+ function sameDataResourceKey(a, b) {
66
+ return a.length === b.length && a.every((value, index) => Object.is(value, b[index]));
67
+ }
68
+ function isAttributableError(value) {
69
+ return (typeof value === "object" || typeof value === "function") && value !== null;
70
+ }
71
+ //#endregion
18
72
  //#region src/data-store.ts
19
73
  const DataResourceSymbol = Symbol.for("fig.data-resource");
20
74
  const DataStoreFactorySymbol = Symbol.for("fig.data-store-factory");
@@ -28,34 +82,13 @@ function dataResource(options) {
28
82
  $$typeof: DataResourceSymbol,
29
83
  [DataStoreFactorySymbol]: dataStoreFactory,
30
84
  debugArgs: options.debugArgs,
85
+ equals: options.equals,
86
+ inactiveRetentionMs: options.inactiveRetentionMs,
31
87
  key: options.key,
32
- load: options.load
88
+ load: options.load,
89
+ watch: options.watch
33
90
  };
34
91
  }
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
92
  /** Reads data store. */
60
93
  function readDataStore() {
61
94
  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 +98,10 @@ function createDataStore(options = {}) {
65
98
  const state = { host: null };
66
99
  const store = createRendererDataStore({
67
100
  getLane: () => state.host === null ? null : state.host.getLane(),
68
- partition: options.partition,
69
- schedule: (owner, lane) => state.host?.schedule(owner, lane)
101
+ schedule: (owner, lane) => state.host?.schedule(owner, lane),
102
+ get watchSources() {
103
+ return state.host?.watchSources === true;
104
+ }
70
105
  });
71
106
  Object.defineProperty(store, DataStoreControllerSymbol, { value: state });
72
107
  if (options.initialData !== void 0) store.hydrate(options.initialData);
@@ -78,16 +113,13 @@ function isDataStoreController(value) {
78
113
  }
79
114
  /** Attach data store. */
80
115
  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.");
116
+ if (initialData !== void 0) throw new Error("Pass initialData to createDataStore(), not the renderer, when adopting a data store.");
82
117
  const state = controller[DataStoreControllerSymbol];
83
118
  if (state === void 0) throw new Error("dataStore must be created with createDataStore().");
84
119
  if (state.host !== null) throw new Error("A data store can only be adopted by one Fig renderer.");
85
120
  state.host = host;
86
121
  return controller;
87
122
  }
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
123
  /** Creates renderer data store. */
92
124
  function createRendererDataStore(host) {
93
125
  return new DefaultDataStore(host);
@@ -101,13 +133,12 @@ var DefaultDataStore = class {
101
133
  entries = /* @__PURE__ */ new Map();
102
134
  inactiveRetentionMs;
103
135
  ownerKeys = /* @__PURE__ */ new WeakMap();
104
- partitionKey;
105
136
  preloadRetentionMs;
106
137
  disposed = false;
138
+ pendingWatchReconciles = null;
107
139
  constructor(host) {
108
140
  this.host = host;
109
141
  this.inactiveRetentionMs = host.inactiveRetentionMs ?? DEFAULT_INACTIVE_RETENTION_MS;
110
- this.partitionKey = host.partition === void 0 ? "" : encodeValue(host.partition, createEncodePath("partition"));
111
142
  this.preloadRetentionMs = host.preloadRetentionMs ?? DEFAULT_PRELOAD_RETENTION_MS;
112
143
  }
113
144
  areDataDependenciesConsistent(reads) {
@@ -135,11 +166,16 @@ var DefaultDataStore = class {
135
166
  if (entry !== void 0) {
136
167
  this.clearInactiveTimer(entry);
137
168
  entry.subscribers.add(owner);
169
+ if (reads !== void 0) entry.retainers?.delete(reads);
138
170
  this.notifyEntryChange(entry);
139
171
  }
140
172
  }
141
173
  }
142
174
  if (orphanCandidates !== null) for (const entry of orphanCandidates) this.abortOrphanedLoad(entry);
175
+ if (nextKeys !== null) for (const key of nextKeys) {
176
+ const entry = this.entries.get(key);
177
+ if (entry !== void 0) this.scheduleWatchReconcile(entry);
178
+ }
143
179
  const consistent = reads === void 0 || this.areDataDependenciesConsistent(reads);
144
180
  reads?.clear();
145
181
  return consistent;
@@ -158,7 +194,10 @@ var DefaultDataStore = class {
158
194
  if (entry === void 0) continue;
159
195
  entry.subscribers.delete(owner);
160
196
  this.notifyEntryChange(entry);
161
- if (retainedKeys?.has(key) !== true) this.scheduleInactiveCleanup(entry);
197
+ if (retainedKeys?.has(key) !== true) {
198
+ this.scheduleInactiveCleanup(entry);
199
+ this.scheduleWatchReconcile(entry);
200
+ }
162
201
  }
163
202
  }
164
203
  collectSubscribedEntries(keys, into) {
@@ -173,8 +212,8 @@ var DefaultDataStore = class {
173
212
  return into;
174
213
  }
175
214
  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;
215
+ if (this.entries.get(entry.canonicalKey) !== entry) return;
216
+ if (isHeld(entry) || entry.preloadTimer !== null || entry.pending === null || entryHasValue(entry)) return;
178
217
  this.evictEntry(entry, "evicted");
179
218
  }
180
219
  dispose() {
@@ -187,27 +226,39 @@ var DefaultDataStore = class {
187
226
  }
188
227
  }
189
228
  hydrate(entries) {
229
+ this.hydrateEntries(entries, true);
230
+ }
231
+ hydrateMissing(entries) {
232
+ this.hydrateEntries(entries, false);
233
+ }
234
+ hydrateEntries(entries, replaceValues) {
190
235
  if (this.disposed) return;
191
236
  for (const hydrated of entries) {
192
237
  if (this.disposed) return;
193
238
  const normalized = normalizeKey(hydrated.key);
194
- const storeKey = this.storeKey(normalized.canonical);
239
+ const storeKey = normalized.canonical;
195
240
  const current = this.entries.get(storeKey);
196
241
  if (current !== void 0) {
197
- this.hydrateEntry(current, normalized.key, hydrated.value);
242
+ if (replaceValues || !entryHasValue(current)) this.hydrateEntry(current, normalized.key, hydrated.value);
198
243
  continue;
199
244
  }
200
- const entry = this.createEntry(normalized, storeKey, null, null, "fulfilled", hydrated.value);
245
+ const entry = this.createEntry(normalized, null, null, "fulfilled", hydrated.value);
201
246
  this.entries.set(storeKey, entry);
202
247
  this.notifyEntryChange(entry);
203
248
  }
204
249
  }
205
250
  snapshot() {
251
+ return this.snapshotKeys(this.entries.keys());
252
+ }
253
+ snapshotKeys(canonicalKeys) {
206
254
  const entries = [];
207
- for (const entry of this.entries.values()) if (entryHasValue(entry)) entries.push({
208
- key: entry.key,
209
- value: entry.value
210
- });
255
+ for (const key of canonicalKeys) {
256
+ const entry = this.entries.get(key);
257
+ if (entry !== void 0 && entryHasValue(entry)) entries.push({
258
+ key: entry.key,
259
+ value: entry.value
260
+ });
261
+ }
211
262
  return entries;
212
263
  }
213
264
  inspectDataEntries() {
@@ -254,8 +305,12 @@ var DefaultDataStore = class {
254
305
  for (const entry of this.entries.values()) if (canonicalKeyStartsWith(entry.canonicalKey, prefixCanonical)) entries.push(entry);
255
306
  this.invalidateEntries(entries);
256
307
  }
257
- async ensureData(resource, ...args) {
308
+ ensureData(resource, ...args) {
309
+ return this.ensureDataUntil(resource, args, void 0);
310
+ }
311
+ async ensureDataUntil(resource, args, until) {
258
312
  for (;;) {
313
+ if (until?.aborted === true) throw until.reason;
259
314
  if (this.disposed) throw new Error("ensureData() requires a live data store; this store was disposed.");
260
315
  const entry = this.entryFor(resource, args, true);
261
316
  this.clearInactiveTimer(entry);
@@ -265,16 +320,24 @@ var DefaultDataStore = class {
265
320
  return entry.value;
266
321
  }
267
322
  if (entry.status === "rejected") throwDataResourceError(entry);
268
- entry.ensureRetainers += 1;
323
+ const retainer = {};
324
+ (entry.retainers ??= /* @__PURE__ */ new Set()).add(retainer);
269
325
  try {
270
- if (entry.pending !== null) await entry.pending.promise;
271
- else await this.startLoad(entry, resource, args, {
326
+ this.recordWatchSource(entry, resource, args);
327
+ let settled;
328
+ if (entry.pending !== null) settled = entry.pending.promise;
329
+ else if (this.waitsOnWatch(resource)) settled = this.beginWatchPending(entry).promise;
330
+ else settled = this.startLoad(entry, resource, args, {
272
331
  lane: this.host.getLane(),
273
332
  refresh: false
274
333
  });
334
+ await (until === void 0 ? settled : untilAborted(settled, until));
275
335
  } finally {
276
- entry.ensureRetainers -= 1;
277
- if (this.entries.get(entry.storeKey) === entry) this.retainPreload(entry);
336
+ entry.retainers?.delete(retainer);
337
+ if (this.entries.get(entry.canonicalKey) === entry) {
338
+ this.retainPreload(entry);
339
+ this.scheduleWatchReconcile(entry);
340
+ }
278
341
  }
279
342
  }
280
343
  }
@@ -294,14 +357,19 @@ var DefaultDataStore = class {
294
357
  const entry = this.entryFor(resource, args, true);
295
358
  this.clearInactiveTimer(entry);
296
359
  this.clearPreloadTimer(entry);
297
- if (reads !== void 0 && !reads.has(entry.storeKey)) reads.set(entry.storeKey, void 0);
360
+ if (reads !== void 0) (entry.retainers ??= /* @__PURE__ */ new Set()).add(reads);
361
+ if (reads !== void 0 && !reads.has(entry.canonicalKey)) reads.set(entry.canonicalKey, void 0);
362
+ this.recordWatchSource(entry, resource, args);
298
363
  this.revalidateIfStale(entry, resource, args);
299
- if (entry.status === "pending" && entry.pending === null) this.startLoad(entry, resource, args, {
300
- lane: this.host.getLane(),
301
- refresh: false
302
- });
364
+ if (entry.status === "pending" && entry.pending === null) {
365
+ if (this.waitsOnWatch(resource)) this.beginWatchPending(entry);
366
+ else this.startLoad(entry, resource, args, {
367
+ lane: this.host.getLane(),
368
+ refresh: false
369
+ });
370
+ }
303
371
  const value = this.readCurrentValue(entry);
304
- if (reads !== void 0 && reads.get(entry.storeKey) === void 0) reads.set(entry.storeKey, { value });
372
+ if (reads !== void 0 && reads.get(entry.canonicalKey) === void 0) reads.set(entry.canonicalKey, { value });
305
373
  return value;
306
374
  }
307
375
  refreshData(resource, ...args) {
@@ -335,14 +403,16 @@ var DefaultDataStore = class {
335
403
  entryFor(resource, args, create) {
336
404
  const normalized = normalizeKey(resource.key(...args));
337
405
  const fingerprint = fingerprintFor(resource, args);
338
- const key = this.storeKey(normalized.canonical);
406
+ const key = normalized.canonical;
339
407
  const current = this.entries.get(key);
340
408
  if (current !== void 0) {
409
+ if (resource.inactiveRetentionMs !== void 0) current.inactiveRetentionMs = resource.inactiveRetentionMs;
341
410
  diagnoseEntryDrift(current, resource, normalized.canonical, fingerprint);
342
411
  return current;
343
412
  }
344
413
  if (!create) return null;
345
- const entry = this.createEntry(normalized, key, resource, fingerprint, "pending", void 0);
414
+ const entry = this.createEntry(normalized, resource, fingerprint, "pending", void 0);
415
+ entry.inactiveRetentionMs = resource.inactiveRetentionMs;
346
416
  if (this.disposed) return entry;
347
417
  this.entries.set(key, entry);
348
418
  this.notifyEntryChange(entry);
@@ -357,15 +427,16 @@ var DefaultDataStore = class {
357
427
  }
358
428
  entryForKey(key) {
359
429
  const normalized = normalizeKey(key);
360
- return this.entries.get(this.storeKey(normalized.canonical)) ?? null;
430
+ return this.entries.get(normalized.canonical) ?? null;
361
431
  }
362
- createEntry(normalized, storeKey, resource, fingerprint, status, value) {
432
+ createEntry(normalized, resource, fingerprint, status, value) {
363
433
  return {
364
434
  canonicalKey: normalized.canonical,
365
- ensureRetainers: 0,
435
+ retainers: null,
366
436
  error: void 0,
367
437
  fingerprint,
368
438
  invalidationVersion: 0,
439
+ inactiveRetentionMs: void 0,
369
440
  inactiveTimer: null,
370
441
  key: normalized.key,
371
442
  lane: null,
@@ -376,13 +447,190 @@ var DefaultDataStore = class {
376
447
  resource,
377
448
  stale: false,
378
449
  status,
379
- storeKey,
380
450
  subscribers: /* @__PURE__ */ new Set(),
381
451
  value,
382
452
  valueController: null,
383
- valueErrors: /* @__PURE__ */ new WeakSet()
453
+ valueErrors: /* @__PURE__ */ new WeakSet(),
454
+ watchController: null,
455
+ watchSource: null
456
+ };
457
+ }
458
+ recordWatchSource(entry, resource, args) {
459
+ if (resource.watch === void 0 || entry.watchSource !== null) return;
460
+ entry.watchSource = {
461
+ args,
462
+ resource
463
+ };
464
+ }
465
+ waitsOnWatch(resource) {
466
+ return resource.load === void 0 && resource.watch !== void 0 && this.host.watchSources === true;
467
+ }
468
+ beginWatchPending(entry) {
469
+ if (entry.pending !== null) return entry.pending;
470
+ const pending = createPendingLoad(new AbortController());
471
+ pending.watch = true;
472
+ entry.pending = pending;
473
+ entry.status = "pending";
474
+ this.notifyEntryChange(entry);
475
+ this.scheduleWatchReconcile(entry);
476
+ return pending;
477
+ }
478
+ dependentHandle(signal) {
479
+ const retain = (entry) => {
480
+ if (signal.aborted || entry.retainers?.has(signal)) return;
481
+ (entry.retainers ??= /* @__PURE__ */ new Set()).add(signal);
482
+ this.clearInactiveTimer(entry);
483
+ signal.addEventListener("abort", () => this.releaseRetainer(entry, signal), { once: true });
484
+ };
485
+ return {
486
+ ensureData: (resource, ...args) => {
487
+ if (signal.aborted) return Promise.reject(signal.reason);
488
+ if (!this.disposed) {
489
+ const entry = this.entryFor(resource, args, true);
490
+ this.recordWatchSource(entry, resource, args);
491
+ retain(entry);
492
+ this.scheduleWatchReconcile(entry);
493
+ }
494
+ return this.ensureDataUntil(resource, args, signal);
495
+ },
496
+ hydrate: (entries) => this.hydrate(entries),
497
+ invalidateData: (resource, ...args) => this.invalidateData(resource, ...args),
498
+ invalidateDataError: (error) => this.invalidateDataError(error),
499
+ invalidateDataKey: (key) => this.invalidateDataKey(key),
500
+ invalidateDataPrefix: (prefix) => this.invalidateDataPrefix(prefix),
501
+ preloadData: (resource, ...args) => this.preloadData(resource, ...args),
502
+ refreshData: (resource, ...args) => this.refreshData(resource, ...args),
503
+ run: (callback) => this.run(callback)
504
+ };
505
+ }
506
+ keepEqualValue(entry, stale) {
507
+ entry.error = void 0;
508
+ entry.pending = null;
509
+ entry.refreshError = void 0;
510
+ entry.refreshFailed = false;
511
+ entry.stale = stale;
512
+ entry.status = "fulfilled";
513
+ this.notifyEntryChange(entry);
514
+ this.scheduleInactiveCleanup(entry);
515
+ }
516
+ scheduleWatchReconcile(entry) {
517
+ if (entry.watchSource === null && entry.watchController === null) return;
518
+ if (this.pendingWatchReconciles !== null) {
519
+ this.pendingWatchReconciles.add(entry);
520
+ return;
521
+ }
522
+ const entries = /* @__PURE__ */ new Set([entry]);
523
+ this.pendingWatchReconciles = entries;
524
+ queueMicrotask(() => {
525
+ this.pendingWatchReconciles = null;
526
+ for (const pending of entries) this.reconcileWatch(pending);
527
+ });
528
+ }
529
+ wantsWatch(entry) {
530
+ const source = entry.watchSource;
531
+ if (source === null || this.disposed || this.host.watchSources !== true || this.entries.get(entry.canonicalKey) !== entry) return false;
532
+ if (entry.subscribers.size > 0) return true;
533
+ return source.resource.load === void 0 && isHeld(entry);
534
+ }
535
+ reconcileWatch(entry) {
536
+ const wanted = this.wantsWatch(entry);
537
+ if (wanted && entry.watchController === null) this.startWatch(entry);
538
+ else if (!wanted && entry.watchController !== null) this.stopWatch(entry, "released");
539
+ if (!wanted && entry.pending?.watch === true && !isHeld(entry)) this.evictEntry(entry, "evicted");
540
+ }
541
+ holdDataRead(reads, canonicalKey) {
542
+ if (!reads.has(canonicalKey)) reads.set(canonicalKey, void 0);
543
+ const entry = this.entries.get(canonicalKey);
544
+ if (entry !== void 0) (entry.retainers ??= /* @__PURE__ */ new Set()).add(reads);
545
+ }
546
+ releaseDataReads(reads) {
547
+ for (const key of reads.keys()) {
548
+ const entry = this.entries.get(key);
549
+ if (entry !== void 0) this.releaseRetainer(entry, reads);
550
+ }
551
+ }
552
+ releaseRetainer(entry, owner) {
553
+ if (!entry.retainers?.delete(owner) || this.entries.get(entry.canonicalKey) !== entry) return;
554
+ this.scheduleInactiveCleanup(entry);
555
+ this.scheduleWatchReconcile(entry);
556
+ }
557
+ startWatch(entry) {
558
+ const source = entry.watchSource;
559
+ const watch = source?.resource.watch;
560
+ if (source === null || watch === void 0) return;
561
+ const controller = new AbortController();
562
+ entry.watchController = controller;
563
+ this.notifyEntryChange(entry);
564
+ const context = this.createWatchContext(entry, source, controller);
565
+ try {
566
+ watch(...source.args, context);
567
+ } catch (error) {
568
+ context.fail(error);
569
+ }
570
+ }
571
+ stopWatch(entry, reason) {
572
+ const controller = entry.watchController;
573
+ if (controller === null) return;
574
+ entry.watchController = null;
575
+ if (reason === "released" && entryHasValue(entry)) entry.stale = true;
576
+ this.notifyEntryChange(entry);
577
+ controller.abort(reason);
578
+ }
579
+ createWatchContext(entry, source, controller) {
580
+ const live = () => !this.disposed && !controller.signal.aborted && this.entries.get(entry.canonicalKey) === entry;
581
+ return {
582
+ data: this.dependentHandle(controller.signal),
583
+ signal: controller.signal,
584
+ publish: (value) => {
585
+ if (!live()) return;
586
+ const equals = source.resource.equals ?? Object.is;
587
+ if (entryHasValue(entry) && equals(entry.value, value)) {
588
+ const pending = entry.pending;
589
+ entry.invalidationVersion = 0;
590
+ entry.lane = null;
591
+ this.keepEqualValue(entry, false);
592
+ pending?.resolve(abortedRefreshResult(entry, "superseded"));
593
+ pending?.controller.abort("superseded");
594
+ return;
595
+ }
596
+ this.hydrateEntry(entry, entry.key, value);
597
+ this.scheduleWatchReconcile(entry);
598
+ },
599
+ fail: (error) => {
600
+ if (!live()) return;
601
+ this.failWatchedEntry(entry, error);
602
+ },
603
+ refresh: () => {
604
+ if (live()) return this.refreshData(source.resource, ...source.args);
605
+ const reason = this.disposed ? "store-disposed" : controller.signal.reason === "released" ? "released" : "evicted";
606
+ return Promise.resolve(abortedRefreshResult(entry, reason));
607
+ },
608
+ invalidate: () => {
609
+ if (!live()) return;
610
+ this.invalidateEntry(entry, this.host.getLane());
611
+ }
384
612
  };
385
613
  }
614
+ failWatchedEntry(entry, error) {
615
+ if (entryHasValue(entry)) {
616
+ entry.refreshError = error;
617
+ entry.refreshFailed = true;
618
+ entry.stale = true;
619
+ this.publish(entry);
620
+ return;
621
+ }
622
+ const pending = entry.pending;
623
+ entry.pending = null;
624
+ entry.error = error;
625
+ entry.status = "rejected";
626
+ markDataResourceError(error, entry.key);
627
+ pending?.resolve({
628
+ error,
629
+ status: "rejected"
630
+ });
631
+ this.publish(entry);
632
+ pending?.controller.abort(error);
633
+ }
386
634
  hydrateEntry(entry, key, value) {
387
635
  this.clearInactiveTimer(entry);
388
636
  const valueController = entry.valueController;
@@ -404,9 +652,6 @@ var DefaultDataStore = class {
404
652
  pending?.controller.abort("superseded");
405
653
  valueController?.abort("superseded");
406
654
  }
407
- storeKey(canonicalKey) {
408
- return this.partitionKey === "" ? canonicalKey : `${this.partitionKey}:${canonicalKey}`;
409
- }
410
655
  startLoad(entry, resource, args, options) {
411
656
  const hadValue = entryHasValue(entry);
412
657
  const load = resource.load;
@@ -444,6 +689,23 @@ var DefaultDataStore = class {
444
689
  };
445
690
  const fulfill = (value) => {
446
691
  if (entry.pending !== pending || controller.signal.aborted) return settleAborted();
692
+ const equals = resource.equals ?? Object.is;
693
+ let equal;
694
+ try {
695
+ equal = entryHasValue(entry) && equals(entry.value, value);
696
+ } catch (error) {
697
+ return reject(error);
698
+ }
699
+ if (equal) {
700
+ this.keepEqualValue(entry, entry.invalidationVersion !== invalidationVersion);
701
+ const result = {
702
+ status: "fulfilled",
703
+ value: entry.value
704
+ };
705
+ pending.resolve(result);
706
+ controller.abort("superseded");
707
+ return result;
708
+ }
447
709
  const superseded = entry.valueController;
448
710
  entry.valueController = controller;
449
711
  entry.valueErrors = valueErrors;
@@ -505,7 +767,10 @@ var DefaultDataStore = class {
505
767
  return pending.promise;
506
768
  }
507
769
  createLoadContext(entry, controller, valueErrors) {
508
- const context = { signal: controller.signal };
770
+ const context = {
771
+ data: this.dependentHandle(controller.signal),
772
+ signal: controller.signal
773
+ };
509
774
  defineLoadContextCapabilities(context, {
510
775
  key: entry.key,
511
776
  attributeError: (error) => {
@@ -515,7 +780,7 @@ var DefaultDataStore = class {
515
780
  },
516
781
  hydrate: (entries) => {
517
782
  if (this.disposed || controller.signal.aborted) return;
518
- const foreign = entries.filter((hydrated) => this.storeKey(normalizeKey(hydrated.key).canonical) !== entry.storeKey);
783
+ const foreign = entries.filter((hydrated) => normalizeKey(hydrated.key).canonical !== entry.canonicalKey);
519
784
  if (foreign.length !== entries.length) warn(`Data rows targeting the loading key ${entry.canonicalKey} were skipped: a loader cannot hydrate its own entry.`);
520
785
  for (const hydrated of foreign) {
521
786
  if (controller.signal.aborted) return;
@@ -541,13 +806,14 @@ var DefaultDataStore = class {
541
806
  pending?.resolve(abortedRefreshResult(entry, reason));
542
807
  pending?.controller.abort(reason);
543
808
  valueController?.abort(reason);
809
+ this.stopWatch(entry, reason);
544
810
  }
545
811
  retainPreload(entry) {
546
812
  this.clearPreloadTimer(entry);
547
813
  if (this.disposed || !Number.isFinite(this.preloadRetentionMs)) return;
548
814
  entry.preloadTimer = scheduleStoreTimer(() => {
549
815
  entry.preloadTimer = null;
550
- if (entry.subscribers.size === 0 && entry.ensureRetainers === 0 && entry.pending !== null && !entryHasValue(entry)) {
816
+ if (!isHeld(entry) && entry.pending !== null && !entryHasValue(entry)) {
551
817
  this.evictEntry(entry, "evicted");
552
818
  return;
553
819
  }
@@ -555,19 +821,20 @@ var DefaultDataStore = class {
555
821
  }, Math.max(0, this.preloadRetentionMs));
556
822
  }
557
823
  scheduleInactiveCleanup(entry) {
558
- if (this.disposed || entry.subscribers.size > 0 || entry.pending !== null || entry.preloadTimer !== null || entry.ensureRetainers > 0 || !Number.isFinite(this.inactiveRetentionMs)) return;
824
+ const retention = entry.inactiveRetentionMs ?? this.inactiveRetentionMs;
825
+ if (this.disposed || isHeld(entry) || entry.pending !== null || entry.preloadTimer !== null || !Number.isFinite(retention)) return;
559
826
  this.clearInactiveTimer(entry);
560
827
  entry.inactiveTimer = scheduleStoreTimer(() => {
561
828
  entry.inactiveTimer = null;
562
- if (entry.subscribers.size > 0 || entry.pending !== null || entry.preloadTimer !== null || entry.ensureRetainers > 0) return;
829
+ if (isHeld(entry) || entry.pending !== null || entry.preloadTimer !== null) return;
563
830
  this.evictEntry(entry, "evicted");
564
- }, Math.max(0, this.inactiveRetentionMs));
831
+ }, Math.max(0, retention));
565
832
  }
566
833
  evictEntry(entry, reason) {
567
- if (this.entries.get(entry.storeKey) !== entry) return;
834
+ if (this.entries.get(entry.canonicalKey) !== entry) return;
568
835
  this.clearInactiveTimer(entry);
569
836
  this.clearPreloadTimer(entry);
570
- this.entries.delete(entry.storeKey);
837
+ this.entries.delete(entry.canonicalKey);
571
838
  this.abortEntryGenerations(entry, reason);
572
839
  this.host.onEntryEvict?.(this.snapshotEntry(entry));
573
840
  }
@@ -596,7 +863,8 @@ var DefaultDataStore = class {
596
863
  stale: entry.stale,
597
864
  status: entry.status,
598
865
  subscriberCount: entry.subscribers.size,
599
- value: hasValue ? entry.value : void 0
866
+ value: hasValue ? entry.value : void 0,
867
+ ...entry.watchController === null ? {} : { watching: true }
600
868
  };
601
869
  }
602
870
  readCurrentValue(entry) {
@@ -644,6 +912,9 @@ function canonicalKeyStartsWith(key, prefix) {
644
912
  const arrayPrefix = prefix.slice(0, -1);
645
913
  return key.startsWith(arrayPrefix) && key[arrayPrefix.length] === ",";
646
914
  }
915
+ function isHeld(entry) {
916
+ return entry.subscribers.size > 0 || (entry.retainers?.size ?? 0) > 0;
917
+ }
647
918
  function entryHasValue(entry) {
648
919
  return entry.status === "fulfilled" || entry.status === "refreshing";
649
920
  }
@@ -735,6 +1006,20 @@ function encodeValue(value, path) {
735
1006
  function isThenable(value) {
736
1007
  return (typeof value === "object" || typeof value === "function") && value !== null && "then" in value && typeof value.then === "function";
737
1008
  }
1009
+ function untilAborted(promise, signal) {
1010
+ if (signal.aborted) return Promise.reject(signal.reason);
1011
+ return new Promise((resolve, reject) => {
1012
+ const abort = () => reject(signal.reason);
1013
+ signal.addEventListener("abort", abort, { once: true });
1014
+ promise.then((value) => {
1015
+ signal.removeEventListener("abort", abort);
1016
+ resolve(value);
1017
+ }, (error) => {
1018
+ signal.removeEventListener("abort", abort);
1019
+ reject(error);
1020
+ });
1021
+ });
1022
+ }
738
1023
  function createPendingLoad(controller) {
739
1024
  let resolve = () => void 0;
740
1025
  return {
@@ -807,6 +1092,6 @@ function setTransitionHandler(handler) {
807
1092
  return previous;
808
1093
  }
809
1094
  //#endregion
810
- 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 };
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 };
811
1096
 
812
- //# sourceMappingURL=transition-CrIF0Kz_.js.map
1097
+ //# sourceMappingURL=transition-CWjp9qRO.js.map