Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
web worker lib
/*
* amele.js
* Author : Eray 'maia' Arslan
* Email : relfishere@gmail.com
* Blog : http://eray.js.org/
* This project is released under the MIT license.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], function (exports) {
root.Amele = factory(root, exports);
});
} else if (typeof exports !== 'undefined') {
factory(root, exports);
} else {
root.Amele = factory(root, {});
}
}(this, function (root, Amele) {
var amele_id = 0;
Amele = function (child) {
this.id = amele_id++;
this.guid = this._guid();
this.child = child.slice(0, this._filePrefix.length) === this._filePrefix ?
new Worker(child.substr(this._filePrefix.length, child.length)) :
new Worker(URL.createObjectURL(new Blob(
[child],
{type: "text/javascript"}
)));
this._eventsUp();
return this;
};
Amele.prototype._filePrefix = "file:";
Amele.prototype.kill = function () {
this._eventsDown();
this.child.terminate();
this.child = null;
};
Amele.prototype.send = function (data) {
this.child.postMessage(data);
return this;
};
Amele.prototype.handle = function () {
};
Amele.prototype.success = function (callback) {
this.handle = callback;
return this;
};
Amele.prototype._guid = function () {
var guid = [];
for (var i = 0; i < 8; i++) {
guid.push(this._s4());
}
return guid.join("-");
};
Amele.prototype._s4 = function () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
Amele.prototype._nope = function () {
};
Amele.prototype._eventsUp = function () {
var self = this;
this.child.onmessage = function (e) {
self.handle(e.data);
};
};
Amele.prototype._eventsDown = function () {
this.child.onmessage = this._nope();
};
return Amele;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment