Skip to content

Commit

Permalink
Formalize interCom comands. Make WorkerTaskDefaultWorker and comRouti…
Browse files Browse the repository at this point in the history
…ng compatible
  • Loading branch information
kaisalmen committed Oct 31, 2023
1 parent c809734 commit 08dfe8d
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 138 deletions.
32 changes: 22 additions & 10 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
},
"dependencies": {
"lil-gui": "~0.18.2",
"wtd-core": "~2.4.0-next.0",
"wtd-three-ext": "~2.4.0-next.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"
},
"devDependencies": {
"@types/three": "~0.158.1",
"http-server": "~14.1.1"
},
"repository": {
Expand Down
13 changes: 7 additions & 6 deletions packages/examples/src/infinite/PotentiallyInfiniteExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
WorkerTaskMessageType,
unpack,
pack,
WorkerTaskCommandResponse,
} from 'wtd-core';
import {
MaterialStore,
Expand Down Expand Up @@ -476,12 +477,12 @@ class PotentiallyInfiniteExample {

const wtm = unpack(message, false);
switch (wtm.cmd) {
case 'initComplete':
case WorkerTaskCommandResponse.INIT_COMPLETE:
console.log('Init Completed: ' + wtm.id);
break;

case 'execComplete':
case 'intermediate':
case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
case WorkerTaskCommandResponse.INTERMEDIATE_CONFIRM:
// were are getting raw vertex buffers here
if (wtm.payloads.length > 0) {
if (taskDescr.name === 'OBJLoader2WorkerModule' && wtm.payloads.length === 1) {
Expand Down Expand Up @@ -520,7 +521,7 @@ class PotentiallyInfiniteExample {
this.addMesh(mesh, wtm.id);
}
else {
if (wtm.cmd !== 'execComplete') {
if (wtm.cmd !== WorkerTaskCommandResponse.EXECUTE_COMPLETE) {
// This is the end-point for the execution
//console.log(`DataTransport: name: ${payload.name} id: ${payload.id} cmd: ${payload.cmd} workerId: ${payload.workerId}`);
console.error('Provided payload.type did not match: ' + wtm.cmd);
Expand Down Expand Up @@ -606,7 +607,7 @@ class PotentiallyInfiniteExample {
class SimpleBlobWorker {

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

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

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

Expand Down
9 changes: 5 additions & 4 deletions packages/examples/src/threejs/Threejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls

import {
DataPayload,
WorkerTaskCommandResponse,
WorkerTaskDirector,
WorkerTaskMessage,
WorkerTaskMessageType,
Expand Down Expand Up @@ -214,7 +215,7 @@ class WorkerTaskDirectorExample {
onComplete: (m: WorkerTaskMessageType) => {
this.processMessage(m);
},
onIntermediate: (m: WorkerTaskMessageType) => {
onIntermediateConfirm: (m: WorkerTaskMessageType) => {
this.processMessage(m);
}
});
Expand All @@ -240,15 +241,15 @@ class WorkerTaskDirectorExample {
private processMessage(message: WorkerTaskMessageType) {
const wtm = unpack(message, false);
switch (wtm.cmd) {
case 'intermediate':
case 'execComplete':
case WorkerTaskCommandResponse.INTERMEDIATE_CONFIRM:
case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
if (wtm.payloads.length === 1) {
this.buildMesh(wtm.id, wtm.payloads[0] as MeshPayload);
}
else if (wtm.payloads.length === 2) {
this.buildMesh(wtm.id, wtm.payloads[0] as MeshPayload, wtm.payloads[1] as MaterialsPayload);
}
if (wtm.cmd === 'execComplete') {
if (wtm.cmd === WorkerTaskCommandResponse.EXECUTE_COMPLETE) {
console.log(`execComplete: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion packages/examples/src/transferables/TransferablesTestbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls

import {
DataPayload,
WorkerTaskCommandResponse,
WorkerTaskDirector,
WorkerTaskMessage,
WorkerTaskMessageType,
Expand Down Expand Up @@ -268,7 +269,7 @@ class TransferablesTestbed {
private processMessage(message: WorkerTaskMessageType) {
let wtm;
switch (message.cmd) {
case 'execComplete':
case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
console.log(`TransferableTestbed#execComplete: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);

wtm = unpack(message, false);
Expand Down
26 changes: 12 additions & 14 deletions packages/examples/src/worker/Com1Worker.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import {
RawPayload,
WorkerTaskCommandRequest,
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

export class Com1Worker extends WorkerTaskDefaultWorker {

intermediate(message: WorkerTaskMessageType): void {
interComIntermediate(message: WorkerTaskMessageType): void {
const rawPayload = message.payloads[0] as RawPayload;
console.log(`Worker2 said: ${rawPayload.message.raw.hello}`);

// after receiving the message from Com2Worker, send execComplete to main
const execComplete = createFromExisting(message, 'execComplete');
const payload = new RawPayload();
payload.message.raw = { hello: 'Worker 1 finished!' };
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const payload = new RawPayload({ hello: 'Worker 1 finished!' });
execComplete.addPayload(payload);

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

execute(message: WorkerTaskMessageType) {
const port = (message.payloads[0] as RawPayload).message.raw.port as MessagePort;
// register the default com-routing function for inter-worker communication
port.onmessage = message => worker.comRouting(message);
this.registerPort('com2', message.payloads[0]);

// send message with cmd 'intermediate' to Com2Worker
const sendWorker2 = createFromExisting(message, 'intermediate');
const payload = new RawPayload();
payload.message.raw = { hello: 'Hi Worker 2!' };
// send message with cmd 'interComIntermediate' to Com2Worker
const sendWorker2 = createFromExisting(message, WorkerTaskCommandRequest.INTERCOM_INTERMEDIATE);
const payload = new RawPayload({ hello: 'Hi Worker 2!' });
sendWorker2.addPayload(payload);
port.postMessage(sendWorker2);

this.postMessageOnPort('com2', sendWorker2);
}
}

Expand Down
26 changes: 12 additions & 14 deletions packages/examples/src/worker/Com2Worker.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import {
RawPayload,
WorkerTaskCommandRequest,
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

export class Com2Worker extends WorkerTaskDefaultWorker {

intermediate(message: WorkerTaskMessageType): void {
interComIntermediate(message: WorkerTaskMessageType): void {
const rawPayload = message.payloads[0] as RawPayload;
console.log(`Worker1 said: ${rawPayload.message.raw.hello}`);

// after receiving the message from Com1Worker, send execComplete to main
const execComplete = createFromExisting(message, 'execComplete');
const payload = new RawPayload();
payload.message.raw = { hello: 'Worker 2 finished!' };
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const payload = new RawPayload({ hello: 'Worker 2 finished!' });
execComplete.addPayload(payload);

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

execute(message: WorkerTaskMessageType) {
const port = (message.payloads[0] as RawPayload).message.raw.port as MessagePort;
// register the default com-routing function for inter-worker communication
port.onmessage = message => worker.comRouting(message);
this.registerPort('com1', message.payloads[0]);

// send message with cmd 'intermediate' to Com1Worker
const sendWorker1 = createFromExisting(message, 'intermediate');
const payload = new RawPayload();
payload.message.raw = { hello: 'Hi Worker 1!' };
// send message with cmd 'interComIntermediate' to Com1Worker
const sendWorker1 = createFromExisting(message, WorkerTaskCommandRequest.INTERCOM_INTERMEDIATE);
const payload = new RawPayload({ hello: 'Hi Worker 1!' });
sendWorker1.addPayload(payload);
port.postMessage(sendWorker1);

this.postMessageOnPort('com1', sendWorker1);
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/examples/src/worker/HelloWorldThreeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SphereGeometry } from 'three';
import {
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting,
Expand All @@ -16,7 +17,7 @@ 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, 'initComplete');
const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
}

Expand All @@ -33,7 +34,7 @@ export class HelloWorlThreedWorker extends WorkerTaskDefaultWorker {
const meshPayload = new MeshPayload();
meshPayload.setBufferGeometry(bufferGeometry, 0);

const execComplete = createFromExisting(message, 'execComplete');
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
execComplete.addPayload(meshPayload);

const transferables = pack(execComplete.payloads, false);
Expand Down
5 changes: 3 additions & 2 deletions packages/examples/src/worker/HelloWorldWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DataPayload,
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting
Expand All @@ -10,7 +11,7 @@ declare const self: DedicatedWorkerGlobalScope;
export class HelloWorldWorker extends WorkerTaskDefaultWorker {

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

Expand All @@ -26,7 +27,7 @@ export class HelloWorldWorker extends WorkerTaskDefaultWorker {
hello: 'say hello'
};

const execComplete = createFromExisting(message, 'execComplete');
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
execComplete.addPayload(dataPayload);

// no need to pack as there aren't any buffers used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BufferGeometry
} from 'three';
import {
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting,
Expand All @@ -19,10 +20,10 @@ class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {
private bufferGeometry?: BufferGeometry = undefined;

init(message: WorkerTaskMessageType) {
const wtm = unpack(message, true);
const wtm = unpack(message, false);
this.bufferGeometry = (wtm.payloads[0] as MeshPayload).message.bufferGeometry as BufferGeometry;

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

Expand Down Expand Up @@ -54,7 +55,7 @@ class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {
}
};

const execComplete = createFromExisting(message, 'execComplete');
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
execComplete.addPayload(meshPayload);

const transferables = pack(execComplete.payloads, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MeshPhongMaterial
} from 'three';
import {
WorkerTaskCommandResponse,
WorkerTaskDefaultWorker,
WorkerTaskMessageType,
createFromExisting,
Expand All @@ -18,7 +19,7 @@ import {
class InfiniteWorkerInternalGeometry extends WorkerTaskDefaultWorker {

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

Expand Down Expand Up @@ -47,7 +48,7 @@ class InfiniteWorkerInternalGeometry extends WorkerTaskDefaultWorker {
const meshPayload = new MeshPayload();
meshPayload.setBufferGeometry(bufferGeometry, 2);

const execComplete = createFromExisting(message, 'execComplete');
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
execComplete.addPayload(meshPayload);
execComplete.addPayload(materialsPayload);

Expand Down
Loading

0 comments on commit 08dfe8d

Please sign in to comment.