Package Exports
- chunkify
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (chunkify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
chunkify
An ES6-developed functional API that prevents long-running scripts from blocking the JavaScript thread.
Introduction
This is an API for getting long-running JavaScripts to periodically unblock the thread. The idea is to use timeouts to chunk up work and let the call stack unwind.
Install
$ npm install --save chunkifyUsage
Import the module:
import chunkify from 'chunkify'Options
In API methods, an optional options object may provide any subset of the following data:
chunk: the number times to successively invokefnbefore yielding control of the main thread.- default value is
1 - must be positive
- aliases:
chunksize
- default value is
delay: the minimal time in milliseconds to wait before continuing to invokefn.- default value is
0 - must be non-negative
- aliases:
yield,yieldtime,delaytime
- default value is
scope: the object on whichfnis invoked on.- default value is
null - must not be a
Number,Boolean, orundefined
- default value is
API
Arrays
chunkify.each(Array array, Function fn, [Object options])
fn is invoked on successive array elements and their indices (fn(item, index)).
Returns a Promise that resolves with undefined when fn has been invoked on all items in array.
chunkify.map(Array array, Function fn, [Object options])
Identical to chunkify.each, except the returned Promise resolves with the array mapped by fn.
chunkify.reduce(Array array, Function fn, [Object options])
Exactly like the native reduce on Array.prototype, but the work is chunked up as above, and the returned promise resolves with the result of the reduction.
If any invocation of fn throws an Error, the returned promise is rejected with an object {error, item, index}, where error is the caught Error, index is the index in array where the invocation threw, and item is array[index]. No further processing happens after the failure.
Loops
chunkify.range(Function fn, Number final, [Object options])
Invoke fn in chunks from the range options.start to final. If options.start is given, it must be a Number less than or equal to final. Its default value is 0.
chunkify.loop(Function fn, Number range, [Object options])
Like chunkify.range, with options.start forced to 0.
If any invocation of fn throws an Error, the promise is rejected with an object {error, index}, where error is the caught Error and index is the index in array where the invocation threw. No further processing happens after the failure.
Contributing
Development is in snake_case ES6.
Get the source.
$ git clone git@github.com:yangmillstheory/chunkifyInstall dependencies.
$ npm installCompile sources.
$ node_modules/.bin/gulpRun tests.
$ npm testLicense
MIT © 2015, Victor Alvarez