Operative
Before reading this please ensure you fully understand the concept of Web Workers.
Operative is a small JS utility (~1.8k gzipped) for seamlessly creating Web Worker scripts. Its features include:
- Seamless API Authoring
- Producing debuggable Worker Blobs
- Providing
console
interface for simple logging - Degrading where Worker/Blob support is lacking
Why Operative?
Utilising unabstracted Workers can be cumbersome and awkward. Having to design and implement message-passing contracts and having to setup event listeners yourself is time-consuming and error-prone. Operative takes care of this stuff so you can focus on your code.
Before you get excited:
Even with Operative you are still subject to the constraints of Web Workers, i.e.
- No DOM/BOM Access
- No synchronous communication with parent page
Quick install
bower install operative
npm install operative
Note: Operative has not been tested in non-browser envs
Or just grab the built JS file from dist/
, also available here (0.3.0):
Creating an Operative Module
An Operative module is defined as an object containing properties/methods:
var calculator = operative({
add: function(a, b, cb) {
cb( a + b );
}
});
This would expose an asynchronous API:
calculator.add(1, 2, function(result) {
result;
});
The add()
function will run within a worker. The value it returns is handled by operative and forwarded, asynchronously to your callback function in the parent page.
Notice that the exposed add
method requires its last argument to be a callback. The last argument passed to an operative method must always be a callback. All preceding arguments are passed into the worker itself.
NOTE: It's important to note that the Operative code is not executed in-place. *It's executed within a Worker. You won't be able to access variables surrounding the definition of your operative:
var something = 123;
var myWorker = operative({
doStuff: function() {
something += 456;
}
});
(the something variable won't exist within the Worker)
Instead you can do:
var myWorker = operative({
something: 123,
doStuff: function() {
this.something += 456;
}
});
Need to iterate 10,000,000,000 times? No problem!
var craziness = operative({
doCrazy: function(cb) {
console.time('Craziness');
for (var i = 0; i < 10000000000; ++i);
console.timeEnd('Craziness');
cb('I am done!');
}
});
craziness.doCrazy(function(result) {
result;
});
Browser Support for Workers
- FF 17+
- Chrome 7+
- Safari 4+
- Opera 11+
- IE10+
Degraded Operative
Operative degrades in this order:
(higher is better/cooler)
- Full Worker via Blob & Structured-Cloning (Ch13+, FF8+, IE11+, Op11.5+, Sf5.1+)
- Full Worker via Eval & Structured-Cloning (IE10)
- Full Worker via Blob & JSON marshalling (???)
- Full Worker via Eval & JSON marshalling (Sf4)
- No Worker: Regular JS called via iframe (older browsers)
Operative will degrade in environments with no Worker or Blob support. In such a case the code would execute as regular in-place JavaScript. The calls will still be asynchronous though, not immediate.
If you are looking to support this fully degraded state (honestly, only do it if you have to) then you'll also need to avoid utilising Worker-specific APIs like importScripts
.
No Worker-Via-Blob Support
Operative supports browsers with no worker-via-blob support (e.g. IE10, Safari 4.0) via eval, and it requires operative.js
or operative.min.js
to be its own file and included in the page via a <script>
tag. Or, alternatively, if its bundled with other JS, you'll have to have an additional operative.js
and specify it before creating an operative module via operative.setSelfURL('path/to/operative.js')
(this'll only generate a request where the aforementioned support is lacking). Due to the usage of eval in these cases it is recommended to debug your operatives in more capable browsers.
Operative API Documentation
- {Function} operative: A global function which creates a new Operative module with the passed methods/properties. Note: Non-function properties must be basic objects that can be passed to
JSON.stringify
. - Pass an object of methods, e.g.
operative({ method: function() {...} ... });
- Or pass a single function, e.g.
operative(function() {})
(in which case a single function is returned) - Either signature allows you to pass dependencies as a second param:
operative(..., ['dep1.js', 'dep2.js'])
. - {Boolean} operative.hasWorkerSupport: A boolean indicating whether both Blob and Worker support is detected.
- {Function} operative.setSelfURL: Allows you to set the URL of the operative script. Use this if you want IE10 & Safari 4/5 support and you're not including operative by the conventional
<script src="operative.js"></script>
. - {Function} operative.setBaseURL: Allows you to set the URL that should be prepended to relative dependency URLs.
Creating an Operative:
To create an operative module pass an object of methods/properties:
var myOperative = operative({
doX: function(a, b, c, callback) {
},
doY: function(a, b, c, callback) {
}
});
Or a single function to create a singular operative:
var myOperative = operative(function(a, b, c, callback) {
callback(...);
});
myOperative(1, 2, 3, function() { });
Returning results
The most simple way to use operative is to pass in a callback function when calling an operative function and within the operative method call the callback with your result:
var combine = operative(function(foo, bar, callback) {
callback(foo + bar);
});
combine('foo', 'bar', function() {
result;
});
Return via Promises
If you don't pass a callback when calling an operative method, operative will assume you want a Promise. Note that operative will reference operative.Promise
and will expect it to be a native Promise implementation or compliant polyfill. Operative does not come bundled with a Promise implementation.
var combine = operative(function(foo, bar) {
var deferred = this.deferred();
if (foo !== 'foo') {
deferred.reject('foo should be "foo"!');
} else {
deferred.fulfill(foo + bar);
}
});
var promise = combine('foo', 'bar');
promise.then(function(value) {
}, function(err) {
});
NOTE: Operative will only give you a promise if you don't pass a callback and if operative.Promise
is defined. By default operative.Promise
will reference window.Promise
(native implementation if it exists).
Transferring objects
Read more about the transfers API here.
Declaring dependencies
Operative accepts a second argument, an array of JS files to load within the worker ( or in its degraded state, an Iframe ):
var lodashWorker = operative(function(method, args, cb) {
cb(
_[method].apply(_, args)
);
}, [
'http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js'
]);
lodashWorker('uniq', [[1, 2, 3, 3, 2, 1, 4, 3, 2]], function(output) {
output;
});
Declared dependencies will be loaded before any of your operative's methods are called. Even if you call one from the outside, that call will be queued until the context (Worker/iFrame) completes loading the dependencies.
Note: Each dependency, if not an absolute URL, will be loaded relative to the calculated base URL, which operative determines like so:
var baseURL = (
location.protocol + '//' +
location.hostname +
(location.port?':'+location.port:'') +
location.pathname
).replace(/[^\/]+$/, '');
To override at runtime use:
operative.setBaseURL('http://some.com/new/absolute/base/');
operative.getBaseURL();
Destroying an operative
To terminate the operative (and thus its worker/iframe):
o.terminate();
(terminate
is aliased to the now-deprecated destroy
)
Testing & Building
Special thanks to BrowserStack for providing free testing!
$ # grab dependencies
$ npm install
$ # install grunt globally if you don't have it...
$ npm install -g grunt-cli
$ # test
$ grunt test
$ # do everything + build dist:
$ grunt
Changelog
- 0.4.0-rc1
- Refactor test suites (use mocha instead of jasmine and fix various flakey specs).
- Deprecate
deferred.fulfil()
(worker context promise API) in favour of deferred.resolve()
(alias for fulfil
still exists). - Introduce Transfers API (#23).
- Fix #18.
- Retain callbacks (allowing them to be called again and again -- a la Events). See #15.
- Introduce small benchmarks suite
- 0.3.2 (7 Jul 2014) AMD Support + Align correctly with ES6 Promise API (PRs 21 and 22 -- thanks Rich!)
- 0.3.1 (27 Apr 2014) Improved release flow via PR #20.
- 0.3.0 (21 Sep 2013) API:
terminate
aliased to destroy
(deprecating the latter). See Issue #14. - 0.2.1 (30 Jul 2013) Fix worker-via-eval support (Safari 4/5, IE8/9)
- 0.2.0 (29 Jul 2013) See #10
- Dependency Loading (initially suggested in #8)
- Deprecate direct returning in favour of a callback passed to each operative invocation.
- Fallback to IFrame (to provide safer sandbox for degraded state)
- 0.1.0 (25 Jul 2013) Support Promises (from Issue #3) if they're provided by a native Promise implementation or compliant polyfill. Also added support for
operative(Function)
which returns a single function. - 0.0.3 (18 Jul 2013) Support for asynchronous returning from within operative methods (via
this.async()
). - 0.0.2 (12 Jul 2013) Improved browser support: IE10 support via eval, degraded JSON-marshalling etc.
- 0.0.1 (11 Jul 2013) Initial
Contributors