Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TaskProcessor to take absolute urls for worker paths #9338

Merged
merged 1 commit into from
Jan 27, 2021
Merged
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
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

### 1.78 - 2021-02-01

##### Additions :tada:

- `TaskProcessor` now accepts an absolute URL in addition to a worker name as it's first parameter. This makes it possible to use custom web workers with Cesium's task processing system without copying them to Cesium's Workers directory.

### 1.77 - 2021-01-04

##### Additions :tada:
Expand Down
16 changes: 9 additions & 7 deletions Source/Core/TaskProcessor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Uri from "../ThirdParty/Uri.js";
import when from "../ThirdParty/when.js";
import buildModuleUrl from "./buildModuleUrl.js";
import defaultValue from "./defaultValue.js";
Expand Down Expand Up @@ -142,7 +143,7 @@ function createWorker(processor) {
},
baseUrl: buildModuleUrl.getCesiumBaseUrl().url,
},
workerModule: TaskProcessor._workerModulePrefix + processor._workerName,
workerModule: processor._workerPath,
};

worker.postMessage(bootstrapMessage);
Expand All @@ -165,7 +166,7 @@ function getWebAssemblyLoaderConfig(processor, wasmOptions) {
if (!defined(wasmOptions.fallbackModulePath)) {
throw new RuntimeError(
"This browser does not support Web Assembly, and no backup module was provided for " +
processor._workerName
processor._workerPath
);
}

Expand Down Expand Up @@ -193,14 +194,15 @@ function getWebAssemblyLoaderConfig(processor, wasmOptions) {
* @alias TaskProcessor
* @constructor
*
* @param {String} workerName The name of the worker. This is expected to be a script
* in the Workers folder.
* @param {String} workerPath The Url to the worker. This can either be an absolute path or relative to the Cesium Workers folder.
* @param {Number} [maximumActiveTasks=5] The maximum number of active tasks. Once exceeded,
* scheduleTask will not queue any more tasks, allowing
* work to be rescheduled in future frames.
*/
function TaskProcessor(workerName, maximumActiveTasks) {
this._workerName = workerName;
function TaskProcessor(workerPath, maximumActiveTasks) {
this._workerPath = new Uri(workerPath).isAbsolute()
? workerPath
: TaskProcessor._workerModulePrefix + workerPath;
this._maximumActiveTasks = defaultValue(maximumActiveTasks, 5);
this._activeTasks = 0;
this._deferreds = {};
Expand All @@ -222,7 +224,7 @@ var emptyTransferableObjectArray = [];
* if there are too many active tasks,
*
* @example
* var taskProcessor = new Cesium.TaskProcessor('myWorkerName');
* var taskProcessor = new Cesium.TaskProcessor('myWorkerPath');
* var promise = taskProcessor.scheduleTask({
* someParameter : true,
* another : 'hello'
Expand Down
62 changes: 41 additions & 21 deletions Specs/Core/TaskProcessorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ import { when } from "../../Source/Cesium.js";
describe("Core/TaskProcessor", function () {
var taskProcessor;

beforeEach(function () {
TaskProcessor._workerModulePrefix = absolutize("../Specs/TestWorkers/");
});

afterEach(function () {
TaskProcessor._workerModulePrefix =
TaskProcessor._defaultWorkerModulePrefix;

if (taskProcessor && !taskProcessor.isDestroyed()) {
taskProcessor = taskProcessor.destroy();
}
});

it("works with a simple worker", function () {
it("works with a simple worker defined as relative to TaskProcessor._workerModulePrefix", function () {
TaskProcessor._workerModulePrefix = absolutize("../Specs/TestWorkers/");
taskProcessor = new TaskProcessor("returnParameters.js");

var parameters = {
Expand All @@ -29,13 +23,21 @@ describe("Core/TaskProcessor", function () {
},
};

return taskProcessor.scheduleTask(parameters).then(function (result) {
expect(result).toEqual(parameters);
});
return taskProcessor
.scheduleTask(parameters)
.then(function (result) {
expect(result).toEqual(parameters);
})
.always(function () {
TaskProcessor._workerModulePrefix =
TaskProcessor._defaultWorkerModulePrefix;
});
});

it("can be destroyed", function () {
taskProcessor = new TaskProcessor("returnParameters.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnParameters.js")
);

expect(taskProcessor.isDestroyed()).toEqual(false);

Expand All @@ -45,7 +47,9 @@ describe("Core/TaskProcessor", function () {
});

it("can transfer array buffer", function () {
taskProcessor = new TaskProcessor("returnByteLength.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnByteLength.js")
);

var byteLength = 100;
var parameters = new ArrayBuffer(byteLength);
Expand All @@ -69,7 +73,9 @@ describe("Core/TaskProcessor", function () {
});

it("can transfer array buffer back from worker", function () {
taskProcessor = new TaskProcessor("transferArrayBuffer.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/transferArrayBuffer.js")
);

var byteLength = 100;
var parameters = {
Expand All @@ -83,7 +89,9 @@ describe("Core/TaskProcessor", function () {
});

it("rejects promise if worker throws", function () {
taskProcessor = new TaskProcessor("throwError.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/throwError.js")
);

var message = "foo";
var parameters = {
Expand All @@ -101,7 +109,9 @@ describe("Core/TaskProcessor", function () {
});

it("rejects promise if worker returns a non-clonable result", function () {
taskProcessor = new TaskProcessor("returnNonCloneable.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnNonCloneable.js")
);

var message = "foo";
var parameters = {
Expand All @@ -119,7 +129,9 @@ describe("Core/TaskProcessor", function () {
});

it("successful task raises the taskCompletedEvent", function () {
taskProcessor = new TaskProcessor("returnParameters.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnParameters.js")
);

var parameters = {
prop: "blah",
Expand All @@ -145,7 +157,9 @@ describe("Core/TaskProcessor", function () {
});

it("unsuccessful task raises the taskCompletedEvent with error", function () {
taskProcessor = new TaskProcessor("returnNonCloneable.js");
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnNonCloneable.js")
);

var message = "foo";
var parameters = {
Expand Down Expand Up @@ -175,7 +189,9 @@ describe("Core/TaskProcessor", function () {

it("can load and compile web assembly module", function () {
var binaryUrl = absolutize("../Specs/TestWorkers/TestWasm/testWasm.wasm");
taskProcessor = new TaskProcessor("returnWasmConfig.js", 5);
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnWasmConfig.js", 5)
);
var promise = taskProcessor.initWebAssemblyModule({
modulePath: "TestWasm/testWasmWrapper",
wasmBinaryFile: binaryUrl,
Expand All @@ -193,7 +209,9 @@ describe("Core/TaskProcessor", function () {

it("uses a backup module if web assembly is not supported", function () {
var binaryUrl = absolutize("../Specs/TestWorkers/TestWasm/testWasm.wasm");
taskProcessor = new TaskProcessor("returnWasmConfig.js", 5);
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnWasmConfig.js", 5)
);

spyOn(FeatureDetection, "supportsWebAssembly").and.returnValue(false);

Expand All @@ -212,7 +230,9 @@ describe("Core/TaskProcessor", function () {

it("throws runtime error if web assembly is not supported and no backup is provided", function () {
var binaryUrl = absolutize("../Specs/TestWorkers/TestWasm/testWasm.wasm");
taskProcessor = new TaskProcessor("returnWasmConfig.js", 5);
taskProcessor = new TaskProcessor(
absolutize("../Specs/TestWorkers/returnWasmConfig.js", 5)
);

spyOn(FeatureDetection, "supportsWebAssembly").and.returnValue(false);

Expand Down