Skip to content

Commit

Permalink
update prototypes version. stil cant decide which one!
Browse files Browse the repository at this point in the history
  • Loading branch information
deebloodd committed May 12, 2015
1 parent 6fd66ad commit 9ea0a3c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function(config) {
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

browsers: ['Chrome'],
browsers: ['Chrome', 'Firefox'],

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"jasmine-core": "^2.3.2",
"karma": "^0.12.31",
"karma-chrome-launcher": "^0.1.10",
"karma-firefox-launcher": "^0.1.6",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.1.4",
"uglifyjs": "^2.4.10"
Expand Down
13 changes: 11 additions & 2 deletions spec/web-worker.prototypes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ describe('worker.prototypes: create big array', function() {
var myWorker, result;

beforeEach(function(done) {

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

myWorker = new $Worker(function(e) {
var foo = [], min = e.data.min, max = e.data.max;

for (var i = 0; i < e.data.length; i++) {
foo.push(Math.floor(Math.random() * (max - min)) + min);
foo.push(Math.floor(Math.random() * (maxa - min)) + min);
}

hello();

self.postMessage(foo);
});

myWorker.loadScripts(function hello() { console.log('hello world'); }, function foo() {});
myWorker.loadScripts(hello);

myWorker.onmessage = function(data) {
result = data;
Expand All @@ -24,6 +27,12 @@ describe('worker.prototypes: create big array', function() {
myWorker.postMessage({length: 1024, min: 0, max: 9999});
});

afterEach(function() {
myWorker.terminate();
myWorker = null;
result = null;
});

it('should create a big array', function() {
expect(result.length).toBe(1024);
});
Expand Down
5 changes: 4 additions & 1 deletion spec/web-worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ describe('worker: create big array', function() {
var myWorker, result;

beforeEach(function(done) {

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

myWorker = $worker(function(e) {
var foo = [], min = e.data.min, max = e.data.max;

Expand All @@ -14,7 +17,7 @@ describe('worker: create big array', function() {
self.postMessage(foo);
});

myWorker.loadScripts(function hello() { console.log('hello world'); }, function foo() {});
myWorker.loadScripts(hello);

myWorker.onmessage = function(data) {
result = data;
Expand Down
13 changes: 4 additions & 9 deletions src/web-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function $worker(func, fb) {
};

/**
* @name run
* @name postMessage
*
* @memberof $worker
*
Expand Down Expand Up @@ -96,17 +96,12 @@ function $worker(func, fb) {
* load named function declarations into the web worker to be used by the web worker
*/
function loadScripts() {
var funcs = arguments;

for(var i = 0, len = funcs.length; i < len; i++) {
blobArray.push(funcs[i]);
for(var i = 0, len = arguments.length; i < len; i++) {
blobArray.push(arguments[i]);
}

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

// Create new web worker. This worker will be referred to as 'shell' from now on
shell = new Worker(window.URL.createObjectURL(blob));
}
}
47 changes: 27 additions & 20 deletions src/web-worker.prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,34 @@
function $Worker(method, fb) {
var worker = this;

// The fallback method to use if web worker failts
// The fallback method to use if web worker fails
worker.fb = fb;

// does the browser support web workers
worker.hasWorkers = !!window.Worker;

// placeholder fo blob
worker.blob = null;

// placeholder for shell
worker.shell = null;

// Array to be used for the blob sent to the web worker
worker.blobArray = ['self.onmessage = ', method, ';'];

// Create blob from the passed in function
worker.blob = !!window.Blob ? new Blob(worker.blobArray, { type: "text/javascript" }) : null;

// does the browser support web workers
worker.hasWorkers = !!window.Worker;
if(worker.hasWorkers) {
// Create blob from the passed in function
worker.blob = new Blob(worker.blobArray, { type: "text/javascript" });

// Create new web worker. This worker will be referred to as 'shell' from now on
worker.shell = worker.hasWorkers ? new Worker(window.URL.createObjectURL(worker.blob)) : method;
// Create new web worker. This worker will be referred to as 'shell' from now on
worker.shell = new Worker(window.URL.createObjectURL(worker.blob));
}
}

/**
* @name run
* @name postMessage
*
* @memberof $worker
* @memberof $Worker
*
* @description
* run the created web worker
Expand Down Expand Up @@ -63,7 +71,7 @@ $Worker.prototype.postMessage = function postMessage(data) {
/**
* @name onmessage
*
* @memberof $worker
* @memberof $Worker
*
* @description
* override this method to when listening for the worker to complete
Expand All @@ -73,36 +81,35 @@ $Worker.prototype.onmessage = function onmessage() { };
/**
* @name terminate
*
* @memberof $worker
* @memberof $Worker
*
* @description
* terminate the created web worker
*/
$Worker.prototype.terminate = function terminate() {
var worker = this;

worker._shell.terminate();
worker.shell.terminate();
};


/**
* @name loadScripts
*
* @memberof $worker
* @memberof $Worker
* @memberof $Worker
*
* @description
* load named function declarations into the web worker to be used by the web worker
*/
$Worker.prototype.loadScripts = function loadScripts() {
var funcs = arguments, worker = this;
var worker = this;

for(var i = 0, len = funcs.length; i < len; i++) {
worker.blobArray.push(funcs[i].toString());
for(var i = 0, len = arguments.length; i < len; i++) {
worker.blobArray.push(arguments[i]);
}

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

// Create new web worker. This worker will be referred to as 'shell' from now on
worker.shell = new Worker(window.URL.createObjectURL(worker.blob));
Expand Down

0 comments on commit 9ea0a3c

Please sign in to comment.