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.
Here's a demo of the code being used in the browser, integrated with Angular.js.
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
Core API
chunkify.generator(Number start, Number final, Object options)
Returns the core Generator used internally. Thus, this is somewhat more unstable than other API methods.
options may specify delay and chunk, as above.
The generator yields integers from the range start and final, inclusive. Values are yielded synchronously within intervals of length chunk. At every chunkth call to .next(), the generator yields a Promise that represents a paused and non-blocking state - this is what unblocks the thread.
This promise resolves after at least delay milliseconds; an error will be thrown in case the generator is advanced again before this Promise has resolved. Further calls to .next() may be resumed after resolution. This process can continue until all integers between start and final have been yielded.
Arrays
chunkify.each(Array array, Function fn, [Object options])
options can specify all of the properties mentioned in options.
fn is invoked in synchronous chunks 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])
options can specify all of the properties mentioned in options, plus a memo value which will be used as the reduction memo as in the native reduce on Array.prototype.
The behavior is just like Array.prototype.reduce, 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.
Ranges
chunkify.interval(Function fn, Number final, [Object options])
options can specify all of the properties mentioned in options, along with a numerical start value that must be less than or equal to final. By default, options.start is 0.
Invoke fn synchronously in chunks on successive integers in the interval options.start to final.
Returns a Promise that resolves with undefined when the entire interval has been traversed.
chunkify.range(Function fn, Number range, [Object options])
Like chunkify.interval, with options.start forced to 0.
If any invocation of fn throws an Error, the returned 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