From 7d911a3fe0c95763d663d28e5a2309898c3ba1a3 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 18 Jun 2015 10:59:10 -0700 Subject: [PATCH] [Workers] Add worker service Add service for running WebWorkers, #12. --- bundles.json | 1 + platform/execution/README.md | 1 + platform/execution/bundle.json | 10 +++ platform/execution/src/WorkerService.js | 68 +++++++++++++++++ platform/execution/test/WorkerServiceSpec.js | 77 ++++++++++++++++++++ platform/execution/test/suite.json | 3 + 6 files changed, 160 insertions(+) create mode 100644 platform/execution/README.md create mode 100644 platform/execution/bundle.json create mode 100644 platform/execution/src/WorkerService.js create mode 100644 platform/execution/test/WorkerServiceSpec.js create mode 100644 platform/execution/test/suite.json diff --git a/bundles.json b/bundles.json index 0486bdf24b..6e28332374 100644 --- a/bundles.json +++ b/bundles.json @@ -9,6 +9,7 @@ "platform/commonUI/general", "platform/commonUI/inspect", "platform/containment", + "platform/execution", "platform/telemetry", "platform/features/layout", "platform/features/pages", diff --git a/platform/execution/README.md b/platform/execution/README.md new file mode 100644 index 0000000000..2188e5f909 --- /dev/null +++ b/platform/execution/README.md @@ -0,0 +1 @@ +Contains services which manage execution and flow control (e.g. for concurrency.) diff --git a/platform/execution/bundle.json b/platform/execution/bundle.json new file mode 100644 index 0000000000..ce12852ee9 --- /dev/null +++ b/platform/execution/bundle.json @@ -0,0 +1,10 @@ +{ + "extensions": { + "services": [ + { + "key": "workerService", + "depends": [ "$window", "workers[]" ] + } + ] + } +} diff --git a/platform/execution/src/WorkerService.js b/platform/execution/src/WorkerService.js new file mode 100644 index 0000000000..b8f24ee614 --- /dev/null +++ b/platform/execution/src/WorkerService.js @@ -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; + } +); diff --git a/platform/execution/test/WorkerServiceSpec.js b/platform/execution/test/WorkerServiceSpec.js new file mode 100644 index 0000000000..24abab6e81 --- /dev/null +++ b/platform/execution/test/WorkerServiceSpec.js @@ -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(); + }); + + }); + } +); diff --git a/platform/execution/test/suite.json b/platform/execution/test/suite.json new file mode 100644 index 0000000000..d14a0714c5 --- /dev/null +++ b/platform/execution/test/suite.json @@ -0,0 +1,3 @@ +[ + "WorkerService" +]