Skip to content

Commit

Permalink
refactor to DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
deebloodd committed Jun 4, 2015
1 parent 8c924e6 commit 3f74cb6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 61 deletions.
10 changes: 5 additions & 5 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1>Look In The Console</h1>
<script src="src/worker.js"></script>
<script>
// Create worker instance
var worker = $worker().extend({
var workers = $worker().extend({
onmessage: function(e) {
console.log(e.data);
},
Expand All @@ -18,23 +18,23 @@ <h1>Look In The Console</h1>
});

// create web worker from worker instance
var worker1 = worker.create(function() {
var worker1 = workers.create(function() {
self.postMessage('Hello World');
});

// create another web worker from worker instance
var worker2 = worker.create(function() {
var worker2 = workers.create(function() {
self.postMessage('Goodbye World');
});

worker.extend({
workers.extend({
bar: true
});

console.log(worker1);

// Post to ALL workers in instance
worker.postMessage();
workers.postMessage();
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions spec/web-worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('web-worker', function() {
myWorker.postMessage({length: 1024, min: 0, max: 9999});
});

afterEach(function() {
myWorker.terminate();
});

it('should create a big array', function() {
expect(result.length).toBe(1024);
});
Expand Down
117 changes: 61 additions & 56 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,7 @@ function $worker() {
*/
onerror: function() {},

/**
* @name postMessage
*
* @memberof $worker
*
* @description
* send data tp the worker and assign the onmessage and on error listeners
*
* @example
* myWorker.postMessage(1988);
*
* @param data
*/
postMessage: function postMessage(data) {
this.shell.postMessage(data);

this.shell.onmessage = this.onmessage;

this.shell.onerror = this.onerror;
},
postMessage: _postMessage,

/**
* @name terminate
Expand Down Expand Up @@ -99,13 +80,11 @@ function $worker() {

this.blobArray.unshift(val.toString());

this.blobArray.unshift(_makeVarName(key));
this.blobArray.unshift(__makeVarName(key));
}
}

this.blob = new Blob(this.blobArray, { type: 'text/javascript' });

this.shell = new Worker(urlBuilder(this.blob));
__createWebWorker.call(this);
},
/**
* @name removeScripts
Expand All @@ -123,14 +102,12 @@ function $worker() {
var index;

for(var i = 0, len = arguments.length; i < len; i++) {
index = this.blobArray.indexOf(_makeVarName(arguments[i]));
index = this.blobArray.indexOf(__makeVarName(arguments[i]));

this.blobArray.splice(index, 3);
}

this.blob = new Blob(this.blobArray, { type: 'text/javascript' });

this.shell = new Worker(urlBuilder(this.blob));
__createWebWorker.call(this);
}
};

Expand All @@ -149,15 +126,15 @@ function $worker() {
* @public
*/
function create(method) {
var createdWorker = Object.create(proto);
var obj = Object.create(proto);

obj.blobArray = ['self.onmessage = ', method.toString(), ';']; // array to be used for blob

createdWorker.blobArray = ['self.onmessage = ', method.toString(), ';']; // array to be used for blob
createdWorker.blob = new Blob(createdWorker.blobArray, { type: 'text/javascript' });
createdWorker.shell = new Worker(urlBuilder(createdWorker.blob));
__createWebWorker.call(obj);

workers.push(createdWorker);
workers.push(obj);

return createdWorker;
return obj;
}

/**
Expand Down Expand Up @@ -199,16 +176,8 @@ function $worker() {
* @param data
*/
function postMessage(data) {
var current;

for(var i = 0, len = workers.length; i < len; i++) {
current = workers[i];

current.shell.postMessage(data);

current.shell.onmessage = current.onmessage;

current.shell.onerror = current.onerror;
_postMessage.call(workers[i], data);
}

return this;
Expand All @@ -223,12 +192,8 @@ function $worker() {
* terminate all workers
*/
function terminate() {
var current;

for(var i = 0, len = workers.length; i < len; i++) {
current = workers[i];

current.shell.terminate();
workers[i].shell.terminate();
}

workers.length = 0;
Expand All @@ -248,18 +213,58 @@ function $worker() {
return workers;
}

/* @private */
function _makeVarName(name) {
return 'self.' + name + ' = ';
/**
* @name postMessage
*
* @memberof $worker
*
* @description
* send data tp the worker and assign the onmessage and on error listeners
*
* @example
* myWorker.postMessage(1988);
*
* @param {*} data
*
* @protected
*/
function _postMessage(data) {
this.shell.postMessage(data);

this.shell.onmessage = this.onmessage;

this.shell.onerror = this.onerror;
}

/**
* Expose public methods
*
* @param {Function} create
* @param {Function} extend
* @param {Function} viewAll
* @name __createWebWorker
*
* @memberof $worker
*
* @private
*/
function __createWebWorker() {
this.blob = new Blob(this.blobArray, { type: 'text/javascript' });

this.shell = new Worker(urlBuilder(this.blob));
}

/**
* @name __makeVarName
*
* @memberof $worker
*
* @param name
*
* @return {string}
*
* @private
*/
function __makeVarName(name) {
return 'self.' + name + ' = ';
}

/* Expose public methods */
return {
create: create,
extend: extend,
Expand Down

0 comments on commit 3f74cb6

Please sign in to comment.