Skip to content

Commit

Permalink
WorkerTaskMessage uuid handling is done internal. WorkerTaskMessageCo…
Browse files Browse the repository at this point in the history
…nfig is only used for config
  • Loading branch information
kaisalmen committed Dec 2, 2023
1 parent a8192c3 commit 4cefe66
Show file tree
Hide file tree
Showing 27 changed files with 704 additions and 521 deletions.
622 changes: 400 additions & 222 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
"npm": "9.8.1"
},
"devDependencies": {
"@types/node": "~18.18.8",
"@typescript-eslint/eslint-plugin": "~6.9.1",
"@types/node": "~18.18.14",
"@typescript-eslint/eslint-plugin": "~6.13.1",
"editorconfig": "~2.0.0",
"eslint": "~8.52.0",
"eslint": "~8.55.0",
"shx": "~0.3.4",
"typedoc": "~0.25.3",
"typedoc-plugin-markdown": "~3.17.0",
"typescript": "~5.2.2",
"vite": "~4.5.0",
"typedoc": "~0.25.4",
"typedoc-plugin-markdown": "~3.17.1",
"typescript": "~5.3.2",
"vite": "~5.0.4",
"vitest": "~0.34.6"
},
"workspaces": [
Expand Down
10 changes: 5 additions & 5 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"preview": "http-server ./preview"
},
"dependencies": {
"lil-gui": "~0.19.0",
"three": "~0.158.0",
"wtd-core": "~3.0.0-next.3",
"wtd-three-ext": "~3.0.0-next.3",
"lil-gui": "~0.19.1",
"three": "~0.159.0",
"wtd-core": "~3.0.0-next.4",
"wtd-three-ext": "~3.0.0-next.4",
"wwobjloader2": "6.2.0-next.4"
},
"devDependencies": {
"@types/three": "~0.158.1",
"@types/three": "~0.159.0",
"http-server": "~14.1.1"
},
"repository": {
Expand Down
7 changes: 3 additions & 4 deletions packages/examples/src/com/WorkerCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
RawPayload,
WorkerTask,
WorkerTaskMessage,
WorkerTaskMessageConfig,
initChannel,
initOffscreenCanvas,
registerResizeHandler
Expand Down Expand Up @@ -96,14 +95,14 @@ class HelloWorldStandardWorkerExample {
setTimeout(async () => {
promises = [];
promises.push(workerTaskCom1.executeWorker({
message: new WorkerTaskMessage()
message: WorkerTaskMessage.createEmpty()
}));
promises.push(workerTaskCom2.executeWorker({
message: new WorkerTaskMessage()
message: WorkerTaskMessage.createEmpty()
}));

const results = await Promise.all(promises);
results.forEach((message: WorkerTaskMessageConfig) => {
results.forEach((message: WorkerTaskMessage) => {
console.log('Received final command: ' + message.cmd);
if (message.payloads) {
const rawPayload = message.payloads[0] as RawPayload;
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/helloWorld/HelloWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HelloWorldModuleWorkerExample {
// execute worker without init
const t0 = performance.now();
const resultExec = await this.workerTaskDirector.enqueueForExecution(taskName, {
message: new WorkerTaskMessage(),
message: WorkerTaskMessage.createEmpty(),
});

const wtm = WorkerTaskMessage.unpack(resultExec, false);
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/helloWorld/HelloWorldWorkerTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HelloWorldWorkerTaskExample {

const t0 = performance.now();
// once the init Promise is done enqueue the execution
const execMessage = new WorkerTaskMessage();
const execMessage = WorkerTaskMessage.createEmpty();
const resultExec = await workerTask.executeWorker({
message: execMessage
});
Expand Down
60 changes: 24 additions & 36 deletions packages/examples/src/infinite/PotentiallyInfiniteExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
WorkerTaskDirector,
DataPayload,
WorkerTaskMessage,
WorkerTaskMessageConfig,
WorkerTaskCommandResponse,
createWorkerBlob,
} from 'wtd-core';
Expand Down Expand Up @@ -96,11 +95,13 @@ class PotentiallyInfiniteExample {
fov: 45
};
private controls: TrackballControls;
private workerTaskDirector: WorkerTaskDirector = new WorkerTaskDirector();
private verbose = false;
private workerTaskDirector: WorkerTaskDirector = new WorkerTaskDirector({
verbose: this.verbose
});

// configure all task that shall be usable on register to the WorkerTaskDirector
taskSimpleBlobWorker = {
id: 0,
name: 'simpleBlobWorker',
use: true,
workerType: 'module',
Expand All @@ -113,7 +114,6 @@ class PotentiallyInfiniteExample {
workerCount: 6
} as TaskDescription;
taskInfiniteWorkerInternalGeometry = {
id: 1,
name: 'InfiniteWorkerInternalGeometry',
use: true,
workerType: 'module',
Expand All @@ -122,7 +122,6 @@ class PotentiallyInfiniteExample {
workerCount: 10
} as TaskDescription;
taskInfiniteWorkerExternalGeometry = {
id: 2,
name: 'InfiniteWorkerExternalGeometry',
use: true,
workerType: 'module',
Expand All @@ -131,7 +130,6 @@ class PotentiallyInfiniteExample {
workerCount: 8
} as TaskDescription;
taskObjLoader2Worker = {
id: 3,
name: 'OBJLoader2WorkerModule',
modelName: 'female02',
use: true,
Expand All @@ -146,7 +144,7 @@ class PotentiallyInfiniteExample {
private materialStore = new MaterialStore(true);
private tasksToUse: TaskDescription[] = [];
private executions: Array<Promise<unknown>> = [];
private objectsUsed = new Map<number, { name: string, pos: Vector3 }>();
private objectsUsed = new Map<string, { name: string, pos: Vector3 }>();
private meshesAdded: string[] = [];
private removeCount = 50;
numberOfMeshesToKeep = 750;
Expand Down Expand Up @@ -208,7 +206,9 @@ class PotentiallyInfiniteExample {
}

resetAppContext() {
this.workerTaskDirector = new WorkerTaskDirector();
this.workerTaskDirector = new WorkerTaskDirector({
verbose: this.verbose
});

this.tasksToUse = [];
this.executions = [];
Expand Down Expand Up @@ -298,8 +298,7 @@ class PotentiallyInfiniteExample {
},
maxParallelExecutions: taskDescr.workerCount
});
const initMessage = new WorkerTaskMessage({
id: taskDescr.id,
const initMessage = WorkerTaskMessage.createNew({
name: taskDescr.name
});
awaiting.push(this.workerTaskDirector.initTaskType(taskDescr.name, {
Expand All @@ -321,8 +320,7 @@ class PotentiallyInfiniteExample {
maxParallelExecutions: taskDescr.workerCount
});

const initMessage = new WorkerTaskMessage({
id: taskDescr.id,
const initMessage = WorkerTaskMessage.createNew({
name: taskDescr.name
});
awaiting.push(this.workerTaskDirector.initTaskType(taskDescr.name, {
Expand All @@ -346,8 +344,7 @@ class PotentiallyInfiniteExample {

const torus = new TorusGeometry(25, 8, 16, 100);
torus.name = 'torus';
const initMessage = new WorkerTaskMessage({
id: taskDescr.id,
const initMessage = WorkerTaskMessage.createNew({
name: taskDescr.name
});
const meshPayload = new MeshPayload();
Expand Down Expand Up @@ -396,8 +393,7 @@ class PotentiallyInfiniteExample {
this.materialStore.addMaterialsFromObject(materialCreator.materials, false);
const buffer = results[results.length - 1] as string | ArrayBuffer;

const initMessage = new WorkerTaskMessage({
id: 0,
const initMessage = WorkerTaskMessage.createNew({
name: 'OBJLoader2WorkerModule'
});
const dataPayload = new DataPayload();
Expand Down Expand Up @@ -437,7 +433,6 @@ class PotentiallyInfiniteExample {
}

console.time('start');
let globalCount = 0;
const taskSelector = this.createTaskSelector();

for (let j = 0; j < this.loopCount && !this.abort; j++) {
Expand All @@ -447,23 +442,16 @@ class PotentiallyInfiniteExample {
const indexToUse = Math.floor(Math.random() * taskSelector.totalWorkers);
const taskDescr = taskSelector.taskSelectorArray[indexToUse];

const execMessage = new WorkerTaskMessage({
id: globalCount
});
const dataPayload = new DataPayload();
dataPayload.message.params = {
modelName: taskDescr.name
};
execMessage.addPayload(dataPayload);

const promise = this.workerTaskDirector.enqueueForExecution(taskDescr.name, {
message: execMessage,
message: WorkerTaskMessage.fromPayload(dataPayload),
onComplete: data => this.processMessage(taskDescr, data),
onIntermediateConfirm: data => this.processMessage(taskDescr, data)
});
this.executions.push(promise);

globalCount++;
}
await Promise.all(this.executions);
this.executions = [];
Expand Down Expand Up @@ -497,7 +485,7 @@ class PotentiallyInfiniteExample {
* @param {object} payload Message received from worker
* @private
*/
private processMessage(taskDescr: TaskDescription, message: WorkerTaskMessageConfig | Error) {
private processMessage(taskDescr: TaskDescription, message: WorkerTaskMessage | Error) {
let material: Material | Material[] | undefined;
let meshPayload: MeshPayload;
let materialsPayload: MaterialsPayload;
Expand All @@ -510,7 +498,7 @@ class PotentiallyInfiniteExample {
const wtm = WorkerTaskMessage.unpack(message, false);
switch (wtm.cmd) {
case WorkerTaskCommandResponse.INIT_COMPLETE:
console.log('Init Completed: ' + wtm.id);
console.log('Init Completed: ' + wtm.uuid);
break;

case WorkerTaskCommandResponse.EXECUTE_COMPLETE:
Expand Down Expand Up @@ -550,7 +538,7 @@ class PotentiallyInfiniteExample {
}
mesh = new Mesh(meshPayload.message.bufferGeometry as BufferGeometry, material);
}
this.addMesh(mesh, wtm.id ?? 0);
this.addMesh(mesh, wtm.uuid ?? 'unknown');
}
else {
if (wtm.cmd !== WorkerTaskCommandResponse.EXECUTE_COMPLETE) {
Expand All @@ -563,16 +551,16 @@ class PotentiallyInfiniteExample {
break;

default:
console.error(`${wtm.id}: Received unknown command: ${wtm.cmd}`);
console.error(`${wtm.uuid}: Received unknown command: ${wtm.cmd}`);
break;
}
}

/**
* Add mesh at random position, but keep sub-meshes of an object together
*/
private addMesh(mesh: Mesh, id: number) {
const storedPos = this.objectsUsed.get(id);
private addMesh(mesh: Mesh, uuid: string) {
const storedPos = this.objectsUsed.get(uuid);
let pos;
if (storedPos) {
pos = storedPos.pos;
Expand All @@ -582,10 +570,10 @@ class PotentiallyInfiniteExample {
pos.applyAxisAngle(this.baseVectorX, 2 * Math.PI * Math.random());
pos.applyAxisAngle(this.baseVectorY, 2 * Math.PI * Math.random());
pos.applyAxisAngle(this.baseVectorZ, 2 * Math.PI * Math.random());
this.objectsUsed.set(id, { name: mesh.name, pos: pos });
this.objectsUsed.set(uuid, { name: mesh.name, pos: pos });
}
mesh.position.set(pos.x, pos.y, pos.z);
mesh.name = id + '' + mesh.name;
mesh.name = uuid + '' + mesh.name;
this.scene.add(mesh);
this.meshesAdded.push(mesh.name);
}
Expand Down Expand Up @@ -638,12 +626,12 @@ class PotentiallyInfiniteExample {
// Simplest way to define a worker, but can't be a module worker
class SimpleBlobWorker {

init(message: WorkerTaskMessageConfig) {
init(message: WorkerTaskMessage) {
message.cmd = 'initComplete';
self.postMessage(message);
}

execute(message: WorkerTaskMessageConfig) {
execute(message: WorkerTaskMessage) {
// burn some time
for (let i = 0; i < 25000000; i++) {
i++;
Expand All @@ -667,7 +655,7 @@ class SimpleBlobWorker {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
comRouting(message: MessageEvent<any>) {
const wtmt = (message as MessageEvent).data as WorkerTaskMessageConfig;
const wtmt = (message as MessageEvent).data as WorkerTaskMessage;
if (wtmt) {
if (wtmt.cmd === 'init') {
this.init(wtmt);
Expand Down
Loading

0 comments on commit 4cefe66

Please sign in to comment.