[Workers] Add worker service

Add service for running WebWorkers, #12.
This commit is contained in:
Victor Woeltjen 2015-06-18 10:59:10 -07:00
parent dc85d3c191
commit 7d911a3fe0
6 changed files with 160 additions and 0 deletions

View File

@ -9,6 +9,7 @@
"platform/commonUI/general",
"platform/commonUI/inspect",
"platform/containment",
"platform/execution",
"platform/telemetry",
"platform/features/layout",
"platform/features/pages",

View File

@ -0,0 +1 @@
Contains services which manage execution and flow control (e.g. for concurrency.)

View File

@ -0,0 +1,10 @@
{
"extensions": {
"services": [
{
"key": "workerService",
"depends": [ "$window", "workers[]" ]
}
]
}
}

View File

@ -0,0 +1,68 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define*/
define(
[],
function () {
"use strict";
/**
* Handles the execution of WebWorkers.
* @constructor
*/
function WorkerService($window, workers) {
var workerUrls = {},
Worker = $window.Worker;
function addWorker(worker) {
var key = worker.key;
if (!workerUrls[key]) {
workerUrls[key] = [
worker.bundle.path,
worker.bundle.sources,
worker.scriptUrl
].join("/");
}
}
(workers || []).forEach(addWorker);
return {
/**
* Start running a new web worker. This will run a worker
* that has been registered under the `workers` category
* of extension.
*
* @param {string} key symbolic identifier for the worker
* @returns {Worker} the running Worker
*/
run: function (key) {
var scriptUrl = workerUrls[key];
return scriptUrl && Worker && new Worker(scriptUrl);
}
};
}
return WorkerService;
}
);

View File

@ -0,0 +1,77 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,describe,it,expect,beforeEach,jasmine*/
define(
["../src/WorkerService"],
function (WorkerService) {
"use strict";
describe("The worker service", function () {
var mockWindow,
testWorkers,
mockWorker,
service;
beforeEach(function () {
mockWindow = jasmine.createSpyObj('$window', ['Worker']);
testWorkers = [
{
key: 'abc',
scriptUrl: 'c.js',
bundle: { path: 'a', sources: 'b' }
},
{
key: 'xyz',
scriptUrl: 'z.js',
bundle: { path: 'x', sources: 'y' }
},
{
key: 'xyz',
scriptUrl: 'bad.js',
bundle: { path: 'bad', sources: 'bad' }
}
];
mockWorker = {};
mockWindow.Worker.andReturn(mockWorker);
service = new WorkerService(mockWindow, testWorkers);
});
it("instantiates workers at registered paths", function () {
expect(service.run('abc')).toBe(mockWorker);
expect(mockWindow.Worker).toHaveBeenCalledWith('a/b/c.js');
});
it("prefers the first worker when multiple keys are found", function () {
expect(service.run('xyz')).toBe(mockWorker);
expect(mockWindow.Worker).toHaveBeenCalledWith('x/y/z.js');
});
it("returns undefined for unknown workers", function () {
expect(service.run('def')).toBeUndefined();
});
});
}
);

View File

@ -0,0 +1,3 @@
[
"WorkerService"
]