Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dist/angular-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ angular.module('FredrikSandell.worker-pool', []).service('WorkerService', [
function ($q) {
var that = {};
var urlToAngular;
var urlToLibraries = [];
var serviceToUrlMap = {};
/*jshint laxcomma:true */
/*jshint quotmark: false */
Expand Down Expand Up @@ -48,6 +49,9 @@ angular.module('FredrikSandell.worker-pool', []).service('WorkerService', [
urlToAngular = urlToAngularJs;
return that;
};
that.addLibrary = function (url) {
urlToLibraries.push(url);
};
that.addDependency = function (serviceName, moduleName, url) {
serviceToUrlMap[serviceName] = {
url: url,
Expand Down Expand Up @@ -80,6 +84,9 @@ angular.module('FredrikSandell.worker-pool', []).service('WorkerService', [
};
function createIncludeStatements(listOfServiceNames) {
var includeString = '';
angular.forEach(urlToLibraries, function (libraryUrl) {
includeString += 'importScripts(\'' + libraryUrl + '\');\n';
});
angular.forEach(listOfServiceNames, function (serviceName) {
if (serviceToUrlMap[serviceName]) {
includeString += 'importScripts(\'' + serviceToUrlMap[serviceName].url + '\');';
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-workers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/angular-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ angular.module('FredrikSandell.worker-pool', [])
.service('WorkerService', ['$q', function ($q) {
var that = {};
var urlToAngular;
var urlToLibraries = [];
var serviceToUrlMap = {};
/*jshint laxcomma:true */
/*jshint quotmark: false */
Expand Down Expand Up @@ -47,11 +48,15 @@ angular.module('FredrikSandell.worker-pool', [])
, "angular.bootstrap(null, ['WorkerApp']);"].join("\n");


that.setAngularUrl = function(urlToAngularJs) {
that.setAngularUrl = function(urlToAngularJs) {
urlToAngular = urlToAngularJs;
return that;
};

that.addLibrary = function(url) {
urlToLibraries.push(url);
};

that.addDependency = function (serviceName, moduleName, url) {
serviceToUrlMap[serviceName] = {
url : url,
Expand Down Expand Up @@ -104,6 +109,9 @@ angular.module('FredrikSandell.worker-pool', [])

function createIncludeStatements(listOfServiceNames) {
var includeString = '';
angular.forEach(urlToLibraries, function(libraryUrl){
includeString += 'importScripts(\'' + libraryUrl + '\');\n';
});
angular.forEach(listOfServiceNames, function (serviceName) {
if (serviceToUrlMap[serviceName]) {
includeString += 'importScripts(\''+serviceToUrlMap[serviceName].url+'\');';
Expand Down
3 changes: 3 additions & 0 deletions src/test/ng-underscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
angular.module('underscore', []).factory('_', ['$window', function($window) {
return $window._; // assumes underscore has already been loaded on the page
}]);
106 changes: 106 additions & 0 deletions test/unit/angular-workers-add-librarySpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
describe('FredrikSandell.worker-pool', function () {

var WorkerService = null;
var rootScope = null;

beforeEach(function() {
module('FredrikSandell.worker-pool');
inject(function (_WorkerService_, $rootScope) {
WorkerService = _WorkerService_;
rootScope = $rootScope;
});
});
beforeEach(function() {
// set the URL to obtain angular
WorkerService.setAngularUrl('https://code.angularjs.org/1.5.7/angular.js');
// set the URL of other mandatory libraries
WorkerService.addLibrary('https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js');
});

it('should be an object', function () {
expect(typeof WorkerService).toBe('object');
});

it('should have a method addLibrary()', function () {
expect(WorkerService.addLibrary).toBeDefined();
expect(typeof WorkerService.addLibrary).toBe('function');
});

it('should have a method createAngularWorker()', function () {
expect(WorkerService.createAngularWorker).toBeDefined();
expect(typeof WorkerService.createAngularWorker).toBe('function');
});

it('should have a method setAngularUrl()', function () {
expect(WorkerService.setAngularUrl).toBeDefined();
expect(typeof WorkerService.setAngularUrl).toBe('function');
});

function waitUntilCompletedToTriggerPromiseResolve(completed, rootScope) {
//must wait before triggering digest loop which resolves of the promises
//worker must be given time to initialize
var checker = setInterval(function(){
if(completed) {
clearInterval(checker);
} else {
rootScope.$apply();
}
}, 100);
}

it('createAngularWorker() should return a valid AngularWorker object', function (done) {
var completed = false;

WorkerService.addDependency(
'_',
'underscore',
'http://localhost:9876/base/src/test/ng-underscore.js'
);
var worker = WorkerService.createAngularWorker(['input', 'output', '_', function (input, output) {
//the worker method does not matter
//should always return a promise resolving to an initialized worker
}]);

worker.then(function(worker) {
expect(typeof worker).toBe('object');
expect(typeof worker.run).toBe('function');
expect(typeof worker.terminate).toBe('function');
}, function(data) {
expect(true).toBe(false); //initialization should be ok
})['finally'](function() {
done();
completed = true;
});
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
});

it('should be possible inject custom dependencies in worker', function (done) {
var completed = false;

WorkerService.addDependency(
'_',
'underscore',
'http://localhost:9876/base/src/test/ng-underscore.js'
);
var workerPromise = WorkerService.createAngularWorker(['input', 'output', '_', function (input, output, PiCalculatorService) {
output.resolve(_.size(input));

}]);

workerPromise.then(function(worker) {
return worker.run([1,2,3]); //execute the run method on the fully initialized worker
}, function error(reason) {
expect(false).toBe(true);

}).then(function success(data) {
expect(data).toEqual(3); //worker should not succeed in this test
})['finally'](function() {
done();
completed = true;
});

waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
});


});