Skip to content

Commit

Permalink
Allow execution of any function in comRouting. Clean API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Nov 4, 2023
1 parent 44d0227 commit 256640a
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 250 deletions.
22 changes: 11 additions & 11 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"build": "npm run build --workspaces",
"dev": "vite",
"dev:debug": "vite --debug --force",
"release:prepare": "npm run reset:repo && npm ci && npm run build && npm run doc",
"release:prepare": "npm run reset:repo && npm ci && npm run lint && npm run build && npm run doc",
"test": "vitest",
"reset:repo:dry": "git clean -f -d -x --dry-run",
"reset:repo": "git clean -f -d -x"
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"dependencies": {
"lil-gui": "~0.19.0",
"three": "~0.158.0",
"wtd-core": "~2.4.0-next.3",
"wtd-three-ext": "~2.4.0-next.3",
"wwobjloader2": "6.2.0-next.3"
"wtd-core": "~2.4.0-next.4",
"wtd-three-ext": "~2.4.0-next.4",
"wwobjloader2": "6.2.0-next.4"
},
"devDependencies": {
"@types/three": "~0.158.1",
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/helloWorld/HelloWorld.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
WorkerTaskDirector,
WorkerTaskMessage,
WorkerTaskMessageType,
unpack
WorkerTaskMessageType
} from 'wtd-core';

/**
Expand Down Expand Up @@ -37,7 +36,7 @@ class HelloWorldModuleWorkerExample {
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => {
const wtm = unpack(m, false);
const wtm = WorkerTaskMessage.unpack(m, false);
console.log(wtm);
if (wtm.payloads.length === 1) {
console.log(wtm.payloads[0]);
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/helloWorld/HelloWorldWorkerTask.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
WorkerTask,
WorkerTaskMessage,
WorkerTaskMessageType,
unpack
WorkerTaskMessageType
} from 'wtd-core';

/**
Expand Down Expand Up @@ -33,7 +32,7 @@ class HelloWorldWorkerTaskExample {
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => {
const wtm = unpack(m, false);
const wtm = WorkerTaskMessage.unpack(m, false);
console.log(wtm);
if (wtm.payloads.length === 1) {
console.log(wtm.payloads[0]);
Expand Down
12 changes: 5 additions & 7 deletions packages/examples/src/infinite/PotentiallyInfiniteExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import {
GUI
} from 'lil-gui';
import {
WorkerTask,
WorkerTaskDirector,
DataPayload,
WorkerTaskMessage,
WorkerTaskMessageType,
unpack,
pack,
WorkerTaskCommandResponse,
createWorkerBlob,
} from 'wtd-core';
import {
MaterialStore,
Expand Down Expand Up @@ -107,7 +105,7 @@ class PotentiallyInfiniteExample {
use: true,
module: true,
blob: true,
workerUrl: WorkerTask.createWorkerBlob([`${SimpleBlobWorker.toString()}
workerUrl: createWorkerBlob([`${SimpleBlobWorker.toString()}
worker = new SimpleBlobWorker();
self.onmessage = message => worker.comRouting(message);
Expand Down Expand Up @@ -341,7 +339,7 @@ class PotentiallyInfiniteExample {
meshPayload.setBufferGeometry(torus, 0);

initMessage.addPayload(meshPayload);
const transferables = pack(initMessage.payloads, false);
const transferables = WorkerTaskMessage.pack(initMessage.payloads, false);
awaiting.push(this.workerTaskDirector.initTaskType(taskDescr.name, {
message: initMessage,
transferables,
Expand Down Expand Up @@ -389,7 +387,7 @@ class PotentiallyInfiniteExample {
dataPayload.message.buffers?.set('modelData', buffer as ArrayBufferLike);
initMessage.addPayload(dataPayload);

const transferables = pack(initMessage.payloads, false);
const transferables = WorkerTaskMessage.pack(initMessage.payloads, false);
await this.workerTaskDirector.initTaskType(initMessage.name, {
message: initMessage,
transferables,
Expand Down Expand Up @@ -487,7 +485,7 @@ class PotentiallyInfiniteExample {
return;
}

const wtm = unpack(message, false);
const wtm = WorkerTaskMessage.unpack(message, false);
switch (wtm.cmd) {
case WorkerTaskCommandResponse.INIT_COMPLETE:
console.log('Init Completed: ' + wtm.id);
Expand Down
8 changes: 3 additions & 5 deletions packages/examples/src/threejs/Threejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import {
WorkerTaskCommandResponse,
WorkerTaskDirector,
WorkerTaskMessage,
WorkerTaskMessageType,
pack,
unpack,
WorkerTaskMessageType
} from 'wtd-core';
import {
MaterialsPayload,
Expand Down Expand Up @@ -189,7 +187,7 @@ class WorkerTaskDirectorExample {
objLoaderInitMessage.addPayload(objLoaderDataPayload);
objLoaderInitMessage.addPayload(materialsPayload);

const transferables = pack(objLoaderInitMessage.payloads, false);
const transferables = WorkerTaskMessage.pack(objLoaderInitMessage.payloads, false);
await this.workerTaskDirector.initTaskType(objLoaderInitMessage.name, {
message: objLoaderInitMessage,
transferables,
Expand Down Expand Up @@ -245,7 +243,7 @@ class WorkerTaskDirectorExample {
* @private
*/
private processMessage(message: WorkerTaskMessageType) {
const wtm = unpack(message, false);
const wtm = WorkerTaskMessage.unpack(message, false);
switch (wtm.cmd) {
case WorkerTaskCommandResponse.INTERMEDIATE_CONFIRM:
case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
Expand Down
10 changes: 4 additions & 6 deletions packages/examples/src/transferables/TransferablesTestbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import {
WorkerTaskCommandResponse,
WorkerTaskDirector,
WorkerTaskMessage,
WorkerTaskMessageType,
pack,
unpack
WorkerTaskMessageType
} from 'wtd-core';
import {
MeshPayload, reconstructBuffer
Expand Down Expand Up @@ -222,7 +220,7 @@ class TransferablesTestbed {
meshPayload.setBufferGeometry(torus, 0);
initMessage.addPayload(meshPayload);

const transferables = pack(initMessage.payloads, false);
const transferables = WorkerTaskMessage.pack(initMessage.payloads, false);
return this.workerTaskDirector.initTaskType(initMessage.name, {
message: initMessage,
transferables,
Expand Down Expand Up @@ -261,7 +259,7 @@ class TransferablesTestbed {
segments: task.segments
};
execMessage.addPayload(dataPayload);
const transferables = pack(execMessage.payloads, false);
const transferables = WorkerTaskMessage.pack(execMessage.payloads, false);

return this.workerTaskDirector.enqueueWorkerExecutionPlan(task.name, {
message: execMessage,
Expand All @@ -278,7 +276,7 @@ class TransferablesTestbed {
case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
console.log(`TransferableTestbed#execComplete: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);

wtm = unpack(message, false);
wtm = WorkerTaskMessage.unpack(message, false);
if (wtm.payloads.length === 1) {

const payload = wtm.payloads[0];
Expand Down
14 changes: 7 additions & 7 deletions packages/examples/src/worker/Com1Worker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
comRouting,
InterComPortHandler,
InterComWorker,
RawPayload,
WorkerTaskCommandRequest,
WorkerTaskCommandResponse,
WorkerTaskMessage,
WorkerTaskMessageType,
WorkerTaskWorker,
comRouting,
createFromExisting
WorkerTaskWorker
} from 'wtd-core';

export class Com1Worker implements WorkerTaskWorker, InterComWorker {
Expand All @@ -19,7 +19,7 @@ export class Com1Worker implements WorkerTaskWorker, InterComWorker {
this.icph.registerPort('com2', message.payloads[0], message => comRouting(this, message));

// send initComplete to main
const initComplete = createFromExisting({} as WorkerTaskMessageType, WorkerTaskCommandResponse.INIT_COMPLETE);
const initComplete = WorkerTaskMessage.createFromExisting({} as WorkerTaskMessageType, WorkerTaskCommandResponse.INIT_COMPLETE);
const payload = new RawPayload({ hello: 'Worker 1 initComplete!' });
initComplete.addPayload(payload);

Expand All @@ -28,7 +28,7 @@ export class Com1Worker implements WorkerTaskWorker, InterComWorker {

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

Expand All @@ -40,7 +40,7 @@ export class Com1Worker implements WorkerTaskWorker, InterComWorker {
console.log(`Worker 2 said: ${rawPayload.message.raw.hello}`);

// after receiving the message from Com2Worker, send interComIntermediateConfirm to worker 2
const intermediateConfirm = createFromExisting(message, WorkerTaskCommandResponse.INTERCOM_INTERMEDIATE_CONFIRM);
const intermediateConfirm = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INTERCOM_INTERMEDIATE_CONFIRM);
const payload = new RawPayload({ confirmed: 'Hi Worker 2. I confirm!' });
intermediateConfirm.addPayload(payload);

Expand All @@ -52,7 +52,7 @@ export class Com1Worker implements WorkerTaskWorker, InterComWorker {
console.log(`Worker 2 confirmed: ${rawPayload.message.raw.confirmed}`);

// after receiving the interComIntermediateConfirm from Com2Worker, send execComplete to main
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const execComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const payload = new RawPayload({ finished: 'Hi Main. Worker 1 completed!' });
execComplete.addPayload(payload);
self.postMessage(execComplete);
Expand Down
14 changes: 7 additions & 7 deletions packages/examples/src/worker/Com2Worker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
comRouting,
InterComPortHandler,
InterComWorker,
RawPayload,
WorkerTaskCommandRequest,
WorkerTaskCommandResponse,
WorkerTaskMessage,
WorkerTaskMessageType,
WorkerTaskWorker,
comRouting,
createFromExisting
WorkerTaskWorker
} from 'wtd-core';

export class Com2Worker implements WorkerTaskWorker, InterComWorker {
Expand All @@ -19,7 +19,7 @@ export class Com2Worker implements WorkerTaskWorker, InterComWorker {
this.icph.registerPort('com1', message.payloads[0], message => comRouting(this, message));

// send initComplete to main
const initComplete = createFromExisting({} as WorkerTaskMessageType, WorkerTaskCommandResponse.INIT_COMPLETE);
const initComplete = WorkerTaskMessage.createFromExisting({} as WorkerTaskMessageType, WorkerTaskCommandResponse.INIT_COMPLETE);
const payload = new RawPayload({ hello: 'Worker 2 initComplete!' });
initComplete.addPayload(payload);

Expand All @@ -28,7 +28,7 @@ export class Com2Worker implements WorkerTaskWorker, InterComWorker {

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

Expand All @@ -40,7 +40,7 @@ export class Com2Worker implements WorkerTaskWorker, InterComWorker {
console.log(`Worker 1 said: ${rawPayload.message.raw.hello}`);

// after receiving the message from Com1Worker, send interComIntermediateConfirm to worker 2
const intermediateConfirm = createFromExisting(message, WorkerTaskCommandResponse.INTERCOM_INTERMEDIATE_CONFIRM);
const intermediateConfirm = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INTERCOM_INTERMEDIATE_CONFIRM);
const payload = new RawPayload({ confirmed: 'Hi Worker 1. I confirm!' });
intermediateConfirm.addPayload(payload);

Expand All @@ -52,7 +52,7 @@ export class Com2Worker implements WorkerTaskWorker, InterComWorker {
console.log(`Worker 1 confirmed: ${rawPayload.message.raw.confirmed}`);

// after receiving the interComIntermediateConfirm from Com1Worker, send execComplete to main
const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const execComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE);
const payload = new RawPayload({ finished: 'Hi Main. Worker 2 completed!' });
execComplete.addPayload(payload);
self.postMessage(execComplete);
Expand Down
13 changes: 6 additions & 7 deletions packages/examples/src/worker/HelloWorldThreeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SphereGeometry } from 'three';
import {
comRouting,
WorkerTaskCommandResponse,
WorkerTaskMessage,
WorkerTaskMessageType,
WorkerTaskWorker,
comRouting,
createFromExisting,
pack
WorkerTaskWorker
} from 'wtd-core';
import {
MeshPayload
Expand All @@ -16,7 +15,7 @@ export class HelloWorlThreedWorker implements WorkerTaskWorker {
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);
const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE);
self.postMessage(initComplete);
}

Expand All @@ -33,10 +32,10 @@ export class HelloWorlThreedWorker implements WorkerTaskWorker {
const meshPayload = new MeshPayload();
meshPayload.setBufferGeometry(bufferGeometry, 0);

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

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

Expand Down
Loading

0 comments on commit 256640a

Please sign in to comment.