react-inp-chunker 0.2.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.
- package/README.md +51 -7
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +1 -1
- package/dist/{deferred-task-CwjZA3K-.js → deferred-task-Cai4fF4m.js} +87 -20
- package/dist/deferred-task-Cai4fF4m.js.map +1 -0
- package/dist/{deferred-task-EWH55ZyL.cjs → deferred-task-DRigxydg.cjs} +87 -20
- package/dist/deferred-task-DRigxydg.cjs.map +1 -0
- package/dist/{index-DShDP2VN.d.ts → index-DuUMqnsA.d.cts} +137 -27
- package/dist/index-DuUMqnsA.d.cts.map +1 -0
- package/dist/{index-DShDP2VN.d.cts → index-DuUMqnsA.d.ts} +137 -27
- package/dist/index-DuUMqnsA.d.ts.map +1 -0
- package/dist/index.cjs +32 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +101 -12
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +101 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -5
- package/dist/index.js.map +1 -1
- package/dist/production/core/index.cjs +1 -1
- package/dist/production/core/index.js +1 -1
- package/dist/production/{deferred-task-BGsfee-L.js → deferred-task-MsTNINAd.js} +61 -22
- package/dist/production/deferred-task-MsTNINAd.js.map +1 -0
- package/dist/production/{deferred-task-zVnq18IN.cjs → deferred-task-j8KF7lk7.cjs} +61 -22
- package/dist/production/deferred-task-j8KF7lk7.cjs.map +1 -0
- package/dist/production/index.cjs +32 -5
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +32 -5
- package/dist/production/index.js.map +1 -1
- package/package.json +15 -9
- package/dist/deferred-task-CwjZA3K-.js.map +0 -1
- package/dist/deferred-task-EWH55ZyL.cjs.map +0 -1
- package/dist/index-DShDP2VN.d.cts.map +0 -1
- package/dist/index-DShDP2VN.d.ts.map +0 -1
- package/dist/production/deferred-task-BGsfee-L.js.map +0 -1
- package/dist/production/deferred-task-zVnq18IN.cjs.map +0 -1
|
@@ -149,28 +149,95 @@ interface ChunkSchedulerOptions<T, R> {
|
|
|
149
149
|
readonly collectResults?: boolean;
|
|
150
150
|
/** Called after each item is successfully processed. */
|
|
151
151
|
readonly onItem?: (result: R, item: T, index: number) => void;
|
|
152
|
+
/**
|
|
153
|
+
* Called when `processItem` throws. Return `'skip'` to drop that one item
|
|
154
|
+
* and keep going — its index and error are recorded in `skipped`, on both
|
|
155
|
+
* the live snapshot and the settled result — or return `'abort'` to end
|
|
156
|
+
* the task the way it always has, as an `'error'` result carrying
|
|
157
|
+
* whatever was collected before the throw. Omitting `onItem` defaults to
|
|
158
|
+
* `'abort'`, so existing callers see no behavior change.
|
|
159
|
+
*
|
|
160
|
+
* Only ever called for `processItem` throwing. An `onItem` that throws
|
|
161
|
+
* still aborts unconditionally: that's the caller's own progress handler
|
|
162
|
+
* misbehaving, not a per-item failure there's anything to recover from.
|
|
163
|
+
*/
|
|
164
|
+
readonly onError?: (error: unknown, item: T, index: number) => 'skip' | 'abort';
|
|
152
165
|
/** External cancellation source — aborting it is equivalent to calling cancel(). */
|
|
153
166
|
readonly signal?: AbortSignal;
|
|
154
167
|
/** Testing-only: forces a specific yield strategy instead of auto-detecting one. */
|
|
155
168
|
readonly yieldStrategy?: YieldStrategy;
|
|
169
|
+
/**
|
|
170
|
+
* Called every time a slice ends and the task is about to yield, with how
|
|
171
|
+
* long that slice ran and how many items are done so far. Unlike the
|
|
172
|
+
* development-only diagnostics above, this ships in the production
|
|
173
|
+
* build — wire it to your own telemetry so you can verify, on real
|
|
174
|
+
* traffic, that this task is actually staying under `timeSliceMs`,
|
|
175
|
+
* instead of trusting this package's own benchmark numbers.
|
|
176
|
+
*/
|
|
177
|
+
readonly onSlice?: (durationMs: number, processed: number) => void;
|
|
178
|
+
/** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
|
|
179
|
+
readonly onYield?: () => void;
|
|
156
180
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
readonly
|
|
160
|
-
readonly error
|
|
181
|
+
/** One item `onError` chose to skip, in the order it happened. */
|
|
182
|
+
interface SkippedChunkItem {
|
|
183
|
+
readonly index: number;
|
|
184
|
+
readonly error: unknown;
|
|
161
185
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Fields every status shares. Split out so the union below only has to spell
|
|
188
|
+
* out what actually differs per status: whether `results` is guaranteed yet,
|
|
189
|
+
* and whether `error` is.
|
|
190
|
+
*/
|
|
191
|
+
interface ChunkResultShared {
|
|
192
|
+
/** Items `onError` skipped. Omitted when nothing was skipped. */
|
|
193
|
+
readonly skipped?: readonly SkippedChunkItem[];
|
|
167
194
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* observe `status: 'done'` without the results that go with it.
|
|
195
|
+
* The `AbortSignal`'s `reason`, present only when this cancellation came
|
|
196
|
+
* from that signal aborting rather than a direct `cancel()` call.
|
|
171
197
|
*/
|
|
172
|
-
readonly
|
|
198
|
+
readonly reason?: unknown;
|
|
173
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Discriminated on `status`: narrowing to `'error'` narrows `error` to
|
|
202
|
+
* `unknown` (defined), and narrowing to `'done' | 'cancelled'` rules it out
|
|
203
|
+
* entirely — `results` is a plain array either way, never `undefined`, since
|
|
204
|
+
* a settled task has always published it by the time anyone can observe
|
|
205
|
+
* this type.
|
|
206
|
+
*/
|
|
207
|
+
type ChunkSchedulerResult<R> = ChunkResultShared & ({
|
|
208
|
+
readonly status: 'done' | 'cancelled';
|
|
209
|
+
readonly results: readonly R[];
|
|
210
|
+
readonly error?: undefined;
|
|
211
|
+
} | {
|
|
212
|
+
readonly status: 'error';
|
|
213
|
+
readonly results: readonly R[];
|
|
214
|
+
readonly error: unknown;
|
|
215
|
+
});
|
|
216
|
+
/**
|
|
217
|
+
* Discriminated on `status`: narrowing to `'done' | 'cancelled' | 'error'`
|
|
218
|
+
* narrows `results` to a defined array — exactly the statuses `isTerminal()`
|
|
219
|
+
* recognizes, and exactly when the engine has actually published it. Narrow
|
|
220
|
+
* to `'error'` specifically to also narrow `error` itself to defined.
|
|
221
|
+
*/
|
|
222
|
+
type ChunkSnapshot<R = unknown> = ChunkResultShared & ({
|
|
223
|
+
readonly status: 'idle' | 'processing' | 'paused';
|
|
224
|
+
readonly processed: number;
|
|
225
|
+
readonly total: number;
|
|
226
|
+
readonly results?: undefined;
|
|
227
|
+
readonly error?: undefined;
|
|
228
|
+
} | {
|
|
229
|
+
readonly status: 'done' | 'cancelled';
|
|
230
|
+
readonly processed: number;
|
|
231
|
+
readonly total: number;
|
|
232
|
+
readonly results: readonly R[];
|
|
233
|
+
readonly error?: undefined;
|
|
234
|
+
} | {
|
|
235
|
+
readonly status: 'error';
|
|
236
|
+
readonly processed: number;
|
|
237
|
+
readonly total: number;
|
|
238
|
+
readonly results: readonly R[];
|
|
239
|
+
readonly error: unknown;
|
|
240
|
+
});
|
|
174
241
|
interface ChunkScheduler<R> {
|
|
175
242
|
start(): void;
|
|
176
243
|
pause(): void;
|
|
@@ -243,22 +310,65 @@ interface DeferredTaskOptions<T> {
|
|
|
243
310
|
readonly signal?: AbortSignal;
|
|
244
311
|
/** Testing-only: forces a specific yield strategy instead of auto-detecting one. */
|
|
245
312
|
readonly yieldStrategy?: YieldStrategy;
|
|
313
|
+
/**
|
|
314
|
+
* Called every time a segment-batch ends and the task is about to yield,
|
|
315
|
+
* with how long that slice ran and how many segments are done so far.
|
|
316
|
+
* Ships in the production build — see the identical option on
|
|
317
|
+
* `createChunkScheduler` for why.
|
|
318
|
+
*/
|
|
319
|
+
readonly onSlice?: (durationMs: number, segmentsDone: number) => void;
|
|
320
|
+
/** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
|
|
321
|
+
readonly onYield?: () => void;
|
|
246
322
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
readonly value?: T;
|
|
250
|
-
readonly error?: unknown;
|
|
251
|
-
}
|
|
252
|
-
interface DeferredTaskSnapshot<T = unknown> {
|
|
253
|
-
readonly status: ChunkStatus;
|
|
254
|
-
readonly error?: unknown;
|
|
323
|
+
/** Fields every status shares — just `reason`, since `error` and `value` differ by status. */
|
|
324
|
+
interface DeferredResultShared {
|
|
255
325
|
/**
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* `status: 'done'` without the value that goes with it.
|
|
326
|
+
* The `AbortSignal`'s `reason`, present only when this cancellation came
|
|
327
|
+
* from that signal aborting rather than a direct `cancel()` call.
|
|
259
328
|
*/
|
|
260
|
-
readonly
|
|
329
|
+
readonly reason?: unknown;
|
|
261
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Discriminated on `status`: narrowing to `'done'` narrows `value` to `T`
|
|
333
|
+
* itself, not `T | undefined` — including when `T` allows `undefined`, a
|
|
334
|
+
* generator genuinely returning it is still a value, distinct from "no
|
|
335
|
+
* value yet". Narrowing to `'error'` narrows `error` to defined instead.
|
|
336
|
+
*/
|
|
337
|
+
type DeferredTaskResult<T> = DeferredResultShared & ({
|
|
338
|
+
readonly status: 'done';
|
|
339
|
+
readonly value: T;
|
|
340
|
+
readonly error?: undefined;
|
|
341
|
+
} | {
|
|
342
|
+
readonly status: 'cancelled';
|
|
343
|
+
readonly value?: undefined;
|
|
344
|
+
readonly error?: undefined;
|
|
345
|
+
} | {
|
|
346
|
+
readonly status: 'error';
|
|
347
|
+
readonly value?: undefined;
|
|
348
|
+
readonly error: unknown;
|
|
349
|
+
});
|
|
350
|
+
/**
|
|
351
|
+
* Same discrimination as `DeferredTaskResult`, plus the non-terminal
|
|
352
|
+
* statuses a live snapshot can still be in, where neither `value` nor
|
|
353
|
+
* `error` exists yet.
|
|
354
|
+
*/
|
|
355
|
+
type DeferredTaskSnapshot<T = unknown> = DeferredResultShared & ({
|
|
356
|
+
readonly status: 'idle' | 'processing' | 'paused';
|
|
357
|
+
readonly value?: undefined;
|
|
358
|
+
readonly error?: undefined;
|
|
359
|
+
} | {
|
|
360
|
+
readonly status: 'done';
|
|
361
|
+
readonly value: T;
|
|
362
|
+
readonly error?: undefined;
|
|
363
|
+
} | {
|
|
364
|
+
readonly status: 'cancelled';
|
|
365
|
+
readonly value?: undefined;
|
|
366
|
+
readonly error?: undefined;
|
|
367
|
+
} | {
|
|
368
|
+
readonly status: 'error';
|
|
369
|
+
readonly value?: undefined;
|
|
370
|
+
readonly error: unknown;
|
|
371
|
+
});
|
|
262
372
|
interface DeferredTask<T> {
|
|
263
373
|
start(): void;
|
|
264
374
|
pause(): void;
|
|
@@ -278,5 +388,5 @@ interface DeferredTask<T> {
|
|
|
278
388
|
* here unchanged.
|
|
279
389
|
*/
|
|
280
390
|
declare function createDeferredTask<T>(options: DeferredTaskOptions<T>): DeferredTask<T>;
|
|
281
|
-
export {
|
|
282
|
-
//# sourceMappingURL=index-
|
|
391
|
+
export { isTerminal as A, supportsSchedulerYield as C, ChunkStatus as D, ChunkMachineState as E, TerminalChunkStatus as O, supportsMessageChannel as S, ChunkEvent as T, isInputPending as _, DeferredTaskSnapshot as a, shouldYieldNow as b, ChunkSchedulerOptions as c, SkippedChunkItem as d, createChunkScheduler as f, YieldToMainOptions as g, YieldStrategy as h, DeferredTaskResult as i, transition as j, initialChunkState as k, ChunkSchedulerResult as l, ShouldYieldOptions as m, DeferredTask as n, createDeferredTask as o, DEFAULT_TIME_SLICE_MS as p, DeferredTaskOptions as r, ChunkScheduler as s, DeferredGenerator as t, ChunkSnapshot as u, now as v, yieldToMain as w, supportsInputPending as x, resolveYieldStrategy as y };
|
|
392
|
+
//# sourceMappingURL=index-DuUMqnsA.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-DuUMqnsA.d.ts","names":[],"sources":["../src/core/chunk-machine.ts","../src/core/scheduler.ts","../src/core/chunk-scheduler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KAAK,gBAAgB;UAElC,oBAAoB;WAC1B,YAAY,kBAAkB;;WAE9B;;;;;;;;;;;WAWA;WACA,SAAS;;WAET,gBAAgB;;;;;;;WAOhB,WAAW,oBAAoB;;WAE/B;;;UAID;;;;;WAKC;;;;;;;;KASC,mBAAmB,KAAK;WAEnB;WAAyB,OAAO;WAAY;;WAE5C;WACA;WACA;;WAEA;WAA0B;WAA4B;;;;;;;KAQ3D,qBAAqB,eAAe;WAG/B;WACA;WACA;;WAEA;WAAyB,OAAO;WAAY;;WAE5C;WACA;WACA;;WAEA;WAA0B;WAA4B;;UAGtD,aAAa;EAC5B;EACA;EACA;EACA;EACA,eAAe,qBAAqB;;EAEpC,YAAY;EACZ,UAAU;WACD,SAAS,QAAQ,mBAAmB;;;;;;;;;iBAU/B,mBAAmB,GAAG,SAAS,oBAAoB,KAAK,aAAa"}
|
package/dist/index.cjs
CHANGED
|
@@ -20,7 +20,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
20
20
|
value: mod,
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
|
23
|
-
const require_deferred_task = require("./deferred-task-
|
|
23
|
+
const require_deferred_task = require("./deferred-task-DRigxydg.cjs");
|
|
24
24
|
let react = require("react");
|
|
25
25
|
react = __toESM(react, 1);
|
|
26
26
|
// src/react/task-controller.ts
|
|
@@ -58,6 +58,8 @@ function useEffectEventFallback(callback) {
|
|
|
58
58
|
}
|
|
59
59
|
const useEffectEvent = react.useEffectEvent ?? useEffectEventFallback;
|
|
60
60
|
// src/react/useChunkedTask.ts
|
|
61
|
+
/** Stable empty array so a never-skipped task doesn't hand out a fresh `[]` every render. */
|
|
62
|
+
const NO_SKIPPED = [];
|
|
61
63
|
const IDLE_STATUS = "idle";
|
|
62
64
|
/**
|
|
63
65
|
* Chunks a heavy, synchronous per-item computation over `items` so it never
|
|
@@ -75,11 +77,18 @@ const IDLE_STATUS = "idle";
|
|
|
75
77
|
* same as any other React dependency.
|
|
76
78
|
*/
|
|
77
79
|
function useChunkedTask(items, processItem, options) {
|
|
78
|
-
const { timeSliceMs, batchSize, collectResults, autoStart = true, onItem } = options ?? {};
|
|
80
|
+
const { timeSliceMs, batchSize, collectResults, autoStart = true, onItem, onError, onSlice, onYield, signal } = options ?? {};
|
|
79
81
|
const stableProcessItem = useEffectEvent((item, index) => processItem(item, index));
|
|
80
82
|
const stableOnItem = useEffectEvent((result, item, index) => {
|
|
81
83
|
onItem?.(result, item, index);
|
|
82
84
|
});
|
|
85
|
+
const stableOnError = useEffectEvent((error, item, index) => onError?.(error, item, index) ?? "abort");
|
|
86
|
+
const stableOnSlice = useEffectEvent((durationMs, processed) => {
|
|
87
|
+
onSlice?.(durationMs, processed);
|
|
88
|
+
});
|
|
89
|
+
const stableOnYield = useEffectEvent(() => {
|
|
90
|
+
onYield?.();
|
|
91
|
+
});
|
|
83
92
|
const [controller] = (0, react.useState)(() => createTaskController());
|
|
84
93
|
const idleSnapshot = (0, react.useMemo)(() => ({
|
|
85
94
|
status: IDLE_STATUS,
|
|
@@ -92,9 +101,13 @@ function useChunkedTask(items, processItem, options) {
|
|
|
92
101
|
items,
|
|
93
102
|
processItem: stableProcessItem,
|
|
94
103
|
onItem: stableOnItem,
|
|
104
|
+
onError: stableOnError,
|
|
105
|
+
onSlice: stableOnSlice,
|
|
106
|
+
onYield: stableOnYield,
|
|
95
107
|
...timeSliceMs !== void 0 ? { timeSliceMs } : {},
|
|
96
108
|
...batchSize !== void 0 ? { batchSize } : {},
|
|
97
|
-
...collectResults !== void 0 ? { collectResults } : {}
|
|
109
|
+
...collectResults !== void 0 ? { collectResults } : {},
|
|
110
|
+
...signal !== void 0 ? { signal } : {}
|
|
98
111
|
});
|
|
99
112
|
controller.replace(scheduler);
|
|
100
113
|
if (autoStart) scheduler.start();
|
|
@@ -108,6 +121,7 @@ function useChunkedTask(items, processItem, options) {
|
|
|
108
121
|
batchSize,
|
|
109
122
|
collectResults,
|
|
110
123
|
autoStart,
|
|
124
|
+
signal,
|
|
111
125
|
controller
|
|
112
126
|
]);
|
|
113
127
|
const start = (0, react.useCallback)(() => controller.getCurrent()?.start(), [controller]);
|
|
@@ -120,6 +134,8 @@ function useChunkedTask(items, processItem, options) {
|
|
|
120
134
|
total: snapshot.total,
|
|
121
135
|
error: snapshot.error,
|
|
122
136
|
results: snapshot.results,
|
|
137
|
+
skipped: snapshot.skipped ?? NO_SKIPPED,
|
|
138
|
+
reason: snapshot.reason,
|
|
123
139
|
isIdle: snapshot.status === "idle",
|
|
124
140
|
isProcessing: snapshot.status === "processing",
|
|
125
141
|
isPaused: snapshot.status === "paused",
|
|
@@ -146,15 +162,24 @@ const IDLE_SNAPSHOT = { status: "idle" };
|
|
|
146
162
|
* you would for `useMemo` or `useEffect`.
|
|
147
163
|
*/
|
|
148
164
|
function useDeferredTask(task, deps, options) {
|
|
149
|
-
const { timeSliceMs, batchSize, autoStart = true } = options ?? {};
|
|
165
|
+
const { timeSliceMs, batchSize, autoStart = true, onSlice, onYield, signal } = options ?? {};
|
|
150
166
|
const stableTask = useEffectEvent(() => task());
|
|
167
|
+
const stableOnSlice = useEffectEvent((durationMs, segmentsDone) => {
|
|
168
|
+
onSlice?.(durationMs, segmentsDone);
|
|
169
|
+
});
|
|
170
|
+
const stableOnYield = useEffectEvent(() => {
|
|
171
|
+
onYield?.();
|
|
172
|
+
});
|
|
151
173
|
const [controller] = (0, react.useState)(() => createTaskController());
|
|
152
174
|
const snapshot = (0, react.useSyncExternalStore)(controller.subscribe, () => controller.getSnapshot() ?? IDLE_SNAPSHOT, () => IDLE_SNAPSHOT);
|
|
153
175
|
(0, react.useEffect)(() => {
|
|
154
176
|
const deferredTask = require_deferred_task.createDeferredTask({
|
|
155
177
|
task: stableTask,
|
|
178
|
+
onSlice: stableOnSlice,
|
|
179
|
+
onYield: stableOnYield,
|
|
156
180
|
...timeSliceMs !== void 0 ? { timeSliceMs } : {},
|
|
157
|
-
...batchSize !== void 0 ? { batchSize } : {}
|
|
181
|
+
...batchSize !== void 0 ? { batchSize } : {},
|
|
182
|
+
...signal !== void 0 ? { signal } : {}
|
|
158
183
|
});
|
|
159
184
|
controller.replace(deferredTask);
|
|
160
185
|
if (autoStart) deferredTask.start();
|
|
@@ -167,6 +192,7 @@ function useDeferredTask(task, deps, options) {
|
|
|
167
192
|
timeSliceMs,
|
|
168
193
|
batchSize,
|
|
169
194
|
autoStart,
|
|
195
|
+
signal,
|
|
170
196
|
controller
|
|
171
197
|
]);
|
|
172
198
|
const start = (0, react.useCallback)(() => controller.getCurrent()?.start(), [controller]);
|
|
@@ -177,6 +203,7 @@ function useDeferredTask(task, deps, options) {
|
|
|
177
203
|
status: snapshot.status,
|
|
178
204
|
error: snapshot.error,
|
|
179
205
|
value: snapshot.value,
|
|
206
|
+
reason: snapshot.reason,
|
|
180
207
|
isIdle: snapshot.status === "idle",
|
|
181
208
|
isProcessing: snapshot.status === "processing",
|
|
182
209
|
isPaused: snapshot.status === "paused",
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["useRef","useCallback","React","useState","useMemo","useSyncExternalStore","createChunkScheduler","useCallback","useState","useSyncExternalStore","createDeferredTask","useCallback"],"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 { ChunkScheduler, ChunkSnapshot } from '../core/chunk-scheduler.js';\nimport { createTaskController } from './task-controller.js';\nimport { useEffectEvent } from './use-effect-event.js';\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\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 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 } = 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\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 ...(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 are Effect Events: stable by\n // construction, intentionally excluded 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 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\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 } = 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\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 ...(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,u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AAa,SAAS,WAAW;EACjC,SAAS,SAAS,WAAW;EAC7B;EACA;EACA;EACA;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["useRef","useCallback","React","useState","useMemo","useSyncExternalStore","createChunkScheduler","useCallback","useState","useSyncExternalStore","createDeferredTask","useCallback"],"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;CAE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AA,YAAAA,OAAkB,WAAW,WAAW,CAAC,EAAE,MAAM,GAAG,CAAC,UAAU,CAAC;CAC9E,MAAM,SAAA,GAAQA,MAAAA,YAAAA,OAAkB,WAAW,WAAW,CAAC,EAAE,MAAM,GAAG,CAAC,UAAU,CAAC;CAC9E,MAAM,UAAA,GAASA,MAAAA,YAAAA,OAAkB,WAAW,WAAW,CAAC,EAAE,OAAO,GAAG,CAAC,UAAU,CAAC;CAChF,MAAM,UAAA,GAASA,MAAAA,YAAAA,OAAkB,WAAW,WAAW,CAAC,EAAE,OAAO,GAAG,CAAC,UAAU,CAAC;CAIhF,OAAO;EACL,QAAQ,SAAS;EACjB,OAAO,SAAS;EAIhB,OAAO,SAAS;EAChB,QAAQ,SAAS;EACjB,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"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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;
|
|
@@ -24,14 +24,44 @@ interface UseChunkedTaskOptions<T, R> {
|
|
|
24
24
|
* without paying for an array copy on every yield checkpoint.
|
|
25
25
|
*/
|
|
26
26
|
readonly onItem?: (result: R, item: T, index: number) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Called when `processItem` throws. Return `'skip'` to drop that item and
|
|
29
|
+
* keep going — it shows up in `skipped` — or `'abort'` to end the task as
|
|
30
|
+
* an error, carrying whatever was collected before the throw. Omitting
|
|
31
|
+
* this defaults to `'abort'`, so existing callers see no behavior change.
|
|
32
|
+
*/
|
|
33
|
+
readonly onError?: (error: unknown, item: T, index: number) => 'skip' | 'abort';
|
|
34
|
+
/**
|
|
35
|
+
* Called every time a slice ends and the task is about to yield, with how
|
|
36
|
+
* long that slice ran and how many items are done so far. Ships in the
|
|
37
|
+
* production build, unlike this package's own dev-only diagnostics — wire
|
|
38
|
+
* it to your own telemetry to verify, on real traffic, that this task is
|
|
39
|
+
* actually staying under `timeSliceMs`.
|
|
40
|
+
*/
|
|
41
|
+
readonly onSlice?: (durationMs: number, processed: number) => void;
|
|
42
|
+
/** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
|
|
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;
|
|
27
53
|
}
|
|
28
|
-
|
|
29
|
-
|
|
54
|
+
/** Fields every status shares, independent of the `status`/`results`/`error` narrowing below. */
|
|
55
|
+
interface UseChunkedTaskShared {
|
|
30
56
|
readonly processed: number;
|
|
31
57
|
readonly total: number;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
58
|
+
/** Items `onError` has skipped so far, growing live as skips happen. Empty when unused. */
|
|
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;
|
|
35
65
|
readonly isIdle: boolean;
|
|
36
66
|
readonly isProcessing: boolean;
|
|
37
67
|
readonly isPaused: boolean;
|
|
@@ -43,6 +73,26 @@ interface UseChunkedTaskState<R> {
|
|
|
43
73
|
readonly resume: () => void;
|
|
44
74
|
readonly cancel: () => void;
|
|
45
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
|
+
});
|
|
46
96
|
/**
|
|
47
97
|
* Chunks a heavy, synchronous per-item computation over `items` so it never
|
|
48
98
|
* blocks the main thread for longer than a time slice — the thing that
|
|
@@ -70,12 +120,32 @@ interface UseDeferredTaskOptions {
|
|
|
70
120
|
readonly batchSize?: number;
|
|
71
121
|
/** Start processing automatically when the task is (re)created. Default `true`. */
|
|
72
122
|
readonly autoStart?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Called every time a segment-batch ends and the task is about to yield,
|
|
125
|
+
* with how long that slice ran and how many segments are done so far.
|
|
126
|
+
* Ships in the production build — see the identical option on
|
|
127
|
+
* `useChunkedTask`/`createChunkScheduler` for why.
|
|
128
|
+
*/
|
|
129
|
+
readonly onSlice?: (durationMs: number, segmentsDone: number) => void;
|
|
130
|
+
/** Called right after each yield resolves and work is about to resume. Pairs with `onSlice`. */
|
|
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;
|
|
73
141
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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;
|
|
79
149
|
readonly isIdle: boolean;
|
|
80
150
|
readonly isProcessing: boolean;
|
|
81
151
|
readonly isPaused: boolean;
|
|
@@ -87,6 +157,25 @@ interface UseDeferredTaskState<T> {
|
|
|
87
157
|
readonly resume: () => void;
|
|
88
158
|
readonly cancel: () => void;
|
|
89
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
|
+
});
|
|
90
179
|
/**
|
|
91
180
|
* Runs an arbitrary heavy, synchronous generator function in chunks, so it
|
|
92
181
|
* never produces a long task on its own. Where `useChunkedTask` chunks "one
|
|
@@ -99,5 +188,5 @@ interface UseDeferredTaskState<T> {
|
|
|
99
188
|
* you would for `useMemo` or `useEffect`.
|
|
100
189
|
*/
|
|
101
190
|
export declare function useDeferredTask<T>(task: () => DeferredGenerator<T>, deps: readonly unknown[], options?: UseDeferredTaskOptions): UseDeferredTaskState<T>;
|
|
102
|
-
export type { ChunkEvent, ChunkMachineState, ChunkScheduler, ChunkSchedulerOptions, ChunkSchedulerResult, ChunkSnapshot, ChunkStatus, DeferredGenerator, DeferredTask, DeferredTaskOptions, DeferredTaskResult, DeferredTaskSnapshot, UseChunkedTaskOptions, UseChunkedTaskState, UseDeferredTaskOptions, UseDeferredTaskState, YieldStrategy };
|
|
191
|
+
export type { ChunkEvent, ChunkMachineState, ChunkScheduler, ChunkSchedulerOptions, ChunkSchedulerResult, ChunkSnapshot, ChunkStatus, DeferredGenerator, DeferredTask, DeferredTaskOptions, DeferredTaskResult, DeferredTaskSnapshot, SkippedChunkItem, UseChunkedTaskOptions, UseChunkedTaskState, UseDeferredTaskOptions, UseDeferredTaskState, YieldStrategy };
|
|
103
192
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/react/useChunkedTask.ts","../src/react/useDeferredTask.ts"],"mappings":";
|
|
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"}
|