#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 */
) TheFunction
passed into thetask
method will be executed on a newthread
. The firstparameter
of theFunction
will be passed by thebegin
method.The
Function
can contain methods fromBonobo
'sEmployee
Thread 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 */
) TheFunction
passed into thedone
method will be executed when theEmployee
Thread calls its owndone
method. The firstparameter
of theFunction
will be what theEmployee
Thread passed through.#####Example
Bonobo('monkey') .done(function(data) { console.log('Response from Bonobo(\'' + this.ref + '\'): ' + data); });
-
#####
on
(event /* String */
,fn /* Function */
) Theon
method allowsBonobo
to listen out for custom events emitted from theEmployee
Thread using theemit
method.#####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 */
) TheFunction
passed into theerror
method will be executed when theEmployee
Thread calls its ownerror
method OR when an error occurs. The firstparameter
of theFunction
will be what theEmployee
Thread passed through OR the error message.#####Example
Bonobo('monkey') .error(function(message) { console.log('Error from Bonobo(\'' + this.ref + '\'): ' + message); });
-
#####
begin
(data
) Thebegin
method tells theEmployee
Thread to start running. Thedata
can be of any type and will be passed through to the firstparameter
of thetask
you have defined.You cannot
begin
anEmployee
Thread 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 willstop
anEmployee
Thread.#####Example
Bonobo('monkey') .stop(); // will stop the Employee Thread with reference: 'monkey'
-
#####
destroy
() This method willstop
anEmployee
Thread anddestroy
theEmployee
.#####Example
Bonobo('monkey') .destroy(); // will stop the Employee Thread with reference: 'monkey' and destroy it
-
#####
Bonobo
.log
(data
) ORconsole
.log
(data
) This method willlog
to theconsole
of the Main Thread.console
.log
in theEmployee
Thread is aliased toBonobo
.log
for 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 thedata
provided 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 thedata
provided to the Main Thread through the handler defined for theevent
name using theon
method.#####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 ofmessage
to 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 analias
for theimportScripts
function that is available within theWeb Worker
API.#####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 theEmployee
Thread 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.