chunkProcessor
chunkProcessor
takes an observable array, observes it and call processor
once for a chunk of items added to the observable array, optionally debouncing the action. The maximum chunk size can be limited by number. This allows both, splitting larger into smaller chunks or (when debounced) combining smaller chunks and/or single items into reasonable chunks of work.
Parameters
observableArray
(Array<T>
): observable array instance to trackprocessor
debounce
(number
): debounce time in ms. With debounce0
, the processor will run synchronously (optional. default:0
)maxChunkSize
(number
): do not call on full array, but smaller chunks. With0
, it will process the full array . (optional. default:0
)
Returns
IDisposer
: stops the processor
Examples
const trackedActions = observable([])
const stop = chunkProcessor(trackedActions, chunkOfMax10Items => {
sendTrackedActionsToServer(chunkOfMax10Items);
}, 100, 10)
// usage:
trackedActions.push("scrolled")
trackedActions.push("hoveredButton")
// when both pushes happen within 100ms, there will be only one call to server