Skip to content

Commit

Permalink
add removeScripts method
Browse files Browse the repository at this point in the history
  • Loading branch information
deebloodd committed May 14, 2015
1 parent 0ac2c4d commit a17b1ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
9 changes: 7 additions & 2 deletions spec/web-worker-success.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ describe('worker - success', function() {
var myWorker, result;

beforeEach(function(done) {
var hello = function hello() { console.log('hello world'); };
var hello = function hello() { };

var goodbye = function goodbye() { };

myWorker = new $Worker(function(e) {
var foo = [], min = e.data.min, max = e.data.max;
Expand All @@ -16,17 +18,20 @@ describe('worker - success', function() {
self.postMessage(foo);
});

myWorker.loadScripts({'hello': hello});
myWorker.loadScripts({'hello': hello}, {'goodbye': goodbye});

myWorker.onmessage = function(data) {
result = data;

done();
};

myWorker.postMessage({length: 1024, min: 0, max: 9999});
});

afterEach(function() {
myWorker.removeScripts('hello', 'goodbye');

myWorker.terminate();
myWorker = null;
result = null;
Expand Down
41 changes: 38 additions & 3 deletions src/web-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function $Worker(method, fb, debug) {
worker.errorHandler = errorHandler;
worker.getBlobArray = getBlobArray;
worker.updateBlobArray = updateBlobArray;
worker.removeFromBlobArray = removeFromBlobArray;

/**
* @name errorHandler
Expand Down Expand Up @@ -74,6 +75,18 @@ function $Worker(method, fb, debug) {
function updateBlobArray(val) {
blobArray.unshift(val);
}

/**
* @name updateBlobArray
*
* @memberof $Worker
*
* @param {Number} idx - index to start at
* @param {Number} length - the number of items to remove
*/
function removeFromBlobArray(idx, length) {
blobArray.splice(idx, length);
}
}

/**
Expand Down Expand Up @@ -114,9 +127,7 @@ $Worker.prototype.postMessage = function postMessage(data) {
* @description
* override this method to when listening for the worker to complete
*/
$Worker.prototype.onmessage = function onmessage(e) {
console.log(e);
};
$Worker.prototype.onmessage = function onmessage() { };

/**
* @name terminate
Expand Down Expand Up @@ -158,6 +169,30 @@ $Worker.prototype.loadScripts = function loadScripts() {
worker.updateBlobArray('var ' + key + ' = ');
}

if(worker.hasWorkers) {
worker.blob = new Blob(worker.getBlobArray(), { type: "text/javascript"});

worker.shell = new Worker(window.URL.createObjectURL(worker.blob));
}
};

/**
* @name removeScripts
*
* @memberof $Worker
*
* @description
* remove a previously loaded function from the worker
*/
$Worker.prototype.removeScripts = function removeScripts() {
var worker = this, array = worker.getBlobArray(), index;

for(var i = 0, len = arguments.length; i < len; i++) {
index = array.indexOf('var ' + arguments[i] + ' = ');

worker.removeFromBlobArray(index, 3);
}

if(worker.hasWorkers) {
worker.blob = new Blob(worker.getBlobArray(), { type: "text/javascript"});

Expand Down

0 comments on commit a17b1ee

Please sign in to comment.