Skip to content

Commit

Permalink
Fixed postMessage and SimpleBlobWorker
Browse files Browse the repository at this point in the history
kaisalmen committed Nov 2, 2023
1 parent 08dfe8d commit af1ab0e
Showing 18 changed files with 144 additions and 160 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@
<div style="padding: 5px">
<h1>WorkerTaskDirector: Examples</h1>

<a href="packages/examples/helloWorld.html">Hello World: Module Worker</a>
<a href="packages/examples/helloWorldWorkerTask.html">Hello World: WorkerTask</a>
<br>
<a href="packages/examples/helloWorldWorkerTask.html">Hello World: Worker Task Only</a>
<a href="packages/examples/helloWorld.html">Hello World: WorkerTaskDirector</a>
<br>
<a href="packages/examples/workerCom.html">Inter-Worker Communication</a>
<br>
203 changes: 101 additions & 102 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@
"compile": "tsc --build tsconfig.build.json",
"watch": "tsc --build tsconfig.build.json --watch",
"build": "npm run build --workspaces",
"build:libs": "npm run build --workspace packages/wtd-core && npm run build --workspace packages/wtd-three-ext",
"dev": "vite",
"dev:debug": "vite --debug --force",
"release:prepare": "npm run reset:repo && npm ci && npm run build && npm run doc",
@@ -39,13 +38,13 @@
"npm": "9.8.1"
},
"devDependencies": {
"@types/node": "~18.18.6",
"@typescript-eslint/eslint-plugin": "~6.8.0",
"@types/node": "~18.18.8",
"@typescript-eslint/eslint-plugin": "~6.9.1",
"editorconfig": "~2.0.0",
"eslint": "~8.52.0",
"shx": "~0.3.4",
"typedoc": "~0.25.2",
"typedoc-plugin-markdown": "~3.16.0",
"typedoc": "~0.25.3",
"typedoc-plugin-markdown": "~3.17.0",
"typescript": "~5.2.2",
"vite": "~4.5.0",
"vitest": "~0.34.6"
4 changes: 2 additions & 2 deletions packages/examples/helloWorld.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World: Module Worker</title>
<title>Hello World: WorkerTaskDirector</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="./main.css">
</head>

<body>
<canvas id="example" style="width: 100%; height: 100vh;"></canvas>
<div id="info">Hello World: Module Worker</div>
<div id="info">Hello World: WorkerTaskDirector</div>
<script type="module" src="./src/helloWorld/HelloWorld.ts"></script>
</body>
</html>
8 changes: 4 additions & 4 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
@@ -28,11 +28,11 @@
"preview": "http-server ./preview"
},
"dependencies": {
"lil-gui": "~0.18.2",
"lil-gui": "~0.19.0",
"three": "~0.158.0",
"wtd-core": "~2.4.0-next.1",
"wtd-three-ext": "~2.4.0-next.1",
"wwobjloader2": "6.2.0-next.0"
"wtd-core": "~2.4.0-next.2",
"wtd-three-ext": "~2.4.0-next.2",
"wwobjloader2": "6.2.0-next.2"
},
"devDependencies": {
"@types/three": "~0.158.1",
4 changes: 2 additions & 2 deletions packages/examples/src/infinite/PotentiallyInfiniteExample.ts
Original file line number Diff line number Diff line change
@@ -607,7 +607,7 @@ class PotentiallyInfiniteExample {
class SimpleBlobWorker {

init(message: WorkerTaskMessageType) {
message.cmd = WorkerTaskCommandResponse.INIT_COMPLETE;
message.cmd = 'initComplete';
self.postMessage(message);
}

@@ -629,7 +629,7 @@ class SimpleBlobWorker {
};
message.payloads[0] = dataPayload;

message.cmd = WorkerTaskCommandResponse.EXECUTE_COMPLETE;
message.cmd = 'executeComplete';
self.postMessage(message);
}

6 changes: 2 additions & 4 deletions packages/examples/src/worker/HelloWorldThreeWorker.ts
Original file line number Diff line number Diff line change
@@ -10,15 +10,13 @@ import {
MeshPayload
} from 'wtd-three-ext';

declare const self: DedicatedWorkerGlobalScope;

export class HelloWorlThreedWorker extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
console.log(`HelloWorldWorker#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);

const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -38,7 +36,7 @@ export class HelloWorlThreedWorker extends WorkerTaskDefaultWorker {
execComplete.addPayload(meshPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}

}
6 changes: 2 additions & 4 deletions packages/examples/src/worker/HelloWorldWorker.ts
Original file line number Diff line number Diff line change
@@ -6,13 +6,11 @@ import {
createFromExisting
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

export class HelloWorldWorker extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -31,7 +29,7 @@ export class HelloWorldWorker extends WorkerTaskDefaultWorker {
execComplete.addPayload(dataPayload);

// no need to pack as there aren't any buffers used
self.postMessage(execComplete);
this.postMessage(execComplete);
}

}
Original file line number Diff line number Diff line change
@@ -13,8 +13,6 @@ import {
MeshPayload
} from 'wtd-three-ext';

declare const self: DedicatedWorkerGlobalScope;

class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {

private bufferGeometry?: BufferGeometry = undefined;
@@ -24,12 +22,12 @@ class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {
this.bufferGeometry = (wtm.payloads[0] as MeshPayload).message.bufferGeometry as BufferGeometry;

const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
if (!this.bufferGeometry) {
self.postMessage(new Error('No initial payload available'));
this.postMessage(new Error('No initial payload available'));
} else {
// clone before re-using as othewise transferables can not be obtained
const geometry = this.bufferGeometry.clone();
@@ -59,7 +57,7 @@ class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {
execComplete.addPayload(meshPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}
}
}
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class InfiniteWorkerInternalGeometry extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -53,7 +53,7 @@ class InfiniteWorkerInternalGeometry extends WorkerTaskDefaultWorker {
execComplete.addPayload(materialsPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}
}

8 changes: 3 additions & 5 deletions packages/examples/src/worker/OBJLoaderWorker.ts
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ import {
MeshPayload,
} from 'wtd-three-ext';

declare const self: DedicatedWorkerGlobalScope;

class OBJLoaderWorker extends WorkerTaskDefaultWorker {

private localData = {
@@ -42,7 +40,7 @@ class OBJLoaderWorker extends WorkerTaskDefaultWorker {
this.localData.materials = materialsPayload.message.materials;

const initComplete = createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}
}

@@ -85,12 +83,12 @@ class OBJLoaderWorker extends WorkerTaskDefaultWorker {
}

const transferables = pack(intermediate.payloads, false);
self.postMessage(intermediate, transferables);
this.postMessage(intermediate, transferables);
}

// signal complete
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
self.postMessage(execComplete);
this.postMessage(execComplete);
}

}
6 changes: 2 additions & 4 deletions packages/examples/src/worker/TransferableWorkerTest1.ts
Original file line number Diff line number Diff line change
@@ -8,15 +8,13 @@ import {
WorkerTaskCommandResponse
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

class TransferableWorkerTest1 extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
console.log(`TransferableWorkerTest1#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);

const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -33,7 +31,7 @@ class TransferableWorkerTest1 extends WorkerTaskDefaultWorker {
execComplete.addPayload(dataPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}

}
6 changes: 2 additions & 4 deletions packages/examples/src/worker/TransferableWorkerTest2.ts
Original file line number Diff line number Diff line change
@@ -8,15 +8,13 @@ import {
WorkerTaskCommandResponse
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

class TransferableWorkerTest2 extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
console.log(`TransferableWorkerTest2#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);

const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -34,7 +32,7 @@ class TransferableWorkerTest2 extends WorkerTaskDefaultWorker {
execComplete.addPayload(payloadOut);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}
}
}
6 changes: 2 additions & 4 deletions packages/examples/src/worker/TransferableWorkerTest3.ts
Original file line number Diff line number Diff line change
@@ -13,8 +13,6 @@ import {
packGeometryBuffers
} from 'wtd-three-ext';

declare const self: DedicatedWorkerGlobalScope;

class TransferableWorkerTest3 extends WorkerTaskDefaultWorker {

private context = {
@@ -30,7 +28,7 @@ class TransferableWorkerTest3 extends WorkerTaskDefaultWorker {
}

const initComplete = createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
this.postMessage(initComplete);
}

execute(message: WorkerTaskMessageType) {
@@ -53,7 +51,7 @@ class TransferableWorkerTest3 extends WorkerTaskDefaultWorker {
execComplete.addPayload(dataPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}
}
}
6 changes: 2 additions & 4 deletions packages/examples/src/worker/TransferableWorkerTest4.ts
Original file line number Diff line number Diff line change
@@ -12,14 +12,12 @@ import {
MeshPayload
} from 'wtd-three-ext';

declare const self: DedicatedWorkerGlobalScope;

class TransferableWorkerTest4 extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
console.log(`TransferableWorkerTest4#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);
message.cmd = WorkerTaskCommandResponse.INIT_COMPLETE;
self.postMessage(message);
this.postMessage(message);
}

execute(message: WorkerTaskMessageType) {
@@ -38,7 +36,7 @@ class TransferableWorkerTest4 extends WorkerTaskDefaultWorker {
execComplete.addPayload(meshPayload);

const transferables = pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
this.postMessage(execComplete, transferables);
}
}

2 changes: 1 addition & 1 deletion packages/wtd-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wtd-core",
"version": "2.4.0-next.1",
"version": "2.4.0-next.2",
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
10 changes: 6 additions & 4 deletions packages/wtd-core/src/WorkerTaskWorker.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@ import { Payload } from './Payload.js';
import { RawPayload } from './RawPayload.js';
import { WorkerTaskCommandRequest, WorkerTaskMessageType } from './WorkerTaskMessage.js';

declare const self: DedicatedWorkerGlobalScope;

export type WorkerTaskWorker = {

init(message: WorkerTaskMessageType): void;
@@ -82,8 +80,12 @@ export class WorkerTaskDefaultWorker implements InterComWorker {
}
}

postMessage(message: WorkerTaskMessageType, options?: StructuredSerializeOptions) {
self.postMessage(message, options);
postMessage(message: unknown, options?: StructuredSerializeOptions | Transferable[]) {
if (Array.isArray(options)) {
self.postMessage(message, options);
} else {
self.postMessage(message, options);
}
}

registerPort(name: string, payload: Payload | undefined) {
4 changes: 2 additions & 2 deletions packages/wtd-three-ext/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wtd-three-ext",
"version": "2.4.0-next.1",
"version": "2.4.0-next.2",
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
@@ -45,7 +45,7 @@
"npm": "9.8.1"
},
"dependencies": {
"wtd-core": "~2.4.0-next.1",
"wtd-core": "~2.4.0-next.2",
"three": "~0.158.0"
},
"devDependencies": {

0 comments on commit af1ab0e

Please sign in to comment.