#Bonobo
##A lightweight (~1.9kb gzipped) wrapper for the HTML5 Web Worker API.
Author: Joe Harlow (joe@f5.io)
Bonobo provides a simple wrapper for the HTML5 Web Worker API. It is library agnostic and has no dependencies.
Bonobo allows you to define workers inline, negating the need for seperate JavaScript files.
Bonobo is built on the Web Worker and Blob APIs. When these are not available it will purely run the task in the main thread by creating a fake worker. This should allow the usage of Bonobo in a Progressive Enhancement environment.
For full capabilities, the following browsers are supported:
- Microsoft Internet Explorer 10+
- Mozilla Firefox 21.0+
- Google Chrome 27.0+
- Apple Safari 5.1+
- Opera 15.0+
Bonobo can be installed with bower, by running:
bower install bonobo
Bonobo can be accessed using either Bonobo or bN. From here on out, we will refer to it as Bonobo.
Bonobo has two sets of methods. One relates to methods available in your main JavaScript file/thread, the other relates to methods available from within your worker.
####Bonobo(reference /* String */)
Calling Bonobo('monkey') creates and returns a new Employee with a reference of 'monkey'. The created Employee can be retrieved at any time using the same method.
The returned Employee has the following methods, which are chainable:
-
#####
task(fn /* Function */) TheFunctionpassed into thetaskmethod will be executed on a newthread. The firstparameterof theFunctionwill be passed by thebeginmethod.The
Functioncan contain methods fromBonobo'sEmployeeThread API.#####Example Bonobo('monkey') .task(function(data) { Bonobo.log('Received: ' + data); // can also use console.log // This computationally expensive code will run on a new thread var arr = []; for (var i = 0, n=1, a=1; i < 16750000; i++, a+=4, n++) { arr.push(i * a / n); } Bonobo.done('I've finished my task!'); });
-
#####
done(fn /* Function */) TheFunctionpassed into thedonemethod will be executed when theEmployeeThread calls its owndonemethod. The firstparameterof theFunctionwill be what theEmployeeThread passed through.#####Example
Bonobo('monkey') .done(function(data) { console.log('Response from Bonobo(\'' + this.ref + '\'): ' + data); }); -
#####
on(event /* String */,fn /* Function */) Theonmethod allowsBonoboto listen out for custom events emitted from theEmployeeThread using theemitmethod.#####Example
Bonobo('monkey') .task(function(data) { Bonobo.emit('refresh', 'Hello World!'); }) .on('refresh', function(data) { console.log('Data from Bonobo(\'' + this.ref + '\'): ' + data); }); -
#####
error(fn /* Function */) TheFunctionpassed into theerrormethod will be executed when theEmployeeThread calls its ownerrormethod OR when an error occurs. The firstparameterof theFunctionwill be what theEmployeeThread passed through OR the error message.#####Example
Bonobo('monkey') .error(function(message) { console.log('Error from Bonobo(\'' + this.ref + '\'): ' + message); }); -
#####
begin(data) Thebeginmethod tells theEmployeeThread to start running. Thedatacan be of any type and will be passed through to the firstparameterof thetaskyou have defined.You cannot
beginanEmployeeThread if you have not defined it'stask.#####Example
Bonobo('monkey') .task(function(data) { console.log(data); // will log '[Bonobo('monkey') : LOG] : Begin your task!' }) .begin('Begin your task!'); -
#####
stop() This method willstopanEmployeeThread.#####Example
Bonobo('monkey') .stop(); // will stop the Employee Thread with reference: 'monkey' -
#####
destroy() This method willstopanEmployeeThread anddestroytheEmployee.#####Example
Bonobo('monkey') .destroy(); // will stop the Employee Thread with reference: 'monkey' and destroy it
-
#####
Bonobo.log(data) ORconsole.log(data) This method willlogto theconsoleof the Main Thread.console.login theEmployeeThread is aliased toBonobo.logfor ease-of-use.#####Example
Bonobo('monkey') .task(function(data) { Bonobo.log('Hello'); // will log 'Hello' to the Main Thread console.log('World!'); // will log 'World!' to the Main Thread }); -
#####
Bonobo.done(data) This method will push thedataprovided to the Main Thread.#####Example
Bonobo('monkey') .task(function(data) { // This computationally expensive code will run on a new thread var arr = []; for (var i = 0, n=1, a=1; i < 16750000; i++, a+=4, n++) { arr.push(i * a / n); } Bonobo.done({ message: 'I\'ve finished my task!', data: arr }); }); -
#####
Bonobo.emit(event /* String */,data) This method will push thedataprovided to the Main Thread through the handler defined for theeventname using theonmethod.#####Example
Bonobo('monkey') .task(function(data) { Bonobo.emit('refresh', 'Hello World!'); }) .on('refresh', function(data) { console.log('Data from Bonobo(\'' + this.ref + '\'): ' + data); }); -
#####
Bonobo.error(message) This method will force an error with the value ofmessageto the Main Thread.#####Example
Bonobo('monkey') .task(function(data) { if (data !== 'Hello World') { Bonobo.error('Wrong data!'); } else { Bonobo.done('I\'ve finished my task!'); } }); -
#####
Bonobo.importJS(...args) This method is analiasfor theimportScriptsfunction that is available within theWeb WorkerAPI.#####Example
Bonobo('monkey') .task(function(data) { Bonobo.importJS('scripts/something.js','scripts/awesome.js'); Bonobo.log(JSON.stringify(Something)); Bonobo.log(JSON.stringify(Awesome)); }); -
#####
Bonobo.stop() This method will stop theEmployeeThread from within itself.#####Example
Bonobo('monkey') .task(function(data) { if (data !== 'Hello World') { Bonobo.stop(); } });
Copyright (C) 2013 Joe Harlow (Fourth of 5 Limited)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.