UNPKG

1.37 kBTypeScriptView Raw
1import { IDisposer } from "./utils";
2/**
3 * `chunkProcessor` takes an observable array, observes it and calls `processor`
4 * once for a chunk of items added to the observable array, optionally deboucing the action.
5 * The maximum chunk size can be limited by number.
6 * This allows both, splitting larger into smaller chunks or (when debounced) combining smaller
7 * chunks and/or single items into reasonable chunks of work.
8 *
9 * @example
10 * const trackedActions = observable([])
11 * const stop = chunkProcessor(trackedActions, chunkOfMax10Items => {
12 * sendTrackedActionsToServer(chunkOfMax10Items);
13 * }, 100, 10)
14 *
15 * // usage:
16 * trackedActions.push("scrolled")
17 * trackedActions.push("hoveredButton")
18 * // when both pushes happen within 100ms, there will be only one call to server
19 *
20 * @param {T[]} observableArray observable array instance to track
21 * @param {(item: T[]) => void} processor action to call per item
22 * @param {number} [debounce=0] optional debounce time in ms. With debounce 0 the processor will run synchronously
23 * @param {number} [maxChunkSize=0] optionally do not call on full array but smaller chunks. With 0 it will process the full array.
24 * @returns {IDisposer} stops the processor
25 */
26export declare function chunkProcessor<T>(observableArray: T[], processor: (item: T[]) => void, debounce?: number, maxChunkSize?: number): IDisposer;