react-inp-chunker 0.3.0 → 0.4.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 (38) hide show
  1. package/README.md +31 -11
  2. package/dist/core/index.cjs +1 -1
  3. package/dist/core/index.d.cts +1 -1
  4. package/dist/core/index.d.ts +1 -1
  5. package/dist/core/index.js +1 -1
  6. package/dist/{deferred-task-6oTEnHWa.js → deferred-task-Cai4fF4m.js} +5 -3
  7. package/dist/deferred-task-Cai4fF4m.js.map +1 -0
  8. package/dist/{deferred-task-DcnC99_Y.cjs → deferred-task-DRigxydg.cjs} +5 -3
  9. package/dist/deferred-task-DRigxydg.cjs.map +1 -0
  10. package/dist/{index-B4gKjLYR.d.ts → index-DuUMqnsA.d.cts} +89 -45
  11. package/dist/index-DuUMqnsA.d.cts.map +1 -0
  12. package/dist/{index-B4gKjLYR.d.cts → index-DuUMqnsA.d.ts} +89 -45
  13. package/dist/index-DuUMqnsA.d.ts.map +1 -0
  14. package/dist/index.cjs +11 -5
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +72 -11
  17. package/dist/index.d.cts.map +1 -1
  18. package/dist/index.d.ts +72 -11
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +11 -5
  21. package/dist/index.js.map +1 -1
  22. package/dist/production/core/index.cjs +1 -1
  23. package/dist/production/core/index.js +1 -1
  24. package/dist/production/{deferred-task-DkHdsliy.js → deferred-task-MsTNINAd.js} +5 -3
  25. package/dist/production/deferred-task-MsTNINAd.js.map +1 -0
  26. package/dist/production/{deferred-task-yh5pWlSj.cjs → deferred-task-j8KF7lk7.cjs} +5 -3
  27. package/dist/production/deferred-task-j8KF7lk7.cjs.map +1 -0
  28. package/dist/production/index.cjs +11 -5
  29. package/dist/production/index.cjs.map +1 -1
  30. package/dist/production/index.js +11 -5
  31. package/dist/production/index.js.map +1 -1
  32. package/package.json +6 -3
  33. package/dist/deferred-task-6oTEnHWa.js.map +0 -1
  34. package/dist/deferred-task-DcnC99_Y.cjs.map +0 -1
  35. package/dist/index-B4gKjLYR.d.cts.map +0 -1
  36. package/dist/index-B4gKjLYR.d.ts.map +0 -1
  37. package/dist/production/deferred-task-DkHdsliy.js.map +0 -1
  38. package/dist/production/deferred-task-yh5pWlSj.cjs.map +0 -1
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { D as ChunkStatus, E as ChunkMachineState, T as ChunkEvent, a as DeferredTaskSnapshot, c as ChunkSchedulerOptions, d as SkippedChunkItem, h as YieldStrategy, i as DeferredTaskResult, l as ChunkSchedulerResult, n as DeferredTask, r as DeferredTaskOptions, s as ChunkScheduler, t as DeferredGenerator, u as ChunkSnapshot } from "./index-B4gKjLYR.cjs";
1
+ import { D as ChunkStatus, E as ChunkMachineState, T as ChunkEvent, a as DeferredTaskSnapshot, c as ChunkSchedulerOptions, d as SkippedChunkItem, h as YieldStrategy, i as DeferredTaskResult, l as ChunkSchedulerResult, n as DeferredTask, r as DeferredTaskOptions, s as ChunkScheduler, t as DeferredGenerator, u as ChunkSnapshot } from "./index-DuUMqnsA.cjs";
2
2
  interface UseChunkedTaskOptions<T, R> {
3
3
  /** Max ms of uninterrupted work before yielding. Default 5. */
4
4
  readonly timeSliceMs?: number;
@@ -41,16 +41,27 @@ interface UseChunkedTaskOptions<T, R> {
41
41
  readonly onSlice?: (durationMs: number, processed: number) => void;
42
42
  /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
43
43
  readonly onYield?: () => void;
44
+ /**
45
+ * External cancellation source — aborting it is equivalent to calling
46
+ * `cancel()`, and its `reason` comes through on `reason` below, so a
47
+ * cancellation caused by the signal is distinguishable from a direct
48
+ * `cancel()` call. A new reference restarts the task the same way a new
49
+ * `items` array does, so memoize it (e.g. from a ref or a parent-owned
50
+ * `AbortController`) unless you mean to restart on every render.
51
+ */
52
+ readonly signal?: AbortSignal;
44
53
  }
45
- interface UseChunkedTaskState<R> {
46
- readonly status: ChunkStatus;
54
+ /** Fields every status shares, independent of the `status`/`results`/`error` narrowing below. */
55
+ interface UseChunkedTaskShared {
47
56
  readonly processed: number;
48
57
  readonly total: number;
49
- readonly error: unknown;
50
- /** The full results array once the task has settled — `undefined` until then. */
51
- readonly results: readonly R[] | undefined;
52
58
  /** Items `onError` has skipped so far, growing live as skips happen. Empty when unused. */
53
59
  readonly skipped: readonly SkippedChunkItem[];
60
+ /**
61
+ * The `signal` option's `reason`, present only when cancellation came
62
+ * from that signal aborting rather than a direct `cancel()` call.
63
+ */
64
+ readonly reason: unknown;
54
65
  readonly isIdle: boolean;
55
66
  readonly isProcessing: boolean;
56
67
  readonly isPaused: boolean;
@@ -62,6 +73,26 @@ interface UseChunkedTaskState<R> {
62
73
  readonly resume: () => void;
63
74
  readonly cancel: () => void;
64
75
  }
76
+ /**
77
+ * Discriminated on `status`. Narrowing to `'done'` or `'cancelled'` narrows
78
+ * `results` to a defined array, no `=== undefined` check needed alongside
79
+ * `isDone`; narrowing to `'error'` narrows `error` to defined instead. The
80
+ * `is*` booleans above remain the simple way to branch when you only care
81
+ * about one status and don't need the narrowing.
82
+ */
83
+ type UseChunkedTaskState<R> = UseChunkedTaskShared & ({
84
+ readonly status: Exclude<ChunkStatus, 'done' | 'cancelled' | 'error'>;
85
+ readonly results: undefined;
86
+ readonly error: undefined;
87
+ } | {
88
+ readonly status: 'done' | 'cancelled';
89
+ readonly results: readonly R[];
90
+ readonly error: undefined;
91
+ } | {
92
+ readonly status: 'error';
93
+ readonly results: readonly R[];
94
+ readonly error: unknown;
95
+ });
65
96
  /**
66
97
  * Chunks a heavy, synchronous per-item computation over `items` so it never
67
98
  * blocks the main thread for longer than a time slice — the thing that
@@ -98,12 +129,23 @@ interface UseDeferredTaskOptions {
98
129
  readonly onSlice?: (durationMs: number, segmentsDone: number) => void;
99
130
  /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
100
131
  readonly onYield?: () => void;
132
+ /**
133
+ * External cancellation source — aborting it is equivalent to calling
134
+ * `cancel()`, and its `reason` comes through on `reason` below, so a
135
+ * cancellation caused by the signal is distinguishable from a direct
136
+ * `cancel()` call. A new reference restarts the task the same way a new
137
+ * entry in `deps` does, so memoize it unless you mean to restart on
138
+ * every render.
139
+ */
140
+ readonly signal?: AbortSignal;
101
141
  }
102
- interface UseDeferredTaskState<T> {
103
- readonly status: ChunkStatus;
104
- readonly error: unknown;
105
- /** The returned value once the task settles with `done` — `undefined` until then. */
106
- readonly value: T | undefined;
142
+ /** Fields every status shares, independent of the `status`/`value`/`error` narrowing below. */
143
+ interface UseDeferredTaskShared {
144
+ /**
145
+ * The `signal` option's `reason`, present only when cancellation came
146
+ * from that signal aborting rather than a direct `cancel()` call.
147
+ */
148
+ readonly reason: unknown;
107
149
  readonly isIdle: boolean;
108
150
  readonly isProcessing: boolean;
109
151
  readonly isPaused: boolean;
@@ -115,6 +157,25 @@ interface UseDeferredTaskState<T> {
115
157
  readonly resume: () => void;
116
158
  readonly cancel: () => void;
117
159
  }
160
+ /**
161
+ * Discriminated on `status`. Narrowing to `'done'` narrows `value` to `T`
162
+ * itself — no `=== undefined` check needed alongside `isDone`, even when
163
+ * `T` allows `undefined` as a genuine return value. Narrowing to `'error'`
164
+ * narrows `error` to defined instead.
165
+ */
166
+ type UseDeferredTaskState<T> = UseDeferredTaskShared & ({
167
+ readonly status: Exclude<ChunkStatus, 'done' | 'error'>;
168
+ readonly value: undefined;
169
+ readonly error: undefined;
170
+ } | {
171
+ readonly status: 'done';
172
+ readonly value: T;
173
+ readonly error: undefined;
174
+ } | {
175
+ readonly status: 'error';
176
+ readonly value: undefined;
177
+ readonly error: unknown;
178
+ });
118
179
  /**
119
180
  * Runs an arbitrary heavy, synchronous generator function in chunks, so it
120
181
  * never produces a long task on its own. Where `useChunkedTask` chunks "one
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"mappings":";UAeiB,sBAAsB,GAAG;;WAE/B;;;;;;;WAOA;;;;;;WAMA;;WAEA;;;;;;;WAOA,UAAU,QAAQ,GAAG,MAAM,GAAG;;;;;;;WAO9B,WAAW,gBAAgB,MAAM,GAAG;;;;;;;;WAQpC,WAAW,oBAAoB;;WAE/B;;UAGM,oBAAoB;WAC1B,QAAQ;WACR;WACA;WACA;;WAEA,kBAAkB;;WAElB,kBAAkB;WAClB;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;;;;;;;;;wBAoBK,eAAe,GAAG,GAChC,gBAAgB,KAChB,cAAc,MAAM,GAAG,kBAAkB,GACzC,UAAU,sBAAsB,GAAG,KAClC,oBAAoB;UCzFN;;WAEN;;;;;;WAMA;;WAEA;;;;;;;WAOA,WAAW,oBAAoB;;WAE/B;;UAGM,qBAAqB;WAC3B,QAAQ;WACR;;WAEA,OAAO;WACP;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;;;;;wBAgBK,gBAAgB,GAC9B,YAAY,kBAAkB,IAC9B,0BACA,UAAU,yBACT,qBAAqB"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"mappings":";UAeiB,sBAAsB,GAAG;;WAE/B;;;;;;;WAOA;;;;;;WAMA;;WAEA;;;;;;;WAOA,UAAU,QAAQ,GAAG,MAAM,GAAG;;;;;;;WAO9B,WAAW,gBAAgB,MAAM,GAAG;;;;;;;;WAQpC,WAAW,oBAAoB;;WAE/B;;;;;;;;;WASA,SAAS;;;UAIV;WACC;WACA;;WAEA,kBAAkB;;;;;WAKlB;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;KAUC,oBAAoB,KAAK;WAGpB,QAAQ,QAAQ;WAChB;WACA;;WAGA;WACA,kBAAkB;WAClB;;WAGA;WACA,kBAAkB;WAClB;;;;;;;;;;;;;;;;;wBAwBD,eAAe,GAAG,GAChC,gBAAgB,KAChB,cAAc,MAAM,GAAG,kBAAkB,GACzC,UAAU,sBAAsB,GAAG,KAClC,oBAAoB;UCjIN;;WAEN;;;;;;WAMA;;WAEA;;;;;;;WAOA,WAAW,oBAAoB;;WAE/B;;;;;;;;;WASA,SAAS;;;UAIV;;;;;WAKC;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;KASC,qBAAqB,KAAK;WAGrB,QAAQ,QAAQ;WAChB;WACA;;WAEA;WAAyB,OAAO;WAAY;;WAC5C;WAA0B;WAA2B;;;;;;;;;;;;;wBAkBtD,gBAAgB,GAC9B,YAAY,kBAAkB,IAC9B,0BACA,UAAU,yBACT,qBAAqB"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { D as ChunkStatus, E as ChunkMachineState, T as ChunkEvent, a as DeferredTaskSnapshot, c as ChunkSchedulerOptions, d as SkippedChunkItem, h as YieldStrategy, i as DeferredTaskResult, l as ChunkSchedulerResult, n as DeferredTask, r as DeferredTaskOptions, s as ChunkScheduler, t as DeferredGenerator, u as ChunkSnapshot } from "./index-B4gKjLYR.js";
1
+ import { D as ChunkStatus, E as ChunkMachineState, T as ChunkEvent, a as DeferredTaskSnapshot, c as ChunkSchedulerOptions, d as SkippedChunkItem, h as YieldStrategy, i as DeferredTaskResult, l as ChunkSchedulerResult, n as DeferredTask, r as DeferredTaskOptions, s as ChunkScheduler, t as DeferredGenerator, u as ChunkSnapshot } from "./index-DuUMqnsA.js";
2
2
  interface UseChunkedTaskOptions<T, R> {
3
3
  /** Max ms of uninterrupted work before yielding. Default 5. */
4
4
  readonly timeSliceMs?: number;
@@ -41,16 +41,27 @@ interface UseChunkedTaskOptions<T, R> {
41
41
  readonly onSlice?: (durationMs: number, processed: number) => void;
42
42
  /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
43
43
  readonly onYield?: () => void;
44
+ /**
45
+ * External cancellation source — aborting it is equivalent to calling
46
+ * `cancel()`, and its `reason` comes through on `reason` below, so a
47
+ * cancellation caused by the signal is distinguishable from a direct
48
+ * `cancel()` call. A new reference restarts the task the same way a new
49
+ * `items` array does, so memoize it (e.g. from a ref or a parent-owned
50
+ * `AbortController`) unless you mean to restart on every render.
51
+ */
52
+ readonly signal?: AbortSignal;
44
53
  }
45
- interface UseChunkedTaskState<R> {
46
- readonly status: ChunkStatus;
54
+ /** Fields every status shares, independent of the `status`/`results`/`error` narrowing below. */
55
+ interface UseChunkedTaskShared {
47
56
  readonly processed: number;
48
57
  readonly total: number;
49
- readonly error: unknown;
50
- /** The full results array once the task has settled — `undefined` until then. */
51
- readonly results: readonly R[] | undefined;
52
58
  /** Items `onError` has skipped so far, growing live as skips happen. Empty when unused. */
53
59
  readonly skipped: readonly SkippedChunkItem[];
60
+ /**
61
+ * The `signal` option's `reason`, present only when cancellation came
62
+ * from that signal aborting rather than a direct `cancel()` call.
63
+ */
64
+ readonly reason: unknown;
54
65
  readonly isIdle: boolean;
55
66
  readonly isProcessing: boolean;
56
67
  readonly isPaused: boolean;
@@ -62,6 +73,26 @@ interface UseChunkedTaskState<R> {
62
73
  readonly resume: () => void;
63
74
  readonly cancel: () => void;
64
75
  }
76
+ /**
77
+ * Discriminated on `status`. Narrowing to `'done'` or `'cancelled'` narrows
78
+ * `results` to a defined array, no `=== undefined` check needed alongside
79
+ * `isDone`; narrowing to `'error'` narrows `error` to defined instead. The
80
+ * `is*` booleans above remain the simple way to branch when you only care
81
+ * about one status and don't need the narrowing.
82
+ */
83
+ type UseChunkedTaskState<R> = UseChunkedTaskShared & ({
84
+ readonly status: Exclude<ChunkStatus, 'done' | 'cancelled' | 'error'>;
85
+ readonly results: undefined;
86
+ readonly error: undefined;
87
+ } | {
88
+ readonly status: 'done' | 'cancelled';
89
+ readonly results: readonly R[];
90
+ readonly error: undefined;
91
+ } | {
92
+ readonly status: 'error';
93
+ readonly results: readonly R[];
94
+ readonly error: unknown;
95
+ });
65
96
  /**
66
97
  * Chunks a heavy, synchronous per-item computation over `items` so it never
67
98
  * blocks the main thread for longer than a time slice — the thing that
@@ -98,12 +129,23 @@ interface UseDeferredTaskOptions {
98
129
  readonly onSlice?: (durationMs: number, segmentsDone: number) => void;
99
130
  /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
100
131
  readonly onYield?: () => void;
132
+ /**
133
+ * External cancellation source — aborting it is equivalent to calling
134
+ * `cancel()`, and its `reason` comes through on `reason` below, so a
135
+ * cancellation caused by the signal is distinguishable from a direct
136
+ * `cancel()` call. A new reference restarts the task the same way a new
137
+ * entry in `deps` does, so memoize it unless you mean to restart on
138
+ * every render.
139
+ */
140
+ readonly signal?: AbortSignal;
101
141
  }
102
- interface UseDeferredTaskState<T> {
103
- readonly status: ChunkStatus;
104
- readonly error: unknown;
105
- /** The returned value once the task settles with `done` — `undefined` until then. */
106
- readonly value: T | undefined;
142
+ /** Fields every status shares, independent of the `status`/`value`/`error` narrowing below. */
143
+ interface UseDeferredTaskShared {
144
+ /**
145
+ * The `signal` option's `reason`, present only when cancellation came
146
+ * from that signal aborting rather than a direct `cancel()` call.
147
+ */
148
+ readonly reason: unknown;
107
149
  readonly isIdle: boolean;
108
150
  readonly isProcessing: boolean;
109
151
  readonly isPaused: boolean;
@@ -115,6 +157,25 @@ interface UseDeferredTaskState<T> {
115
157
  readonly resume: () => void;
116
158
  readonly cancel: () => void;
117
159
  }
160
+ /**
161
+ * Discriminated on `status`. Narrowing to `'done'` narrows `value` to `T`
162
+ * itself — no `=== undefined` check needed alongside `isDone`, even when
163
+ * `T` allows `undefined` as a genuine return value. Narrowing to `'error'`
164
+ * narrows `error` to defined instead.
165
+ */
166
+ type UseDeferredTaskState<T> = UseDeferredTaskShared & ({
167
+ readonly status: Exclude<ChunkStatus, 'done' | 'error'>;
168
+ readonly value: undefined;
169
+ readonly error: undefined;
170
+ } | {
171
+ readonly status: 'done';
172
+ readonly value: T;
173
+ readonly error: undefined;
174
+ } | {
175
+ readonly status: 'error';
176
+ readonly value: undefined;
177
+ readonly error: unknown;
178
+ });
118
179
  /**
119
180
  * Runs an arbitrary heavy, synchronous generator function in chunks, so it
120
181
  * never produces a long task on its own. Where `useChunkedTask` chunks "one
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"mappings":";UAeiB,sBAAsB,GAAG;;WAE/B;;;;;;;WAOA;;;;;;WAMA;;WAEA;;;;;;;WAOA,UAAU,QAAQ,GAAG,MAAM,GAAG;;;;;;;WAO9B,WAAW,gBAAgB,MAAM,GAAG;;;;;;;;WAQpC,WAAW,oBAAoB;;WAE/B;;UAGM,oBAAoB;WAC1B,QAAQ;WACR;WACA;WACA;;WAEA,kBAAkB;;WAElB,kBAAkB;WAClB;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;;;;;;;;;wBAoBK,eAAe,GAAG,GAChC,gBAAgB,KAChB,cAAc,MAAM,GAAG,kBAAkB,GACzC,UAAU,sBAAsB,GAAG,KAClC,oBAAoB;UCzFN;;WAEN;;;;;;WAMA;;WAEA;;;;;;;WAOA,WAAW,oBAAoB;;WAE/B;;UAGM,qBAAqB;WAC3B,QAAQ;WACR;;WAEA,OAAO;WACP;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;;;;;wBAgBK,gBAAgB,GAC9B,YAAY,kBAAkB,IAC9B,0BACA,UAAU,yBACT,qBAAqB"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"mappings":";UAeiB,sBAAsB,GAAG;;WAE/B;;;;;;;WAOA;;;;;;WAMA;;WAEA;;;;;;;WAOA,UAAU,QAAQ,GAAG,MAAM,GAAG;;;;;;;WAO9B,WAAW,gBAAgB,MAAM,GAAG;;;;;;;;WAQpC,WAAW,oBAAoB;;WAE/B;;;;;;;;;WASA,SAAS;;;UAIV;WACC;WACA;;WAEA,kBAAkB;;;;;WAKlB;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;;KAUC,oBAAoB,KAAK;WAGpB,QAAQ,QAAQ;WAChB;WACA;;WAGA;WACA,kBAAkB;WAClB;;WAGA;WACA,kBAAkB;WAClB;;;;;;;;;;;;;;;;;wBAwBD,eAAe,GAAG,GAChC,gBAAgB,KAChB,cAAc,MAAM,GAAG,kBAAkB,GACzC,UAAU,sBAAsB,GAAG,KAClC,oBAAoB;UCjIN;;WAEN;;;;;;WAMA;;WAEA;;;;;;;WAOA,WAAW,oBAAoB;;WAE/B;;;;;;;;;WASA,SAAS;;;UAIV;;;;;WAKC;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;;KASC,qBAAqB,KAAK;WAGrB,QAAQ,QAAQ;WAChB;WACA;;WAEA;WAAyB,OAAO;WAAY;;WAC5C;WAA0B;WAA2B;;;;;;;;;;;;;wBAkBtD,gBAAgB,GAC9B,YAAY,kBAAkB,IAC9B,0BACA,UAAU,yBACT,qBAAqB"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as createChunkScheduler, t as createDeferredTask } from "./deferred-task-6oTEnHWa.js";
1
+ import { n as createChunkScheduler, t as createDeferredTask } from "./deferred-task-Cai4fF4m.js";
2
2
  import * as React from "react";
3
3
  import { useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
4
4
  // src/react/task-controller.ts
@@ -55,7 +55,7 @@ const IDLE_STATUS = "idle";
55
55
  * same as any other React dependency.
56
56
  */
57
57
  function useChunkedTask(items, processItem, options) {
58
- const { timeSliceMs, batchSize, collectResults, autoStart = true, onItem, onError, onSlice, onYield } = options ?? {};
58
+ const { timeSliceMs, batchSize, collectResults, autoStart = true, onItem, onError, onSlice, onYield, signal } = options ?? {};
59
59
  const stableProcessItem = useEffectEvent((item, index) => processItem(item, index));
60
60
  const stableOnItem = useEffectEvent((result, item, index) => {
61
61
  onItem?.(result, item, index);
@@ -84,7 +84,8 @@ function useChunkedTask(items, processItem, options) {
84
84
  onYield: stableOnYield,
85
85
  ...timeSliceMs !== void 0 ? { timeSliceMs } : {},
86
86
  ...batchSize !== void 0 ? { batchSize } : {},
87
- ...collectResults !== void 0 ? { collectResults } : {}
87
+ ...collectResults !== void 0 ? { collectResults } : {},
88
+ ...signal !== void 0 ? { signal } : {}
88
89
  });
89
90
  controller.replace(scheduler);
90
91
  if (autoStart) scheduler.start();
@@ -98,6 +99,7 @@ function useChunkedTask(items, processItem, options) {
98
99
  batchSize,
99
100
  collectResults,
100
101
  autoStart,
102
+ signal,
101
103
  controller
102
104
  ]);
103
105
  const start = useCallback(() => controller.getCurrent()?.start(), [controller]);
@@ -111,6 +113,7 @@ function useChunkedTask(items, processItem, options) {
111
113
  error: snapshot.error,
112
114
  results: snapshot.results,
113
115
  skipped: snapshot.skipped ?? NO_SKIPPED,
116
+ reason: snapshot.reason,
114
117
  isIdle: snapshot.status === "idle",
115
118
  isProcessing: snapshot.status === "processing",
116
119
  isPaused: snapshot.status === "paused",
@@ -137,7 +140,7 @@ const IDLE_SNAPSHOT = { status: "idle" };
137
140
  * you would for `useMemo` or `useEffect`.
138
141
  */
139
142
  function useDeferredTask(task, deps, options) {
140
- const { timeSliceMs, batchSize, autoStart = true, onSlice, onYield } = options ?? {};
143
+ const { timeSliceMs, batchSize, autoStart = true, onSlice, onYield, signal } = options ?? {};
141
144
  const stableTask = useEffectEvent(() => task());
142
145
  const stableOnSlice = useEffectEvent((durationMs, segmentsDone) => {
143
146
  onSlice?.(durationMs, segmentsDone);
@@ -153,7 +156,8 @@ function useDeferredTask(task, deps, options) {
153
156
  onSlice: stableOnSlice,
154
157
  onYield: stableOnYield,
155
158
  ...timeSliceMs !== void 0 ? { timeSliceMs } : {},
156
- ...batchSize !== void 0 ? { batchSize } : {}
159
+ ...batchSize !== void 0 ? { batchSize } : {},
160
+ ...signal !== void 0 ? { signal } : {}
157
161
  });
158
162
  controller.replace(deferredTask);
159
163
  if (autoStart) deferredTask.start();
@@ -166,6 +170,7 @@ function useDeferredTask(task, deps, options) {
166
170
  timeSliceMs,
167
171
  batchSize,
168
172
  autoStart,
173
+ signal,
169
174
  controller
170
175
  ]);
171
176
  const start = useCallback(() => controller.getCurrent()?.start(), [controller]);
@@ -176,6 +181,7 @@ function useDeferredTask(task, deps, options) {
176
181
  status: snapshot.status,
177
182
  error: snapshot.error,
178
183
  value: snapshot.value,
184
+ reason: snapshot.reason,
179
185
  isIdle: snapshot.status === "idle",
180
186
  isProcessing: snapshot.status === "processing",
181
187
  isPaused: snapshot.status === "paused",
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/react/task-controller.ts","../src/react/use-effect-event.ts","../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"sourcesContent":["/**\n * The glue between a task handle (`ChunkScheduler<R>`, `DeferredTask<T>` —\n * anything with `subscribe`/`getSnapshot`) recreated from scratch whenever a\n * hook's deps change, inside an effect, and `useSyncExternalStore`, which\n * wants one stable `subscribe` function for a component's whole lifetime.\n *\n * `useSyncExternalStore` resubscribes whenever the `subscribe` function\n * identity changes — fine for correctness, but it means the naive approach\n * (pass the current task's own `.subscribe` directly) either churns\n * subscriptions every render or, worse, drifts: the component keeps\n * `subscribe`d to whichever task was current when it last resubscribed, not\n * necessarily the one `getSnapshot` is reading from after a swap.\n *\n * A controller sidesteps that: it's created once per hook instance (lazy\n * `useState` initializer in the hook, so it's stable without touching a\n * ref), hands `useSyncExternalStore` a `subscribe`/`getSnapshot` pair that\n * never change identity, and internally forwards to whichever task is\n * \"current\" — re-pointing itself via `replace()` every time the effect\n * swaps in a new one. Generic over the task shape so both `useChunkedTask`\n * and `useDeferredTask` share this one implementation.\n *\n * Every property below is an arrow function, deliberately — not method\n * shorthand — so none of them close over `this`, and `controller.subscribe`\n * can be handed to `useSyncExternalStore` directly as a plain function\n * reference without an unbound-method footgun.\n */\nexport interface SubscribableTask<Snapshot> {\n subscribe(listener: () => void): () => void;\n getSnapshot(): Snapshot;\n}\n\nexport interface TaskController<Task extends SubscribableTask<unknown>> {\n subscribe: (listener: () => void) => () => void;\n getSnapshot: () => ReturnType<Task['getSnapshot']> | undefined;\n getCurrent: () => Task | null;\n /** Swaps in a new task (or none), detaching from the previous one first. */\n replace: (task: Task | null) => void;\n}\n\nexport function createTaskController<\n Task extends SubscribableTask<unknown>,\n>(): TaskController<Task> {\n let current: Task | null = null;\n let detachFromCurrent: (() => void) | undefined;\n const listeners = new Set<() => void>();\n\n const notify = (): void => {\n for (const listener of listeners) listener();\n };\n\n return {\n subscribe: (listener) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n getSnapshot: () =>\n current?.getSnapshot() as ReturnType<Task['getSnapshot']> | undefined,\n getCurrent: () => current,\n replace: (task) => {\n detachFromCurrent?.();\n current = task;\n detachFromCurrent = task?.subscribe(notify);\n notify();\n },\n };\n}\n","import * as React from 'react';\nimport { useCallback, useInsertionEffect, useRef } from 'react';\n\n/**\n * `useEffectEvent` only became a stable React export in 19.2. On React 18\n * and 19.0–19.1 it doesn't exist at all — and a named\n * `import { useEffectEvent } from 'react'` then fails outright: at module\n * link time under Node ESM (\"Named export 'useEffectEvent' not found\"), or\n * as \"useEffectEvent is not a function\" on first render in a bundler. Both\n * versions are inside this package's declared peer range, so that has to\n * work, not just type-check.\n *\n * So the native hook is read as a property of the namespace object, where a\n * missing export is plain `undefined` rather than a link error, and used\n * whenever the running React has it. Otherwise the fallback keeps the same\n * contract: a stable function identity that always calls the latest\n * callback. The latest callback is committed from an insertion effect —\n * after render, before any layout or passive effect of the same commit can\n * observe it — so effects always see the current closure, without the\n * render-time ref mutation the React Compiler rejects.\n */\ntype EffectEventHook = <Args extends readonly unknown[], Return>(\n callback: (...args: Args) => Return,\n) => (...args: Args) => Return;\n\nexport function useEffectEventFallback<Args extends readonly unknown[], Return>(\n callback: (...args: Args) => Return,\n): (...args: Args) => Return {\n const latest = useRef(callback);\n useInsertionEffect(() => {\n latest.current = callback;\n });\n return useCallback((...args: Args) => latest.current(...args), []);\n}\n\n// Through `unknown` on purpose: React 18's types have no `useEffectEvent`\n// member at all, so a direct assertion wouldn't compile against them.\nconst nativeUseEffectEvent = (\n React as unknown as { readonly useEffectEvent?: EffectEventHook }\n).useEffectEvent;\n\nexport const useEffectEvent: EffectEventHook =\n nativeUseEffectEvent ?? useEffectEventFallback;\n","import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from 'react';\n\nimport type { ChunkStatus } from '../core/chunk-machine.js';\nimport { createChunkScheduler } from '../core/chunk-scheduler.js';\nimport type {\n ChunkScheduler,\n ChunkSnapshot,\n SkippedChunkItem,\n} from '../core/chunk-scheduler.js';\nimport { createTaskController } from './task-controller.js';\nimport { useEffectEvent } from './use-effect-event.js';\n\n/** Stable empty array so a never-skipped task doesn't hand out a fresh `[]` every render. */\nconst NO_SKIPPED: readonly SkippedChunkItem[] = [];\n\nexport interface UseChunkedTaskOptions<T, R> {\n /** Max ms of uninterrupted work before yielding. Default 5. */\n readonly timeSliceMs?: number;\n /**\n * Items to process between clock checks. Default 1. Raise it only for\n * items measured in nanoseconds, where reading the clock costs more than\n * the work; a slice can then run up to `batchSize` items past its budget.\n * See the `batchSize` note on `createChunkScheduler`.\n */\n readonly batchSize?: number;\n /**\n * Keep every result in `results`. Default true. Set false when results\n * are consumed through `onItem`, to avoid retaining one result per item;\n * `results` is then an empty array once the task settles.\n */\n readonly collectResults?: boolean;\n /** Start processing automatically when the task is (re)created. Default `true`. */\n readonly autoStart?: boolean;\n /**\n * Called synchronously after each item is processed. This is the cheap\n * way to consume results incrementally while the task is still running —\n * write into a ref-backed buffer, a canvas, a chart, whatever you need —\n * without paying for an array copy on every yield checkpoint.\n */\n readonly onItem?: (result: R, item: T, index: number) => void;\n /**\n * Called when `processItem` throws. Return `'skip'` to drop that item and\n * keep going — it shows up in `skipped` — or `'abort'` to end the task as\n * an error, carrying whatever was collected before the throw. Omitting\n * this defaults to `'abort'`, so existing callers see no behavior change.\n */\n readonly onError?: (error: unknown, item: T, index: number) => 'skip' | 'abort';\n /**\n * Called every time a slice ends and the task is about to yield, with how\n * long that slice ran and how many items are done so far. Ships in the\n * production build, unlike this package's own dev-only diagnostics — wire\n * it to your own telemetry to verify, on real traffic, that this task is\n * actually staying under `timeSliceMs`.\n */\n readonly onSlice?: (durationMs: number, processed: number) => void;\n /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */\n readonly onYield?: () => void;\n}\n\nexport interface UseChunkedTaskState<R> {\n readonly status: ChunkStatus;\n readonly processed: number;\n readonly total: number;\n readonly error: unknown;\n /** The full results array once the task has settled — `undefined` until then. */\n readonly results: readonly R[] | undefined;\n /** Items `onError` has skipped so far, growing live as skips happen. Empty when unused. */\n readonly skipped: readonly SkippedChunkItem[];\n readonly isIdle: boolean;\n readonly isProcessing: boolean;\n readonly isPaused: boolean;\n readonly isDone: boolean;\n readonly isCancelled: boolean;\n readonly isError: boolean;\n readonly start: () => void;\n readonly pause: () => void;\n readonly resume: () => void;\n readonly cancel: () => void;\n}\n\nconst IDLE_STATUS: ChunkStatus = 'idle';\n\n/**\n * Chunks a heavy, synchronous per-item computation over `items` so it never\n * blocks the main thread for longer than a time slice — the thing that\n * costs you Interaction to Next Paint. Starts automatically by default;\n * pass `{ autoStart: false }` to wait for an explicit `start()` call (e.g.\n * behind a button).\n *\n * `processItem` and `onItem` can be fresh closures every render — the hook\n * always calls the latest version (via `useEffectEvent`) without that\n * causing the task to restart. What *does* restart the task is `items`\n * changing identity (a new array reference), or `timeSliceMs`/`autoStart`\n * changing. That's deliberate: a new `items` array means new work, and\n * memoizing it when you don't want that is the caller's call to make, the\n * same as any other React dependency.\n */\nexport function useChunkedTask<T, R>(\n items: readonly T[],\n processItem: (item: T, index: number) => R,\n options?: UseChunkedTaskOptions<T, R>,\n): UseChunkedTaskState<R> {\n const {\n timeSliceMs,\n batchSize,\n collectResults,\n autoStart = true,\n onItem,\n onError,\n onSlice,\n onYield,\n } = options ?? {};\n\n // Stable identity, always runs the latest processItem/onItem passed this\n // render — the React-Compiler-safe replacement for a ref mutated during\n // render. Effect Events are meant to be called from inside an effect\n // (here: from the scheduler's async loop, which the effect below owns),\n // never during render itself.\n const stableProcessItem = useEffectEvent((item: T, index: number) =>\n processItem(item, index),\n );\n const stableOnItem = useEffectEvent((result: R, item: T, index: number) => {\n onItem?.(result, item, index);\n });\n const stableOnError = useEffectEvent(\n (error: unknown, item: T, index: number): 'skip' | 'abort' =>\n onError?.(error, item, index) ?? 'abort',\n );\n const stableOnSlice = useEffectEvent((durationMs: number, processed: number) => {\n onSlice?.(durationMs, processed);\n });\n const stableOnYield = useEffectEvent(() => {\n onYield?.();\n });\n\n // Created once, lazily, via useState rather than a ref mutated during\n // render — the controller itself is inert (no subscriptions, no timers)\n // until an effect calls replace() on it, so a discarded render (double\n // render in dev, or one React throws away) never leaks anything.\n const [controller] = useState(() => createTaskController<ChunkScheduler<R>>());\n\n // What getSnapshot() reports before the effect below has ever run —\n // including during SSR, where effects never run at all.\n const idleSnapshot = useMemo<ChunkSnapshot<R>>(\n () => ({ status: IDLE_STATUS, processed: 0, total: items.length }),\n [items.length],\n );\n\n const snapshot = useSyncExternalStore(\n controller.subscribe,\n () => controller.getSnapshot() ?? idleSnapshot,\n () => idleSnapshot,\n );\n\n useEffect(() => {\n const scheduler = createChunkScheduler<T, R>({\n items,\n processItem: stableProcessItem,\n onItem: stableOnItem,\n onError: stableOnError,\n onSlice: stableOnSlice,\n onYield: stableOnYield,\n ...(timeSliceMs !== undefined ? { timeSliceMs } : {}),\n ...(batchSize !== undefined ? { batchSize } : {}),\n ...(collectResults !== undefined ? { collectResults } : {}),\n });\n controller.replace(scheduler);\n if (autoStart) scheduler.start();\n\n return () => {\n // Safe even if the task already finished on its own — cancel() on a\n // terminal scheduler is a documented no-op, not an error.\n scheduler.cancel();\n controller.replace(null);\n };\n // stableProcessItem/stableOnItem/stableOnError/stableOnSlice/stableOnYield\n // are Effect Events: stable by construction, intentionally excluded\n // from the reactive dependency set\n // (that's the whole point of useEffectEvent) rather than omitted by\n // oversight.\n }, [items, timeSliceMs, batchSize, collectResults, autoStart, controller]);\n\n const start = useCallback(() => controller.getCurrent()?.start(), [controller]);\n const pause = useCallback(() => controller.getCurrent()?.pause(), [controller]);\n const resume = useCallback(() => controller.getCurrent()?.resume(), [controller]);\n const cancel = useCallback(() => controller.getCurrent()?.cancel(), [controller]);\n\n return {\n status: snapshot.status,\n processed: snapshot.processed,\n total: snapshot.total,\n error: snapshot.error,\n // From the snapshot, not a direct read of the scheduler: the engine\n // publishes results before it announces a terminal status, so the two\n // always arrive together and a concurrent render can never tear them\n // apart.\n results: snapshot.results,\n skipped: snapshot.skipped ?? NO_SKIPPED,\n isIdle: snapshot.status === 'idle',\n isProcessing: snapshot.status === 'processing',\n isPaused: snapshot.status === 'paused',\n isDone: snapshot.status === 'done',\n isCancelled: snapshot.status === 'cancelled',\n isError: snapshot.status === 'error',\n start,\n pause,\n resume,\n cancel,\n };\n}\n","import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';\n\nimport type { ChunkStatus } from '../core/chunk-machine.js';\nimport { createDeferredTask } from '../core/deferred-task.js';\nimport type {\n DeferredGenerator,\n DeferredTask,\n DeferredTaskSnapshot,\n} from '../core/deferred-task.js';\nimport { createTaskController } from './task-controller.js';\nimport { useEffectEvent } from './use-effect-event.js';\n\nexport interface UseDeferredTaskOptions {\n /** Max ms of uninterrupted work before yielding. Default 5. */\n readonly timeSliceMs?: number;\n /**\n * Generator segments to run between clock checks. Default 1. Raise it\n * only for segments measured in nanoseconds; a slice can then run up to\n * `batchSize` segments past its budget.\n */\n readonly batchSize?: number;\n /** Start processing automatically when the task is (re)created. Default `true`. */\n readonly autoStart?: boolean;\n /**\n * Called every time a segment-batch ends and the task is about to yield,\n * with how long that slice ran and how many segments are done so far.\n * Ships in the production build — see the identical option on\n * `useChunkedTask`/`createChunkScheduler` for why.\n */\n readonly onSlice?: (durationMs: number, segmentsDone: number) => void;\n /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */\n readonly onYield?: () => void;\n}\n\nexport interface UseDeferredTaskState<T> {\n readonly status: ChunkStatus;\n readonly error: unknown;\n /** The returned value once the task settles with `done` — `undefined` until then. */\n readonly value: T | undefined;\n readonly isIdle: boolean;\n readonly isProcessing: boolean;\n readonly isPaused: boolean;\n readonly isDone: boolean;\n readonly isCancelled: boolean;\n readonly isError: boolean;\n readonly start: () => void;\n readonly pause: () => void;\n readonly resume: () => void;\n readonly cancel: () => void;\n}\n\nconst IDLE_SNAPSHOT: DeferredTaskSnapshot<never> = { status: 'idle' };\n\n/**\n * Runs an arbitrary heavy, synchronous generator function in chunks, so it\n * never produces a long task on its own. Where `useChunkedTask` chunks \"one\n * function applied to every item of an array\" automatically, this hook\n * leaves the chunk boundaries to you — put `yield` wherever your function\n * can safely pause (see `DeferredGenerator`'s doc comment for an example).\n *\n * `task` is only called when the effect (re)creates the underlying task, not\n * on every render — list whatever it closes over in `deps`, the same way\n * you would for `useMemo` or `useEffect`.\n */\nexport function useDeferredTask<T>(\n task: () => DeferredGenerator<T>,\n deps: readonly unknown[],\n options?: UseDeferredTaskOptions,\n): UseDeferredTaskState<T> {\n const { timeSliceMs, batchSize, autoStart = true, onSlice, onYield } = options ?? {};\n\n // Stable identity, always runs the latest `task` passed this render —\n // see useChunkedTask.ts for why this is useEffectEvent rather than a ref\n // mutated during render.\n const stableTask = useEffectEvent(() => task());\n const stableOnSlice = useEffectEvent((durationMs: number, segmentsDone: number) => {\n onSlice?.(durationMs, segmentsDone);\n });\n const stableOnYield = useEffectEvent(() => {\n onYield?.();\n });\n\n const [controller] = useState(() => createTaskController<DeferredTask<T>>());\n\n const snapshot = useSyncExternalStore(\n controller.subscribe,\n () => controller.getSnapshot() ?? IDLE_SNAPSHOT,\n () => IDLE_SNAPSHOT,\n );\n\n useEffect(\n () => {\n const deferredTask = createDeferredTask<T>({\n task: stableTask,\n onSlice: stableOnSlice,\n onYield: stableOnYield,\n ...(timeSliceMs !== undefined ? { timeSliceMs } : {}),\n ...(batchSize !== undefined ? { batchSize } : {}),\n });\n controller.replace(deferredTask);\n if (autoStart) deferredTask.start();\n\n return () => {\n deferredTask.cancel();\n controller.replace(null);\n };\n },\n // `deps` plays the role `items` plays for useChunkedTask: it's the\n // caller's declaration of \"this is new work\". `stableTask` is an Effect\n // Event — stable by construction, intentionally excluded from the\n // reactive dependency set rather than omitted by oversight. `deps` is a\n // caller-supplied array, the same pattern (and the same reason it can't\n // be statically verified) as useMemo/useEffect's own deps parameter.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [...deps, timeSliceMs, batchSize, autoStart, controller],\n );\n\n const start = useCallback(() => controller.getCurrent()?.start(), [controller]);\n const pause = useCallback(() => controller.getCurrent()?.pause(), [controller]);\n const resume = useCallback(() => controller.getCurrent()?.resume(), [controller]);\n const cancel = useCallback(() => controller.getCurrent()?.cancel(), [controller]);\n\n return {\n status: snapshot.status,\n error: snapshot.error,\n // From the snapshot: the engine publishes the value before it\n // announces `done`, so a concurrent render can never see one without\n // the other. See useChunkedTask.ts.\n value: snapshot.value,\n isIdle: snapshot.status === 'idle',\n isProcessing: snapshot.status === 'processing',\n isPaused: snapshot.status === 'paused',\n isDone: snapshot.status === 'done',\n isCancelled: snapshot.status === 'cancelled',\n isError: snapshot.status === 'error',\n start,\n pause,\n resume,\n cancel,\n };\n}\n"],"mappings":";;;;AAuCA,SAAgB,uBAEU;CACxB,IAAI,UAAuB;CAC3B,IAAI;CACJ,MAAM,4BAAY,IAAI,IAAgB;CAEtC,MAAM,eAAqB;EACzB,KAAK,MAAM,YAAY,WAAW,SAAS;CAC7C;CAEA,OAAO;EACL,YAAY,aAAa;GACvB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EACA,mBACE,SAAS,YAAY;EACvB,kBAAkB;EAClB,UAAU,SAAS;GACjB,oBAAoB;GACpB,UAAU;GACV,o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AAC;CAED,MAAM,CAAC,cAAc,eAAe,qBAAsC,CAAC;CAE3E,MAAM,WAAW,qBACf,WAAW,iBACL,WAAW,YAAY,KAAK,qBAC5B,aACR;CAEA,gBACQ;EACJ,MAAM,eAAe,mBAAsB;GACzC,MAAM;GACN,SAAS;GACT,SAAS;GACT,GAAI,gBAAgB,KAAA,IAAY,EAAE,YAAY,IAAI,CAAC;GACnD,GAAI,cAAc,KAAA,IAAY,EAAE,UAAU,IAAI,CAAC;EACjD,CAAC;EACD,WAAW,QAAQ,YAAY;EAC/B,IAAI,WAAW,aAAa,MAAM;EAElC,aAAa;GACX,aAAa,OAAO;GACpB,WAAW,QAAQ,IAAI;EACzB;CACF,GAQA;EAAC,GAAG;EAAM;EAAa;EAAW;EAAW;CAAU,CACzD;CAEA,MAAM,QAAQ,kBAAkB,WAAW,WAAW,CAAC,EAAE,MAAM,GAAG,CAAC,UAAU,CAAC;CAC9E,MAAM,QAAQ,kBAAkB,WAAW,WAAW,CAAC,EAAE,MAAM,GAAG,CAAC,UAAU,CAAC;CAC9E,MAAM,SAAS,kBAAkB,WAAW,WAAW,CAAC,EAAE,OAAO,GAAG,CAAC,UAAU,CAAC;CAChF,MAAM,SAAS,kBAAkB,WAAW,WAAW,CAAC,EAAE,OAAO,GAAG,CAAC,UAAU,CAAC;CAEhF,OAAO;EACL,QAAQ,SAAS;EACjB,OAAO,SAAS;EAIhB,OAAO,SAAS;EAChB,QAAQ,SAAS,WAAW;EAC5B,cAAc,SAAS,WAAW;EAClC,UAAU,SAAS,WAAW;EAC9B,QAAQ,SAAS,WAAW;EAC5B,aAAa,SAAS,WAAW;EACjC,SAAS,SAAS,WAAW;EAC7B;EACA;EACA;EACA;CACF;AACF"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/react/task-controller.ts","../src/react/use-effect-event.ts","../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"sourcesContent":["/**\n * The glue between a task handle (`ChunkScheduler<R>`, `DeferredTask<T>` —\n * anything with `subscribe`/`getSnapshot`) recreated from scratch whenever a\n * hook's deps change, inside an effect, and `useSyncExternalStore`, which\n * wants one stable `subscribe` function for a component's whole lifetime.\n *\n * `useSyncExternalStore` resubscribes whenever the `subscribe` function\n * identity changes — fine for correctness, but it means the naive approach\n * (pass the current task's own `.subscribe` directly) either churns\n * subscriptions every render or, worse, drifts: the component keeps\n * `subscribe`d to whichever task was current when it last resubscribed, not\n * necessarily the one `getSnapshot` is reading from after a swap.\n *\n * A controller sidesteps that: it's created once per hook instance (lazy\n * `useState` initializer in the hook, so it's stable without touching a\n * ref), hands `useSyncExternalStore` a `subscribe`/`getSnapshot` pair that\n * never change identity, and internally forwards to whichever task is\n * \"current\" — re-pointing itself via `replace()` every time the effect\n * swaps in a new one. Generic over the task shape so both `useChunkedTask`\n * and `useDeferredTask` share this one implementation.\n *\n * Every property below is an arrow function, deliberately — not method\n * shorthand — so none of them close over `this`, and `controller.subscribe`\n * can be handed to `useSyncExternalStore` directly as a plain function\n * reference without an unbound-method footgun.\n */\nexport interface SubscribableTask<Snapshot> {\n subscribe(listener: () => void): () => void;\n getSnapshot(): Snapshot;\n}\n\nexport interface TaskController<Task extends SubscribableTask<unknown>> {\n subscribe: (listener: () => void) => () => void;\n getSnapshot: () => ReturnType<Task['getSnapshot']> | undefined;\n getCurrent: () => Task | null;\n /** Swaps in a new task (or none), detaching from the previous one first. */\n replace: (task: Task | null) => void;\n}\n\nexport function createTaskController<\n Task extends SubscribableTask<unknown>,\n>(): TaskController<Task> {\n let current: Task | null = null;\n let detachFromCurrent: (() => void) | undefined;\n const listeners = new Set<() => void>();\n\n const notify = (): void => {\n for (const listener of listeners) listener();\n };\n\n return {\n subscribe: (listener) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n getSnapshot: () =>\n current?.getSnapshot() as ReturnType<Task['getSnapshot']> | undefined,\n getCurrent: () => current,\n replace: (task) => {\n detachFromCurrent?.();\n current = task;\n detachFromCurrent = task?.subscribe(notify);\n notify();\n },\n };\n}\n","import * as React from 'react';\nimport { useCallback, useInsertionEffect, useRef } from 'react';\n\n/**\n * `useEffectEvent` only became a stable React export in 19.2. On React 18\n * and 19.0–19.1 it doesn't exist at all — and a named\n * `import { useEffectEvent } from 'react'` then fails outright: at module\n * link time under Node ESM (\"Named export 'useEffectEvent' not found\"), or\n * as \"useEffectEvent is not a function\" on first render in a bundler. Both\n * versions are inside this package's declared peer range, so that has to\n * work, not just type-check.\n *\n * So the native hook is read as a property of the namespace object, where a\n * missing export is plain `undefined` rather than a link error, and used\n * whenever the running React has it. Otherwise the fallback keeps the same\n * contract: a stable function identity that always calls the latest\n * callback. The latest callback is committed from an insertion effect —\n * after render, before any layout or passive effect of the same commit can\n * observe it — so effects always see the current closure, without the\n * render-time ref mutation the React Compiler rejects.\n */\ntype EffectEventHook = <Args extends readonly unknown[], Return>(\n callback: (...args: Args) => Return,\n) => (...args: Args) => Return;\n\nexport function useEffectEventFallback<Args extends readonly unknown[], Return>(\n callback: (...args: Args) => Return,\n): (...args: Args) => Return {\n const latest = useRef(callback);\n useInsertionEffect(() => {\n latest.current = callback;\n });\n return useCallback((...args: Args) => latest.current(...args), []);\n}\n\n// Through `unknown` on purpose: React 18's types have no `useEffectEvent`\n// member at all, so a direct assertion wouldn't compile against them.\nconst nativeUseEffectEvent = (\n React as unknown as { readonly useEffectEvent?: EffectEventHook }\n).useEffectEvent;\n\nexport const useEffectEvent: EffectEventHook =\n nativeUseEffectEvent ?? useEffectEventFallback;\n","import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from 'react';\n\nimport type { ChunkStatus } from '../core/chunk-machine.js';\nimport { createChunkScheduler } from '../core/chunk-scheduler.js';\nimport type {\n ChunkScheduler,\n ChunkSnapshot,\n SkippedChunkItem,\n} from '../core/chunk-scheduler.js';\nimport { createTaskController } from './task-controller.js';\nimport { useEffectEvent } from './use-effect-event.js';\n\n/** Stable empty array so a never-skipped task doesn't hand out a fresh `[]` every render. */\nconst NO_SKIPPED: readonly SkippedChunkItem[] = [];\n\nexport interface UseChunkedTaskOptions<T, R> {\n /** Max ms of uninterrupted work before yielding. Default 5. */\n readonly timeSliceMs?: number;\n /**\n * Items to process between clock checks. Default 1. Raise it only for\n * items measured in nanoseconds, where reading the clock costs more than\n * the work; a slice can then run up to `batchSize` items past its budget.\n * See the `batchSize` note on `createChunkScheduler`.\n */\n readonly batchSize?: number;\n /**\n * Keep every result in `results`. Default true. Set false when results\n * are consumed through `onItem`, to avoid retaining one result per item;\n * `results` is then an empty array once the task settles.\n */\n readonly collectResults?: boolean;\n /** Start processing automatically when the task is (re)created. Default `true`. */\n readonly autoStart?: boolean;\n /**\n * Called synchronously after each item is processed. This is the cheap\n * way to consume results incrementally while the task is still running —\n * write into a ref-backed buffer, a canvas, a chart, whatever you need —\n * without paying for an array copy on every yield checkpoint.\n */\n readonly onItem?: (result: R, item: T, index: number) => void;\n /**\n * Called when `processItem` throws. Return `'skip'` to drop that item and\n * keep going — it shows up in `skipped` — or `'abort'` to end the task as\n * an error, carrying whatever was collected before the throw. Omitting\n * this defaults to `'abort'`, so existing callers see no behavior change.\n */\n readonly onError?: (error: unknown, item: T, index: number) => 'skip' | 'abort';\n /**\n * Called every time a slice ends and the task is about to yield, with how\n * long that slice ran and how many items are done so far. Ships in the\n * production build, unlike this package's own dev-only diagnostics — wire\n * it to your own telemetry to verify, on real traffic, that this task is\n * actually staying under `timeSliceMs`.\n */\n readonly onSlice?: (durationMs: number, processed: number) => void;\n /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */\n readonly onYield?: () => void;\n /**\n * External cancellation source — aborting it is equivalent to calling\n * `cancel()`, and its `reason` comes through on `reason` below, so a\n * cancellation caused by the signal is distinguishable from a direct\n * `cancel()` call. A new reference restarts the task the same way a new\n * `items` array does, so memoize it (e.g. from a ref or a parent-owned\n * `AbortController`) unless you mean to restart on every render.\n */\n readonly signal?: AbortSignal;\n}\n\n/** Fields every status shares, independent of the `status`/`results`/`error` narrowing below. */\ninterface UseChunkedTaskShared {\n readonly processed: number;\n readonly total: number;\n /** Items `onError` has skipped so far, growing live as skips happen. Empty when unused. */\n readonly skipped: readonly SkippedChunkItem[];\n /**\n * The `signal` option's `reason`, present only when cancellation came\n * from that signal aborting rather than a direct `cancel()` call.\n */\n readonly reason: unknown;\n readonly isIdle: boolean;\n readonly isProcessing: boolean;\n readonly isPaused: boolean;\n readonly isDone: boolean;\n readonly isCancelled: boolean;\n readonly isError: boolean;\n readonly start: () => void;\n readonly pause: () => void;\n readonly resume: () => void;\n readonly cancel: () => void;\n}\n\n/**\n * Discriminated on `status`. Narrowing to `'done'` or `'cancelled'` narrows\n * `results` to a defined array, no `=== undefined` check needed alongside\n * `isDone`; narrowing to `'error'` narrows `error` to defined instead. The\n * `is*` booleans above remain the simple way to branch when you only care\n * about one status and don't need the narrowing.\n */\nexport type UseChunkedTaskState<R> = UseChunkedTaskShared &\n (\n | {\n readonly status: Exclude<ChunkStatus, 'done' | 'cancelled' | 'error'>;\n readonly results: undefined;\n readonly error: undefined;\n }\n | {\n readonly status: 'done' | 'cancelled';\n readonly results: readonly R[];\n readonly error: undefined;\n }\n | {\n readonly status: 'error';\n readonly results: readonly R[];\n readonly error: unknown;\n }\n );\n\n// Narrowed to the literal, not widened to `ChunkStatus`, so the object\n// literal below satisfies the discriminated `ChunkSnapshot` union on its\n// own merits — no assertion needed for a status this function knows for sure.\nconst IDLE_STATUS = 'idle';\n\n/**\n * Chunks a heavy, synchronous per-item computation over `items` so it never\n * blocks the main thread for longer than a time slice — the thing that\n * costs you Interaction to Next Paint. Starts automatically by default;\n * pass `{ autoStart: false }` to wait for an explicit `start()` call (e.g.\n * behind a button).\n *\n * `processItem` and `onItem` can be fresh closures every render — the hook\n * always calls the latest version (via `useEffectEvent`) without that\n * causing the task to restart. What *does* restart the task is `items`\n * changing identity (a new array reference), or `timeSliceMs`/`autoStart`\n * changing. That's deliberate: a new `items` array means new work, and\n * memoizing it when you don't want that is the caller's call to make, the\n * same as any other React dependency.\n */\nexport function useChunkedTask<T, R>(\n items: readonly T[],\n processItem: (item: T, index: number) => R,\n options?: UseChunkedTaskOptions<T, R>,\n): UseChunkedTaskState<R> {\n const {\n timeSliceMs,\n batchSize,\n collectResults,\n autoStart = true,\n onItem,\n onError,\n onSlice,\n onYield,\n signal,\n } = options ?? {};\n\n // Stable identity, always runs the latest processItem/onItem passed this\n // render — the React-Compiler-safe replacement for a ref mutated during\n // render. Effect Events are meant to be called from inside an effect\n // (here: from the scheduler's async loop, which the effect below owns),\n // never during render itself.\n const stableProcessItem = useEffectEvent((item: T, index: number) =>\n processItem(item, index),\n );\n const stableOnItem = useEffectEvent((result: R, item: T, index: number) => {\n onItem?.(result, item, index);\n });\n const stableOnError = useEffectEvent(\n (error: unknown, item: T, index: number): 'skip' | 'abort' =>\n onError?.(error, item, index) ?? 'abort',\n );\n const stableOnSlice = useEffectEvent((durationMs: number, processed: number) => {\n onSlice?.(durationMs, processed);\n });\n const stableOnYield = useEffectEvent(() => {\n onYield?.();\n });\n\n // Created once, lazily, via useState rather than a ref mutated during\n // render — the controller itself is inert (no subscriptions, no timers)\n // until an effect calls replace() on it, so a discarded render (double\n // render in dev, or one React throws away) never leaks anything.\n const [controller] = useState(() => createTaskController<ChunkScheduler<R>>());\n\n // What getSnapshot() reports before the effect below has ever run —\n // including during SSR, where effects never run at all.\n const idleSnapshot = useMemo<ChunkSnapshot<R>>(\n () => ({ status: IDLE_STATUS, processed: 0, total: items.length }),\n [items.length],\n );\n\n const snapshot = useSyncExternalStore(\n controller.subscribe,\n () => controller.getSnapshot() ?? idleSnapshot,\n () => idleSnapshot,\n );\n\n useEffect(() => {\n const scheduler = createChunkScheduler<T, R>({\n items,\n processItem: stableProcessItem,\n onItem: stableOnItem,\n onError: stableOnError,\n onSlice: stableOnSlice,\n onYield: stableOnYield,\n ...(timeSliceMs !== undefined ? { timeSliceMs } : {}),\n ...(batchSize !== undefined ? { batchSize } : {}),\n ...(collectResults !== undefined ? { collectResults } : {}),\n ...(signal !== undefined ? { signal } : {}),\n });\n controller.replace(scheduler);\n if (autoStart) scheduler.start();\n\n return () => {\n // Safe even if the task already finished on its own — cancel() on a\n // terminal scheduler is a documented no-op, not an error.\n scheduler.cancel();\n controller.replace(null);\n };\n // stableProcessItem/stableOnItem/stableOnError/stableOnSlice/stableOnYield\n // are Effect Events: stable by construction, intentionally excluded\n // from the reactive dependency set\n // (that's the whole point of useEffectEvent) rather than omitted by\n // oversight.\n }, [items, timeSliceMs, batchSize, collectResults, autoStart, signal, controller]);\n\n const start = useCallback(() => controller.getCurrent()?.start(), [controller]);\n const pause = useCallback(() => controller.getCurrent()?.pause(), [controller]);\n const resume = useCallback(() => controller.getCurrent()?.resume(), [controller]);\n const cancel = useCallback(() => controller.getCurrent()?.cancel(), [controller]);\n\n // Built from `snapshot`'s own wide-`status` fields, so — same as the core\n // engine this wraps — it can't be checked structurally against the\n // discriminated union above; `snapshot` already carries that invariant.\n return {\n status: snapshot.status,\n processed: snapshot.processed,\n total: snapshot.total,\n error: snapshot.error,\n // From the snapshot, not a direct read of the scheduler: the engine\n // publishes results before it announces a terminal status, so the two\n // always arrive together and a concurrent render can never tear them\n // apart.\n results: snapshot.results,\n skipped: snapshot.skipped ?? NO_SKIPPED,\n reason: snapshot.reason,\n isIdle: snapshot.status === 'idle',\n isProcessing: snapshot.status === 'processing',\n isPaused: snapshot.status === 'paused',\n isDone: snapshot.status === 'done',\n isCancelled: snapshot.status === 'cancelled',\n isError: snapshot.status === 'error',\n start,\n pause,\n resume,\n cancel,\n } as UseChunkedTaskState<R>;\n}\n","import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';\n\nimport type { ChunkStatus } from '../core/chunk-machine.js';\nimport { createDeferredTask } from '../core/deferred-task.js';\nimport type {\n DeferredGenerator,\n DeferredTask,\n DeferredTaskSnapshot,\n} from '../core/deferred-task.js';\nimport { createTaskController } from './task-controller.js';\nimport { useEffectEvent } from './use-effect-event.js';\n\nexport interface UseDeferredTaskOptions {\n /** Max ms of uninterrupted work before yielding. Default 5. */\n readonly timeSliceMs?: number;\n /**\n * Generator segments to run between clock checks. Default 1. Raise it\n * only for segments measured in nanoseconds; a slice can then run up to\n * `batchSize` segments past its budget.\n */\n readonly batchSize?: number;\n /** Start processing automatically when the task is (re)created. Default `true`. */\n readonly autoStart?: boolean;\n /**\n * Called every time a segment-batch ends and the task is about to yield,\n * with how long that slice ran and how many segments are done so far.\n * Ships in the production build — see the identical option on\n * `useChunkedTask`/`createChunkScheduler` for why.\n */\n readonly onSlice?: (durationMs: number, segmentsDone: number) => void;\n /** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */\n readonly onYield?: () => void;\n /**\n * External cancellation source — aborting it is equivalent to calling\n * `cancel()`, and its `reason` comes through on `reason` below, so a\n * cancellation caused by the signal is distinguishable from a direct\n * `cancel()` call. A new reference restarts the task the same way a new\n * entry in `deps` does, so memoize it unless you mean to restart on\n * every render.\n */\n readonly signal?: AbortSignal;\n}\n\n/** Fields every status shares, independent of the `status`/`value`/`error` narrowing below. */\ninterface UseDeferredTaskShared {\n /**\n * The `signal` option's `reason`, present only when cancellation came\n * from that signal aborting rather than a direct `cancel()` call.\n */\n readonly reason: unknown;\n readonly isIdle: boolean;\n readonly isProcessing: boolean;\n readonly isPaused: boolean;\n readonly isDone: boolean;\n readonly isCancelled: boolean;\n readonly isError: boolean;\n readonly start: () => void;\n readonly pause: () => void;\n readonly resume: () => void;\n readonly cancel: () => void;\n}\n\n/**\n * Discriminated on `status`. Narrowing to `'done'` narrows `value` to `T`\n * itself — no `=== undefined` check needed alongside `isDone`, even when\n * `T` allows `undefined` as a genuine return value. Narrowing to `'error'`\n * narrows `error` to defined instead.\n */\nexport type UseDeferredTaskState<T> = UseDeferredTaskShared &\n (\n | {\n readonly status: Exclude<ChunkStatus, 'done' | 'error'>;\n readonly value: undefined;\n readonly error: undefined;\n }\n | { readonly status: 'done'; readonly value: T; readonly error: undefined }\n | { readonly status: 'error'; readonly value: undefined; readonly error: unknown }\n );\n\n// Narrowed to the literal, not widened to `ChunkStatus`, so this satisfies\n// the discriminated `DeferredTaskSnapshot` union on its own merits.\nconst IDLE_SNAPSHOT: DeferredTaskSnapshot<never> = { status: 'idle' };\n\n/**\n * Runs an arbitrary heavy, synchronous generator function in chunks, so it\n * never produces a long task on its own. Where `useChunkedTask` chunks \"one\n * function applied to every item of an array\" automatically, this hook\n * leaves the chunk boundaries to you — put `yield` wherever your function\n * can safely pause (see `DeferredGenerator`'s doc comment for an example).\n *\n * `task` is only called when the effect (re)creates the underlying task, not\n * on every render — list whatever it closes over in `deps`, the same way\n * you would for `useMemo` or `useEffect`.\n */\nexport function useDeferredTask<T>(\n task: () => DeferredGenerator<T>,\n deps: readonly unknown[],\n options?: UseDeferredTaskOptions,\n): UseDeferredTaskState<T> {\n const {\n timeSliceMs,\n batchSize,\n autoStart = true,\n onSlice,\n onYield,\n signal,\n } = options ?? {};\n\n // Stable identity, always runs the latest `task` passed this render —\n // see useChunkedTask.ts for why this is useEffectEvent rather than a ref\n // mutated during render.\n const stableTask = useEffectEvent(() => task());\n const stableOnSlice = useEffectEvent((durationMs: number, segmentsDone: number) => {\n onSlice?.(durationMs, segmentsDone);\n });\n const stableOnYield = useEffectEvent(() => {\n onYield?.();\n });\n\n const [controller] = useState(() => createTaskController<DeferredTask<T>>());\n\n const snapshot = useSyncExternalStore(\n controller.subscribe,\n () => controller.getSnapshot() ?? IDLE_SNAPSHOT,\n () => IDLE_SNAPSHOT,\n );\n\n useEffect(\n () => {\n const deferredTask = createDeferredTask<T>({\n task: stableTask,\n onSlice: stableOnSlice,\n onYield: stableOnYield,\n ...(timeSliceMs !== undefined ? { timeSliceMs } : {}),\n ...(batchSize !== undefined ? { batchSize } : {}),\n ...(signal !== undefined ? { signal } : {}),\n });\n controller.replace(deferredTask);\n if (autoStart) deferredTask.start();\n\n return () => {\n deferredTask.cancel();\n controller.replace(null);\n };\n },\n // `deps` plays the role `items` plays for useChunkedTask: it's the\n // caller's declaration of \"this is new work\". `stableTask` is an Effect\n // Event — stable by construction, intentionally excluded from the\n // reactive dependency set rather than omitted by oversight. `deps` is a\n // caller-supplied array, the same pattern (and the same reason it can't\n // be statically verified) as useMemo/useEffect's own deps parameter.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [...deps, timeSliceMs, batchSize, autoStart, signal, controller],\n );\n\n const start = useCallback(() => controller.getCurrent()?.start(), [controller]);\n const pause = useCallback(() => controller.getCurrent()?.pause(), [controller]);\n const resume = useCallback(() => controller.getCurrent()?.resume(), [controller]);\n const cancel = useCallback(() => controller.getCurrent()?.cancel(), [controller]);\n\n // Built from `snapshot`'s own wide-`status` fields — see the identical\n // note in useChunkedTask.ts; `snapshot` already carries the invariant.\n return {\n status: snapshot.status,\n error: snapshot.error,\n // From the snapshot: the engine publishes the value before it\n // announces `done`, so a concurrent render can never see one without\n // the other. See useChunkedTask.ts.\n value: snapshot.value,\n reason: snapshot.reason,\n isIdle: snapshot.status === 'idle',\n isProcessing: snapshot.status === 'processing',\n isPaused: snapshot.status === 'paused',\n isDone: snapshot.status === 'done',\n isCancelled: snapshot.status === 'cancelled',\n isError: snapshot.status === 'error',\n start,\n pause,\n resume,\n cancel,\n } as UseDeferredTaskState<T>;\n}\n"],"mappings":";;;;AAuCA,SAAgB,uBAEU;CACxB,IAAI,UAAuB;CAC3B,IAAI;CACJ,MAAM,4BAAY,IAAI,IAAgB;CAEtC,MAAM,eAAqB;EACzB,KAAK,MAAM,YAAY,WAAW,SAAS;CAC7C;CAEA,OAAO;EACL,YAAY,aAAa;GACvB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EACA,mBACE,SAAS,YAAY;EACvB,kBAAkB;EAClB,UAAU,SAAS;GACjB,oBAAoB;GACpB,UAAU;GACV,oBAAoB,MAAM,UAAU,MAAM;GAC1C,OAAO;EACT;CACF;AACF;;AC1CA,SAAgB,uBACd,UAC2B;CAC3B,MAAM,SAAS,OAAO,QA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+ const require_deferred_task = require("../deferred-task-j8KF7lk7.cjs");
3
3
  exports.DEFAULT_TIME_SLICE_MS = require_deferred_task.DEFAULT_TIME_SLICE_MS;
4
4
  exports.createChunkScheduler = require_deferred_task.createChunkScheduler;
5
5
  exports.createDeferredTask = require_deferred_task.createDeferredTask;
@@ -1 +1 @@
1
- export { r as DEFAULT_TIME_SLICE_MS, n as createChunkScheduler, t as createDeferredTask, f as initialChunkState, i as isInputPending, p as isTerminal, a as now, o as resolveYieldStrategy, s as shouldYieldNow, c as supportsInputPending, l as supportsMessageChannel, u as supportsSchedulerYield, m as transition, d as yieldToMain } from "../deferred-task-DkHdsliy.js";
1
+ export { r as DEFAULT_TIME_SLICE_MS, n as createChunkScheduler, t as createDeferredTask, f as initialChunkState, i as isInputPending, p as isTerminal, a as now, o as resolveYieldStrategy, s as shouldYieldNow, c as supportsInputPending, l as supportsMessageChannel, u as supportsSchedulerYield, m as transition, d as yieldToMain } from "../deferred-task-MsTNINAd.js";
@@ -440,6 +440,7 @@ function createDeferredTask(options) {
440
440
  let loopStarted = false;
441
441
  let settledFlag = false;
442
442
  let settledValue;
443
+ let hasSettledValue = false;
443
444
  let cancelReason;
444
445
  let snapshot = { status: machineState.status };
445
446
  let snapshotStale = false;
@@ -470,7 +471,7 @@ function createDeferredTask(options) {
470
471
  snapshot = {
471
472
  status: machineState.status,
472
473
  ...machineState.error !== void 0 ? { error: machineState.error } : {},
473
- ...settledValue !== void 0 ? { value: settledValue } : {},
474
+ ...hasSettledValue ? { value: settledValue } : {},
474
475
  ...cancelReason !== void 0 ? { reason: cancelReason } : {}
475
476
  };
476
477
  }
@@ -499,13 +500,14 @@ function createDeferredTask(options) {
499
500
  */
500
501
  function publishValue(resolvedValue) {
501
502
  settledValue = resolvedValue;
503
+ hasSettledValue = true;
502
504
  snapshotStale = true;
503
505
  }
504
506
  function settleWith(status, resolvedValue, error) {
505
507
  if (status === "done") {
506
508
  settle({
507
509
  status,
508
- ...resolvedValue !== void 0 ? { value: resolvedValue } : {}
510
+ value: resolvedValue
509
511
  });
510
512
  return;
511
513
  }
@@ -642,4 +644,4 @@ function createDeferredTask(options) {
642
644
  }
643
645
  export { now as a, supportsInputPending as c, yieldToMain as d, initialChunkState as f, isInputPending as i, supportsMessageChannel as l, transition as m, createChunkScheduler as n, resolveYieldStrategy as o, isTerminal as p, DEFAULT_TIME_SLICE_MS as r, shouldYieldNow as s, createDeferredTask as t, supportsSchedulerYield as u };
644
646
 
645
- //# sourceMappingURL=deferred-task-DkHdsliy.js.map
647
+ //# sourceMappingURL=deferred-task-MsTNINAd.js.map