diff --git a/package-lock.json b/package-lock.json index 10217b5..a37e879 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3594,13 +3594,13 @@ "link": true }, "node_modules/wwobjloader2": { - "version": "6.2.0-next.3", - "resolved": "https://registry.npmjs.org/wwobjloader2/-/wwobjloader2-6.2.0-next.3.tgz", - "integrity": "sha512-p/adxs08FiWXh+k07lFNSKVMt+4r7Xwz7FeIUfmjevB6TFouQ8cRyqAHllyGIGNsSUipmY4ZKX6XBTE8QV4HSw==", + "version": "6.2.0-next.4", + "resolved": "https://registry.npmjs.org/wwobjloader2/-/wwobjloader2-6.2.0-next.4.tgz", + "integrity": "sha512-FgsjTKMp+TOWjKRlCsT4vb/8/1ZjSGpPjZbxi60MAtV1pX9AKL5wJ/By1L+vtHP4rVUxrjqEhSY/3pBqvDUzkA==", "dependencies": { "three": "~0.158.0", - "wtd-core": "~2.4.0-next.3", - "wtd-three-ext": "~2.4.0-next.3" + "wtd-core": "~2.4.0-next.4", + "wtd-three-ext": "~2.4.0-next.4" }, "peerDependencies": { "three": ">= 0.137.5 < 1" @@ -3630,9 +3630,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", @@ -3640,15 +3640,15 @@ } }, "packages/wtd-core": { - "version": "2.4.0-next.3", + "version": "2.4.0-next.4", "license": "MIT" }, "packages/wtd-three-ext": { - "version": "2.4.0-next.3", + "version": "2.4.0-next.4", "license": "MIT", "dependencies": { "three": "~0.158.0", - "wtd-core": "~2.4.0-next.3" + "wtd-core": "~2.4.0-next.4" }, "devDependencies": { "@types/three": "~0.158.1" diff --git a/package.json b/package.json index 6716ef5..61ddc7d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/packages/examples/package.json b/packages/examples/package.json index 4a49848..3db6668 100644 --- a/packages/examples/package.json +++ b/packages/examples/package.json @@ -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", diff --git a/packages/examples/src/helloWorld/HelloWorld.ts b/packages/examples/src/helloWorld/HelloWorld.ts index 3737f2b..f0d6074 100644 --- a/packages/examples/src/helloWorld/HelloWorld.ts +++ b/packages/examples/src/helloWorld/HelloWorld.ts @@ -1,8 +1,7 @@ import { WorkerTaskDirector, WorkerTaskMessage, - WorkerTaskMessageType, - unpack + WorkerTaskMessageType } from 'wtd-core'; /** @@ -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]); diff --git a/packages/examples/src/helloWorld/HelloWorldWorkerTask.ts b/packages/examples/src/helloWorld/HelloWorldWorkerTask.ts index d244023..aca328a 100644 --- a/packages/examples/src/helloWorld/HelloWorldWorkerTask.ts +++ b/packages/examples/src/helloWorld/HelloWorldWorkerTask.ts @@ -1,8 +1,7 @@ import { WorkerTask, WorkerTaskMessage, - WorkerTaskMessageType, - unpack + WorkerTaskMessageType } from 'wtd-core'; /** @@ -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]); diff --git a/packages/examples/src/infinite/PotentiallyInfiniteExample.ts b/packages/examples/src/infinite/PotentiallyInfiniteExample.ts index 553a798..77578fd 100644 --- a/packages/examples/src/infinite/PotentiallyInfiniteExample.ts +++ b/packages/examples/src/infinite/PotentiallyInfiniteExample.ts @@ -23,14 +23,12 @@ import { GUI } from 'lil-gui'; import { - WorkerTask, WorkerTaskDirector, DataPayload, WorkerTaskMessage, WorkerTaskMessageType, - unpack, - pack, WorkerTaskCommandResponse, + createWorkerBlob, } from 'wtd-core'; import { MaterialStore, @@ -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); @@ -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, @@ -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, @@ -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); diff --git a/packages/examples/src/threejs/Threejs.ts b/packages/examples/src/threejs/Threejs.ts index 92eb6ab..d315db9 100644 --- a/packages/examples/src/threejs/Threejs.ts +++ b/packages/examples/src/threejs/Threejs.ts @@ -20,9 +20,7 @@ import { WorkerTaskCommandResponse, WorkerTaskDirector, WorkerTaskMessage, - WorkerTaskMessageType, - pack, - unpack, + WorkerTaskMessageType } from 'wtd-core'; import { MaterialsPayload, @@ -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, @@ -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: diff --git a/packages/examples/src/transferables/TransferablesTestbed.ts b/packages/examples/src/transferables/TransferablesTestbed.ts index 85d2c2f..85e22fc 100644 --- a/packages/examples/src/transferables/TransferablesTestbed.ts +++ b/packages/examples/src/transferables/TransferablesTestbed.ts @@ -19,9 +19,7 @@ import { WorkerTaskCommandResponse, WorkerTaskDirector, WorkerTaskMessage, - WorkerTaskMessageType, - pack, - unpack + WorkerTaskMessageType } from 'wtd-core'; import { MeshPayload, reconstructBuffer @@ -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, @@ -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, @@ -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]; diff --git a/packages/examples/src/worker/Com1Worker.ts b/packages/examples/src/worker/Com1Worker.ts index 6c4a1a0..7b09271 100644 --- a/packages/examples/src/worker/Com1Worker.ts +++ b/packages/examples/src/worker/Com1Worker.ts @@ -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 { @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/packages/examples/src/worker/Com2Worker.ts b/packages/examples/src/worker/Com2Worker.ts index 6a9c9c5..e2619b8 100644 --- a/packages/examples/src/worker/Com2Worker.ts +++ b/packages/examples/src/worker/Com2Worker.ts @@ -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 { @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/packages/examples/src/worker/HelloWorldThreeWorker.ts b/packages/examples/src/worker/HelloWorldThreeWorker.ts index 6d91413..e961160 100644 --- a/packages/examples/src/worker/HelloWorldThreeWorker.ts +++ b/packages/examples/src/worker/HelloWorldThreeWorker.ts @@ -1,11 +1,10 @@ import { SphereGeometry } from 'three'; import { + comRouting, WorkerTaskCommandResponse, + WorkerTaskMessage, WorkerTaskMessageType, - WorkerTaskWorker, - comRouting, - createFromExisting, - pack + WorkerTaskWorker } from 'wtd-core'; import { MeshPayload @@ -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); } @@ -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); } diff --git a/packages/examples/src/worker/HelloWorldWorker.ts b/packages/examples/src/worker/HelloWorldWorker.ts index 3c726f7..43d5b18 100644 --- a/packages/examples/src/worker/HelloWorldWorker.ts +++ b/packages/examples/src/worker/HelloWorldWorker.ts @@ -1,16 +1,16 @@ import { + comRouting, DataPayload, WorkerTaskCommandResponse, + WorkerTaskMessage, WorkerTaskMessageType, - WorkerTaskWorker, - comRouting, - createFromExisting + WorkerTaskWorker } from 'wtd-core'; export class HelloWorldWorker implements WorkerTaskWorker { init(message: WorkerTaskMessageType) { - const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); + const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } @@ -26,7 +26,7 @@ export class HelloWorldWorker implements WorkerTaskWorker { hello: 'say hello' }; - const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.addPayload(dataPayload); // no need to pack as there aren't any buffers used diff --git a/packages/examples/src/worker/InfiniteWorkerExternalGeometry.ts b/packages/examples/src/worker/InfiniteWorkerExternalGeometry.ts index 72cbfd0..f12983a 100644 --- a/packages/examples/src/worker/InfiniteWorkerExternalGeometry.ts +++ b/packages/examples/src/worker/InfiniteWorkerExternalGeometry.ts @@ -3,12 +3,10 @@ import { } from 'three'; import { WorkerTaskCommandResponse, + WorkerTaskMessage, WorkerTaskMessageType, WorkerTaskWorker, - comRouting, - createFromExisting, - pack, - unpack + comRouting } from 'wtd-core'; import { MeshPayload @@ -19,10 +17,10 @@ class InfiniteWorkerExternalGeometry implements WorkerTaskWorker { private bufferGeometry?: BufferGeometry = undefined; init(message: WorkerTaskMessageType) { - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); this.bufferGeometry = (wtm.payloads[0] as MeshPayload).message.bufferGeometry as BufferGeometry; - const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); + const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } @@ -54,10 +52,10 @@ class InfiniteWorkerExternalGeometry implements WorkerTaskWorker { } }; - 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); } } diff --git a/packages/examples/src/worker/InfiniteWorkerInternalGeometry.ts b/packages/examples/src/worker/InfiniteWorkerInternalGeometry.ts index 1d77a67..d035738 100644 --- a/packages/examples/src/worker/InfiniteWorkerInternalGeometry.ts +++ b/packages/examples/src/worker/InfiniteWorkerInternalGeometry.ts @@ -4,12 +4,11 @@ import { MeshPhongMaterial } from 'three'; import { + comRouting, WorkerTaskCommandResponse, + WorkerTaskMessage, WorkerTaskMessageType, - WorkerTaskWorker, - comRouting, - createFromExisting, - pack + WorkerTaskWorker } from 'wtd-core'; import { MaterialUtils, @@ -20,7 +19,7 @@ import { class InfiniteWorkerInternalGeometry implements WorkerTaskWorker { init(message: WorkerTaskMessageType) { - const initComplete = createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); + const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } @@ -49,11 +48,11 @@ class InfiniteWorkerInternalGeometry implements WorkerTaskWorker { const meshPayload = new MeshPayload(); meshPayload.setBufferGeometry(bufferGeometry, 2); - const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.addPayload(meshPayload); execComplete.addPayload(materialsPayload); - const transferables = pack(execComplete.payloads, false); + const transferables = WorkerTaskMessage.pack(execComplete.payloads, false); self.postMessage(execComplete, transferables); } } diff --git a/packages/examples/src/worker/OBJLoaderWorker.ts b/packages/examples/src/worker/OBJLoaderWorker.ts index cf8c69e..33fb245 100644 --- a/packages/examples/src/worker/OBJLoaderWorker.ts +++ b/packages/examples/src/worker/OBJLoaderWorker.ts @@ -4,15 +4,13 @@ import { } from 'three'; import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js'; import { + comRouting, AssociatedArrayType, DataPayload, WorkerTaskCommandResponse, + WorkerTaskMessage, WorkerTaskMessageType, - WorkerTaskWorker, - comRouting, - createFromExisting, - pack, - unpack + WorkerTaskWorker } from 'wtd-core'; import { MaterialsPayload, @@ -32,7 +30,7 @@ class OBJLoaderWorker implements WorkerTaskWorker { init(message: WorkerTaskMessageType) { console.log(`OBJLoaderWorker#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); if (wtm.payloads.length === 2) { const dataPayload = wtm.payloads[0] as DataPayload; const materialsPayload = wtm.payloads[1] as MaterialsPayload; @@ -40,7 +38,7 @@ class OBJLoaderWorker implements WorkerTaskWorker { this.localData.buffer = dataPayload.message.buffers?.get('modelData'); this.localData.materials = materialsPayload.message.materials; - const initComplete = createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE); + const initComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } } @@ -70,7 +68,7 @@ class OBJLoaderWorker implements WorkerTaskWorker { mesh.name = mesh.name + message.id; // signal intermediate feedback - const intermediate = createFromExisting(message, WorkerTaskCommandResponse.INTERMEDIATE_CONFIRM); + const intermediate = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INTERMEDIATE_CONFIRM); const meshPayload = new MeshPayload(); meshPayload.setMesh(mesh, 0); @@ -83,12 +81,12 @@ class OBJLoaderWorker implements WorkerTaskWorker { intermediate.addPayload(materialPayload); } - const transferables = pack(intermediate.payloads, false); + const transferables = WorkerTaskMessage.pack(intermediate.payloads, false); self.postMessage(intermediate, transferables); } // signal complete - const execComplete = createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.EXECUTE_COMPLETE); self.postMessage(execComplete); } diff --git a/packages/examples/src/worker/TransferableWorkerTest1.ts b/packages/examples/src/worker/TransferableWorkerTest1.ts index 2937c71..134a3b8 100644 --- a/packages/examples/src/worker/TransferableWorkerTest1.ts +++ b/packages/examples/src/worker/TransferableWorkerTest1.ts @@ -1,11 +1,9 @@ import { - WorkerTaskMessageType, + comRouting, DataPayload, - createFromExisting, - pack, - unpack, WorkerTaskCommandResponse, - comRouting, + WorkerTaskMessage, + WorkerTaskMessageType, WorkerTaskWorker } from 'wtd-core'; @@ -14,24 +12,24 @@ class TransferableWorkerTest1 implements WorkerTaskWorker { 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); + const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } execute(message: WorkerTaskMessageType) { console.log(`TransferableWorkerTest1#execute: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); const dataPayload = new DataPayload(); dataPayload.message.params = { data: new Uint32Array(32 * 1024 * 1024) }; - const execComplete = createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.addPayload(dataPayload); - const transferables = pack(execComplete.payloads, false); + const transferables = WorkerTaskMessage.pack(execComplete.payloads, false); self.postMessage(execComplete, transferables); } diff --git a/packages/examples/src/worker/TransferableWorkerTest2.ts b/packages/examples/src/worker/TransferableWorkerTest2.ts index 2de4d7e..eff7220 100644 --- a/packages/examples/src/worker/TransferableWorkerTest2.ts +++ b/packages/examples/src/worker/TransferableWorkerTest2.ts @@ -1,11 +1,9 @@ import { - WorkerTaskMessageType, + comRouting, DataPayload, - createFromExisting, - unpack, - pack, WorkerTaskCommandResponse, - comRouting, + WorkerTaskMessage, + WorkerTaskMessageType, WorkerTaskWorker } from 'wtd-core'; @@ -14,25 +12,25 @@ class TransferableWorkerTest2 implements WorkerTaskWorker { 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); + const initComplete = WorkerTaskMessage.createFromExisting(message, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } execute(message: WorkerTaskMessageType) { console.log(`TransferableWorkerTest2#execute: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); if (wtm.payloads.length === 1) { const payload = wtm.payloads[0] as DataPayload; if (payload.message.params) { const payloadOut = new DataPayload(); payloadOut.message.buffers?.set('data', new Uint32Array(32 * 1024 * 1024)); - const execComplete = createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.name = payload.message.params.name as string; execComplete.addPayload(payloadOut); - const transferables = pack(execComplete.payloads, false); + const transferables = WorkerTaskMessage.pack(execComplete.payloads, false); self.postMessage(execComplete, transferables); } } diff --git a/packages/examples/src/worker/TransferableWorkerTest3.ts b/packages/examples/src/worker/TransferableWorkerTest3.ts index 99e7c90..9288e36 100644 --- a/packages/examples/src/worker/TransferableWorkerTest3.ts +++ b/packages/examples/src/worker/TransferableWorkerTest3.ts @@ -1,12 +1,10 @@ import { BufferGeometry } from 'three'; import { - WorkerTaskMessageType, + comRouting, DataPayload, - createFromExisting, - unpack, - pack, WorkerTaskCommandResponse, - comRouting, + WorkerTaskMessage, + WorkerTaskMessageType, WorkerTaskWorker } from 'wtd-core'; import { @@ -23,12 +21,12 @@ class TransferableWorkerTest3 implements WorkerTaskWorker { init(message: WorkerTaskMessageType) { console.log(`TransferableWorkerTest3#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); if (wtm.payloads.length > 0) { this.context.initPayload = wtm.payloads[0] as MeshPayload; } - const initComplete = createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE); + const initComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.INIT_COMPLETE); self.postMessage(initComplete); } @@ -36,7 +34,7 @@ class TransferableWorkerTest3 implements WorkerTaskWorker { console.log(`TransferableWorkerTest3#execute: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); if (this.context.initPayload) { - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); // just put the buffers into the buffers of a DataPayload const bufferGeometry = this.context.initPayload.message.bufferGeometry; @@ -48,10 +46,10 @@ class TransferableWorkerTest3 implements WorkerTaskWorker { packGeometryBuffers(false, bufferGeometry as BufferGeometry, dataPayload.message.buffers); } - const execComplete = createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.addPayload(dataPayload); - const transferables = pack(execComplete.payloads, false); + const transferables = WorkerTaskMessage.pack(execComplete.payloads, false); self.postMessage(execComplete, transferables); } } diff --git a/packages/examples/src/worker/TransferableWorkerTest4.ts b/packages/examples/src/worker/TransferableWorkerTest4.ts index 44c7f3c..cd57b42 100644 --- a/packages/examples/src/worker/TransferableWorkerTest4.ts +++ b/packages/examples/src/worker/TransferableWorkerTest4.ts @@ -1,13 +1,11 @@ import { TorusKnotGeometry } from 'three'; import { - WorkerTaskMessageType, + comRouting, DataPayload, - createFromExisting, - unpack, - pack, WorkerTaskCommandResponse, - WorkerTaskWorker, - comRouting + WorkerTaskMessage, + WorkerTaskMessageType, + WorkerTaskWorker } from 'wtd-core'; import { MeshPayload @@ -24,7 +22,7 @@ class TransferableWorkerTest4 implements WorkerTaskWorker { execute(message: WorkerTaskMessageType) { console.log(`TransferableWorkerTest4#execute: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`); - const wtm = unpack(message, false); + const wtm = WorkerTaskMessage.unpack(message, false); if (wtm.payloads.length === 1) { const payload = wtm.payloads[0] as DataPayload; const bufferGeometry = new TorusKnotGeometry(20, 3, payload.message.params?.segments as number, payload.message.params?.segments as number); @@ -33,10 +31,10 @@ class TransferableWorkerTest4 implements WorkerTaskWorker { const meshPayload = new MeshPayload(); meshPayload.setBufferGeometry(bufferGeometry, 0); - const execComplete = createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); + const execComplete = WorkerTaskMessage.createFromExisting(wtm, WorkerTaskCommandResponse.EXECUTE_COMPLETE); execComplete.addPayload(meshPayload); - const transferables = pack(execComplete.payloads, false); + const transferables = WorkerTaskMessage.pack(execComplete.payloads, false); self.postMessage(execComplete, transferables); } } diff --git a/packages/wtd-core/package.json b/packages/wtd-core/package.json index 0d0239e..02c8aa2 100644 --- a/packages/wtd-core/package.json +++ b/packages/wtd-core/package.json @@ -1,6 +1,6 @@ { "name": "wtd-core", - "version": "2.4.0-next.3", + "version": "2.4.0-next.4", "license": "MIT", "type": "module", "main": "./dist/index.js", diff --git a/packages/wtd-core/src/DataPayload.ts b/packages/wtd-core/src/DataPayload.ts index fe16747..26a72b9 100644 --- a/packages/wtd-core/src/DataPayload.ts +++ b/packages/wtd-core/src/DataPayload.ts @@ -1,4 +1,5 @@ -import { AssociatedArrayType, Payload, PayloadHandler, PayloadRegister, fillTransferables } from './Payload.js'; +import { AssociatedArrayType, Payload, PayloadHandler, PayloadRegister } from './Payload.js'; +import { fillTransferables } from './utiilies.js'; export type ParameterizedMessage = { params?: AssociatedArrayType; diff --git a/packages/wtd-core/src/Payload.ts b/packages/wtd-core/src/Payload.ts index 4ca536c..33cf689 100644 --- a/packages/wtd-core/src/Payload.ts +++ b/packages/wtd-core/src/Payload.ts @@ -13,40 +13,3 @@ export type PayloadHandler = { export class PayloadRegister { static handler = new Map(); } - -export const fillTransferables = (buffers: IterableIterator, transferables: Transferable[], cloneBuffers: boolean) => { - for (const buffer of buffers) { - const potentialClone = cloneBuffers ? buffer.slice(0) : buffer; - - const outputBuffer = (potentialClone as Uint8Array).buffer; - if (outputBuffer) { - transferables.push(outputBuffer); - } - else { - transferables.push(potentialClone); - } - } -}; - -/** - * Applies values from parameter object via set functions or via direct assignment. - * - * @param {object} objToAlter The objToAlter instance - * @param {AssociatedArrayType} params The parameter object - * @param {boolean} forceCreation Force the creation of a property - */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export const applyProperties = (objToAlter: any, params?: AssociatedArrayType, forceCreation?: boolean) => { - if (!params) return; - - for (const [k, v] of Object.entries(params)) { - const funcName = 'set' + k.substring(0, 1).toLocaleUpperCase() + k.substring(1); - - if (Object.prototype.hasOwnProperty.call(objToAlter, funcName) && typeof objToAlter[funcName] === 'function') { - objToAlter[funcName] = v; - } - else if (Object.prototype.hasOwnProperty.call(objToAlter, k) || forceCreation) { - objToAlter[k] = v; - } - } -}; diff --git a/packages/wtd-core/src/WorkerTask.ts b/packages/wtd-core/src/WorkerTask.ts index 8f91622..1dee8a5 100644 --- a/packages/wtd-core/src/WorkerTask.ts +++ b/packages/wtd-core/src/WorkerTask.ts @@ -61,17 +61,6 @@ export class WorkerTask { this.executing = executing; } - static createWorkerBlob(code: string[]) { - const simpleWorkerBlob = new Blob(code, { type: 'application/javascript' }); - return window.URL.createObjectURL(simpleWorkerBlob); - } - - static async wait(milliseconds: number) { - return new Promise(resolve => { - setTimeout(resolve, milliseconds); - }); - } - async initWorker(plan: WorkerInitPlan) { return new Promise((resolve, reject) => { this.worker = this.createWorker(); diff --git a/packages/wtd-core/src/WorkerTaskMessage.ts b/packages/wtd-core/src/WorkerTaskMessage.ts index 720b18a..6b27f85 100644 --- a/packages/wtd-core/src/WorkerTaskMessage.ts +++ b/packages/wtd-core/src/WorkerTaskMessage.ts @@ -61,32 +61,33 @@ export class WorkerTaskMessage implements WorkerTaskMessageType { this.payloads.push(payloads); } } -} -export const createFromExisting = (message: WorkerTaskMessageType, cmd?: WorkerTaskCommands) => { - const wtm = new WorkerTaskMessage(message); - if (cmd) { - wtm.cmd = cmd; + static createFromExisting(message: WorkerTaskMessageType, cmd?: WorkerTaskCommands) { + const wtm = new WorkerTaskMessage(message); + if (cmd) { + wtm.cmd = cmd; + } + return wtm; } - return wtm; -}; -export const pack = (payloads: Payload[], cloneBuffers: boolean): Transferable[] => { - const transferables: Transferable[] = []; - for (const payload of payloads) { - const handler = PayloadRegister.handler.get(payload.$type); - handler?.pack(payload, transferables, cloneBuffers); + static pack(payloads: Payload[], cloneBuffers: boolean): Transferable[] { + const transferables: Transferable[] = []; + for (const payload of payloads) { + const handler = PayloadRegister.handler.get(payload.$type); + handler?.pack(payload, transferables, cloneBuffers); + } + return transferables; } - return transferables; -}; -export const unpack = (rawMessage: WorkerTaskMessageType, cloneBuffers: boolean) => { - const instance = new WorkerTaskMessage(rawMessage); - instance.cmd = rawMessage.cmd; + static unpack(rawMessage: WorkerTaskMessageType, cloneBuffers: boolean) { + const instance = new WorkerTaskMessage(rawMessage); + instance.cmd = rawMessage.cmd; - for (const payload of rawMessage.payloads) { - const handler = PayloadRegister.handler.get(payload.$type); - instance.addPayload(handler?.unpack(payload, cloneBuffers)); + for (const payload of rawMessage.payloads) { + const handler = PayloadRegister.handler.get(payload.$type); + instance.addPayload(handler?.unpack(payload, cloneBuffers)); + } + return instance; } - return instance; -}; + +} diff --git a/packages/wtd-core/src/WorkerTaskWorker.ts b/packages/wtd-core/src/WorkerTaskWorker.ts index 76364e3..31f4784 100644 --- a/packages/wtd-core/src/WorkerTaskWorker.ts +++ b/packages/wtd-core/src/WorkerTaskWorker.ts @@ -1,6 +1,6 @@ import { Payload } from './Payload.js'; import { RawPayload } from './RawPayload.js'; -import { WorkerTaskCommandRequest, WorkerTaskCommandResponse, WorkerTaskMessageType } from './WorkerTaskMessage.js'; +import { WorkerTaskMessageType } from './WorkerTaskMessage.js'; export type WorkerTaskWorker = { @@ -44,45 +44,14 @@ export class InterComPortHandler { } } -export const printDefaultMessage = (funcName: string, message: WorkerTaskMessageType) => { - console.log(`WorkerTaskDefaultWorker#${funcName}: name: ${message.name} id: ${message.id} workerId: ${message.workerId}`); -}; - export const comRouting = (workerImpl: WorkerTaskWorker | InterComWorker, message: MessageEvent) => { const wtmt = (message as MessageEvent).data as WorkerTaskMessageType; if (wtmt) { - const wtw = workerImpl as WorkerTaskWorker; - const icw = workerImpl as InterComWorker; - switch (wtmt.cmd) { - case WorkerTaskCommandRequest.INIT: - wtw.init?.(wtmt); - break; - case WorkerTaskCommandRequest.INTERMEDIATE: - wtw.intermediate?.(wtmt); - break; - case WorkerTaskCommandRequest.EXECUTE: - wtw.execute?.(wtmt); - break; - case WorkerTaskCommandRequest.INTERCOM_INIT: - icw.interComInit?.(wtmt); - break; - case WorkerTaskCommandResponse.INTERCOM_INIT_COMPLETE: - icw.interComInitComplete?.(wtmt); - break; - case WorkerTaskCommandRequest.INTERCOM_INTERMEDIATE: - icw.interComIntermediate?.(wtmt); - break; - case WorkerTaskCommandResponse.INTERCOM_INTERMEDIATE_CONFIRM: - icw.interComIntermediateConfirm?.(wtmt); - break; - case WorkerTaskCommandRequest.INTERCOM_EXECUTE: - icw.interComExecute?.(wtmt); - break; - case WorkerTaskCommandResponse.INTERCOM_EXECUTE_COMPLETE: - icw.interComExecuteComplete?.(wtmt); - break; - default: - break; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const obj = (workerImpl as any); + const funcName = wtmt.cmd; + if (typeof obj[funcName] === 'function') { + obj[funcName](wtmt); } } }; diff --git a/packages/wtd-core/src/index.ts b/packages/wtd-core/src/index.ts index 80f55f2..02bc3b5 100644 --- a/packages/wtd-core/src/index.ts +++ b/packages/wtd-core/src/index.ts @@ -5,7 +5,6 @@ import type { import { InterComPortHandler, - printDefaultMessage, comRouting } from './WorkerTaskWorker.js'; @@ -30,9 +29,7 @@ import type { } from './Payload.js'; import { - PayloadRegister, - applyProperties, - fillTransferables + PayloadRegister } from './Payload.js'; import type { @@ -60,31 +57,31 @@ import type { import { WorkerTaskMessage, - createFromExisting, - pack, - unpack, WorkerTaskCommandRequest, WorkerTaskCommandResponse } from './WorkerTaskMessage.js'; +import { + applyProperties, + createWorkerBlob, + fillTransferables +} from './utiilies.js'; + export { WorkerExecutionPlan, WorkerInitPlan, WorkerRegistration, WorkerTask, + createWorkerBlob, WorkerTaskDirector, WorkerTaskWorker, InterComWorker, InterComPortHandler, - printDefaultMessage, comRouting, WorkerTaskMessageType, WorkerTaskCommandRequest, WorkerTaskCommandResponse, WorkerTaskMessage, - createFromExisting, - pack, - unpack, AssociatedArrayType, Payload, ParameterizedMessage, diff --git a/packages/wtd-core/src/utiilies.ts b/packages/wtd-core/src/utiilies.ts new file mode 100644 index 0000000..f5be4d4 --- /dev/null +++ b/packages/wtd-core/src/utiilies.ts @@ -0,0 +1,44 @@ +import { AssociatedArrayType } from './Payload.js'; + +export const fillTransferables = (buffers: IterableIterator, transferables: Transferable[], cloneBuffers: boolean) => { + for (const buffer of buffers) { + const potentialClone = cloneBuffers ? buffer.slice(0) : buffer; + + const outputBuffer = (potentialClone as Uint8Array).buffer; + if (outputBuffer) { + transferables.push(outputBuffer); + } + else { + transferables.push(potentialClone); + } + } +}; + +/** + * Applies values from parameter object via set functions or via direct assignment. + * + * @param {object} objToAlter The objToAlter instance + * @param {AssociatedArrayType} params The parameter object + * @param {boolean} forceCreation Force the creation of a property + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const applyProperties = (objToAlter: any, params?: AssociatedArrayType, forceCreation?: boolean) => { + if (!params) return; + + for (const [k, v] of Object.entries(params)) { + const funcName = 'set' + k.substring(0, 1).toLocaleUpperCase() + k.substring(1); + + if (Object.prototype.hasOwnProperty.call(objToAlter, funcName) && typeof objToAlter[funcName] === 'function') { + objToAlter[funcName] = v; + } + else if (Object.prototype.hasOwnProperty.call(objToAlter, k) || forceCreation) { + objToAlter[k] = v; + } + } +}; + +export const createWorkerBlob = (code: string[]) => { + const simpleWorkerBlob = new Blob(code, { type: 'application/javascript' }); + return window.URL.createObjectURL(simpleWorkerBlob); +}; + diff --git a/packages/wtd-core/test/DataPayload.test.ts b/packages/wtd-core/test/DataPayload.test.ts index b45f5ef..3798178 100644 --- a/packages/wtd-core/test/DataPayload.test.ts +++ b/packages/wtd-core/test/DataPayload.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest'; -import { applyProperties } from '../src/Payload.js'; +import { applyProperties } from 'wtd-core'; describe('DataPayload applyProperties Tests', () => { test('verify empty', () => { diff --git a/packages/wtd-three-ext/package.json b/packages/wtd-three-ext/package.json index 958d7b7..6c972ee 100644 --- a/packages/wtd-three-ext/package.json +++ b/packages/wtd-three-ext/package.json @@ -1,6 +1,6 @@ { "name": "wtd-three-ext", - "version": "2.4.0-next.3", + "version": "2.4.0-next.4", "license": "MIT", "type": "module", "main": "./dist/index.js", @@ -45,7 +45,7 @@ "npm": "9.8.1" }, "dependencies": { - "wtd-core": "~2.4.0-next.3", + "wtd-core": "~2.4.0-next.4", "three": "~0.158.0" }, "devDependencies": {