Skip to content

Commit

Permalink
Unclutter API, allow sending intermediate messages to Worker, fix asy…
Browse files Browse the repository at this point in the history
…nc issues
  • Loading branch information
kaisalmen committed Oct 8, 2023
1 parent 5977e80 commit fb27fc4
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 2,583 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ workerTaskDirector.registerTask(taskName, {
try {
await workerTaskDirector.initTaskType(taskName)
// once the init Promise returns enqueue the execution
const execMessage = new WorkerTaskMessage({
id: 0,
name: taskName
});
const execMessage = new WorkerTaskMessage();

await workerTaskDirector.enqueueWorkerExecutionPlan({
taskTypeName: execMessage.name,
await workerTaskDirector.enqueueWorkerExecutionPlan(taskName, {
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => { console.log('Received final command: ' + m.cmd); }
Expand Down
2,497 changes: 82 additions & 2,415 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"dev": "npx vite",
"dev:debug": "npx vite --debug --force",
"prerelease": "npm run build && npm run doc",
"test": "vitest"
"test": "vitest",
"reset:repo": "git clean -f -X -d"
},
"keywords": [],
"homepage": "https://github.com/kaisalmen/three-wtm#README",
Expand Down
11 changes: 5 additions & 6 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"type": "module",
"private": "true",
"scripts": {
"clean": "shx rm -fr *.tsbuildinfo dist",
"clean": "shx rm -fr *.tsbuildinfo dist src/worker/generated",
"doc": "echo 'docs are not generated for examples'",
"compile": "tsc -b",
"copy:worker:OBJLoader2Worker": "shx mkdir -p ./src/worker/generated && shx cp -f ../../node_modules/wwobjloader2/dist/worker/OBJLoader2Worker*.js ./src/worker/generated",
"copy:worker:OBJLoader2Worker": "shx mkdir -p ./src/worker/generated && shx cp -f ../../node_modules/wwobjloader2/lib/worker/OBJLoader2Worker*.js ./src/worker/generated",
"build:worker:HelloWorldWorker": "shx rm -f ./src/worker/generated/HelloWorldWorker*.js && vite -c build/vite.config.HelloWorldWorker.ts build",
"build:worker:HelloWorldThreeWorker": "shx rm -f ./src/worker/generated/HelloWorldThreeWorker*.js && vite -c build/vite.config.HelloWorldThreeWorker.ts build",
"build:worker:InfiniteWorkerExternalGeometry": "shx rm -f ./src/worker/generated/InfiniteWorkerExternalGeometry*.js && vite -c build/vite.config.InfiniteWorkerExternalGeometry.ts build",
Expand All @@ -26,10 +26,9 @@
"preview": "http-server ./preview"
},
"dependencies": {
"lil-gui": "~0.17.0",
"wtd-core": "2.2.0",
"wtd-three-ext": "2.2.0",
"wwobjloader2": "5.1.0-next.0"
"lil-gui": "~0.18.2",
"wtd-three-ext": "2.3.0-next.0",
"wwobjloader2": "~6.0.1"
},
"devDependencies": {
"http-server": "~14.1.1"
Expand Down
10 changes: 3 additions & 7 deletions packages/examples/src/helloWorld/HelloWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ class HelloWorldModuleWorkerExample {

const t0 = performance.now();
// once the init Promise returns enqueue the execution
const execMessage = new WorkerTaskMessage({
id: 0,
name: taskName
});
const resultExec = await this.workerTaskDirector.enqueueWorkerExecutionPlan({
taskTypeName: execMessage.name,
const execMessage = new WorkerTaskMessage();
const resultExec = await this.workerTaskDirector.enqueueWorkerExecutionPlan(taskName, {
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => {
Expand All @@ -55,7 +51,7 @@ class HelloWorldModuleWorkerExample {
console.log(msg);
alert(msg);
console.log('Done');
} catch (e: unknown) {
} catch (e) {
console.error(e);
}
}
Expand Down
10 changes: 3 additions & 7 deletions packages/examples/src/helloWorld/HelloWorldStandard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ class HelloWorldStandardWorkerExample {

const t0 = performance.now();
// once the init Promise returns enqueue the execution
const execMessage = new WorkerTaskMessage({
id: 0,
name: taskName
});
const resultExec = await this.workerTaskDirector.enqueueWorkerExecutionPlan({
taskTypeName: execMessage.name,
const execMessage = new WorkerTaskMessage();
const resultExec = await this.workerTaskDirector.enqueueWorkerExecutionPlan(taskName, {
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => {
Expand All @@ -55,7 +51,7 @@ class HelloWorldStandardWorkerExample {
console.log(msg);
alert(msg);
console.log('Done');
} catch (e: unknown) {
} catch (e) {
console.error(e);
}
}
Expand Down
14 changes: 3 additions & 11 deletions packages/examples/src/helloWorld/HelloWorldWorkerTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class HelloWorldWorkerTaskExample {
url: new URL(import.meta.env.DEV ? '../worker/HelloWorldWorker.ts' : '../worker/generated/HelloWorldWorker-es.js', import.meta.url)
}, true);

const initMessage = new WorkerTaskMessage({
cmd: 'init',
id: 0,
name: taskName
});
const initMessage = new WorkerTaskMessage();

try {
// init the worker task without any payload (worker init without function invocation on worker)
Expand All @@ -31,12 +27,8 @@ class HelloWorldWorkerTaskExample {

const t0 = performance.now();
// once the init Promise returns enqueue the execution
const execMessage = new WorkerTaskMessage({
id: 0,
name: taskName
});
const execMessage = new WorkerTaskMessage();
const resultExec = await workerTask.executeWorker({
taskTypeName: execMessage.name,
message: execMessage,
// decouple result evaluation ...
onComplete: (m: WorkerTaskMessageType) => {
Expand All @@ -55,7 +47,7 @@ class HelloWorldWorkerTaskExample {
console.log(msg);
alert(msg);
console.log('Done');
} catch (e: unknown) {
} catch (e) {
console.error(e);
}
}
Expand Down
44 changes: 18 additions & 26 deletions packages/examples/src/infinite/PotentiallyInfiniteExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ type TaskDescription = {
modelName?: string;
filenameObj?: URL;
filenameMtl?: URL;
buffer?: string | ArrayBuffer;
materialStore?: MaterialStore;
};

/**
Expand Down Expand Up @@ -125,10 +123,10 @@ class PotentiallyInfiniteExample {
workerUrl: new URL(import.meta.env.DEV ? '../worker/generated/OBJLoader2WorkerClassic.js' : '../worker/generated/OBJLoader2WorkerModule.js', import.meta.url),
workerCount: 2,
filenameMtl: new URL('../../models/obj/female02/female02.mtl', import.meta.url),
filenameObj: new URL('../../models/obj/female02/female02.obj', import.meta.url),
materialStore: new MaterialStore(true)
filenameObj: new URL('../../models/obj/female02/female02.obj', import.meta.url)
} as TaskDescription;

private materialStore = new MaterialStore(true);
private tasksToUse: TaskDescription[] = [];
private executions: Array<Promise<unknown>> = [];
private objectsUsed = new Map<number, { name: string, pos: THREE.Vector3 }>();
Expand Down Expand Up @@ -254,8 +252,8 @@ class PotentiallyInfiniteExample {
this.renderer.render(this.scene, this.camera);
}

run() {
this.initContent();
async run() {
await this.initContent();
}

/**
Expand Down Expand Up @@ -336,38 +334,32 @@ class PotentiallyInfiniteExample {
const loadMtl = new Promise<MTLLoader.MaterialCreator>(resolve => {
const mtlLoader = new MTLLoader();
mtlLoader.load(taskDescr!.filenameMtl!.href, resolve);
}).then((materialCreator: MTLLoader.MaterialCreator) => {
materialCreator.preload();
this.taskObjLoader2Worker.materialStore?.addMaterialsFromObject(materialCreator.materials, false);
});
awaiting.push(loadMtl);

const fileLoader = new THREE.FileLoader();
fileLoader.setResponseType('arraybuffer');
const loadObj = fileLoader.loadAsync(taskDescr!.filenameObj!.href as string)

Check failure on line 342 in packages/examples/src/infinite/PotentiallyInfiniteExample.ts

View workflow job for this annotation

GitHub Actions / wtd

Missing semicolon
.then(async (buffer: string | ArrayBuffer) => {
this.taskObjLoader2Worker.buffer = buffer as ArrayBufferLike;
});
awaiting.push(loadObj);
}

if (awaiting.length > 0) {
const results = await Promise.all(awaiting);
console.log(results);

if (this.taskObjLoader2Worker.use) {
const materialCreator = results[results.length - 2] as MTLLoader.MaterialCreator;
materialCreator.preload();
this.materialStore.addMaterialsFromObject(materialCreator.materials, false);
const buffer = results[results.length - 1] as string | ArrayBuffer;

const initMessage = new WorkerTaskMessage({
id: 0,
name: 'OBJLoader2WorkerModule'
});

const dataPayload = new DataPayload();
dataPayload.buffers.set('modelData', this.taskObjLoader2Worker.buffer as ArrayBufferLike);
if (this.taskObjLoader2Worker.materialStore) {
dataPayload.params = {
materialNames: new Set(Array.from(this.taskObjLoader2Worker.materialStore?.getMaterials().keys()))
};
}
dataPayload.params = {
materialNames: new Set(Array.from(this.materialStore.getMaterials().keys()))
};
dataPayload.buffers.set('modelData', buffer as ArrayBufferLike);
initMessage.addPayload(dataPayload);

const transferables = initMessage.pack(false);
Expand All @@ -381,7 +373,7 @@ class PotentiallyInfiniteExample {
}
}
else {
Promise.reject('No task type has been configured');
Promise.reject('No task type has been configured. Unable to execute!');
}
}

Expand Down Expand Up @@ -479,7 +471,7 @@ class PotentiallyInfiniteExample {
const preparedMesh = dataPayloadOBJ?.params?.preparedMesh;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mesh = OBJLoader2.buildThreeMesh(preparedMesh, taskDescr.materialStore?.getMaterials(), false) as THREE.Mesh;
mesh = OBJLoader2.buildThreeMesh(preparedMesh, this.materialStore.getMaterials(), false) as THREE.Mesh;
} else {
meshPayload = wtm.payloads[0] as MeshPayload;
if (meshPayload.params?.color) {
Expand All @@ -490,7 +482,7 @@ class PotentiallyInfiniteExample {

if (wtm.payloads.length === 2) {
materialsPayload = wtm.payloads[1] as MaterialsPayload;
const storedMaterials = taskDescr.materialStore ? taskDescr.materialStore.getMaterials() : new Map();
const storedMaterials = this.materialStore.getMaterials();
material = materialsPayload.processMaterialTransport(storedMaterials, true);
if (!material) {
material = new THREE.MeshStandardMaterial({ color: 0xFF0000 });
Expand Down Expand Up @@ -748,13 +740,13 @@ class GUIControls {
this.controllers.set('resetExecution', controllerResetExecution);
}

executeLoading() {
async executeLoading() {
this.started = true;
for (const controller of this.controllers.values()) {
this.flipElement(controller, false);
}
this.flipElement(this.controllers.get('stopExecution'), true);
this.app.run();
await this.app.run();
}

stopExecution() {
Expand Down
7 changes: 3 additions & 4 deletions packages/examples/src/threejs/Threejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class WorkerTaskDirectorExample {

awaiting.push(loadObj());
const result = await Promise.all(awaiting);
console.log('Loaded OBJ');
console.log('Awaited all required init and data loading.');

const objLoaderDataPayload = new DataPayload();
objLoaderDataPayload.buffers.set('modelData', result[1] as ArrayBufferLike);
Expand All @@ -174,7 +174,7 @@ class WorkerTaskDirectorExample {
const transferables = objLoaderInitMessage.pack(false);
await this.workerTaskDirector.initTaskType(objLoaderInitMessage.name, objLoaderInitMessage, transferables);
console.timeEnd('Init tasks');
this.executeTasks();
await this.executeTasks();
}

/** Once all tasks are initialized a 1024 tasks are enqueued for execution by WorkerTaskDirector. */
Expand All @@ -194,8 +194,7 @@ class WorkerTaskDirectorExample {
name: `${name}_${globalCount}`
});

const voidPromise = this.workerTaskDirector.enqueueWorkerExecutionPlan({
taskTypeName: name ?? 'unknown',
const voidPromise = this.workerTaskDirector.enqueueWorkerExecutionPlan(name ?? 'unknown', {
message: execMessage,
onComplete: (m: WorkerTaskMessageType) => {
this.processMessage(m);
Expand Down
3 changes: 1 addition & 2 deletions packages/examples/src/transferables/TransferablesTestbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ class TransferablesTestbed {
execMessage.addPayload(dataPayload);
const transferables = execMessage.pack(false);

return this.workerTaskDirector.enqueueWorkerExecutionPlan({
return this.workerTaskDirector.enqueueWorkerExecutionPlan(task.name, {
message: execMessage,
taskTypeName: task.name,
onComplete: (m: WorkerTaskMessageType) => {
this.processMessage(m);
},
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/worker/HelloWorldThreeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SphereGeometry } from 'three';
import {
WorkerTaskDirectorDefaultWorker,
WorkerTaskDirectorWorker,
WorkerTaskDefaultWorker,
WorkerTaskMessage,
WorkerTaskMessageType
} from 'wtd-core';
Expand All @@ -11,7 +10,7 @@ import {

declare const self: DedicatedWorkerGlobalScope;

export class HelloWorlThreedWorker extends WorkerTaskDirectorDefaultWorker implements WorkerTaskDirectorWorker {
export class HelloWorlThreedWorker extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
console.log(`HelloWorldWorker#init: name: ${message.name} id: ${message.id} cmd: ${message.cmd} workerId: ${message.workerId}`);
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/worker/HelloWorldWorker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {
DataPayload,
WorkerTaskDirectorDefaultWorker,
WorkerTaskDirectorWorker,
WorkerTaskDefaultWorker,
WorkerTaskMessage,
WorkerTaskMessageType
} from 'wtd-core';

declare const self: DedicatedWorkerGlobalScope;

export class HelloWorldWorker extends WorkerTaskDirectorDefaultWorker implements WorkerTaskDirectorWorker {
export class HelloWorldWorker extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
const initComplete = WorkerTaskMessage.createFromExisting(message, 'initComplete');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
BufferGeometry
} from 'three';
import {
WorkerTaskDirectorDefaultWorker,
WorkerTaskDirectorWorker,
WorkerTaskDefaultWorker,
WorkerTaskMessage,
WorkerTaskMessageType
} from 'wtd-core';
Expand All @@ -13,7 +12,7 @@ import {

declare const self: DedicatedWorkerGlobalScope;

class InfiniteWorkerExternalGeometry extends WorkerTaskDirectorDefaultWorker implements WorkerTaskDirectorWorker {
class InfiniteWorkerExternalGeometry extends WorkerTaskDefaultWorker {

private localData = {
meshPayloadRaw: undefined as MeshPayloadType | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
MeshPhongMaterial
} from 'three';
import {
WorkerTaskDirectorDefaultWorker,
WorkerTaskDirectorWorker,
WorkerTaskDefaultWorker,
WorkerTaskMessage,
WorkerTaskMessageType
} from 'wtd-core';
Expand All @@ -15,7 +14,7 @@ import {
MaterialsPayload,
} from 'wtd-three-ext';

class InfiniteWorkerInternalGeometry extends WorkerTaskDirectorDefaultWorker implements WorkerTaskDirectorWorker {
class InfiniteWorkerInternalGeometry extends WorkerTaskDefaultWorker {

init(message: WorkerTaskMessageType) {
const initComplete = WorkerTaskMessage.createFromExisting(message, 'initComplete');
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/worker/OBJLoaderWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js';
import {
AssociatedArrayType,
WorkerTaskDirectorDefaultWorker,
WorkerTaskDirectorWorker,
WorkerTaskDefaultWorker,
WorkerTaskMessage,
WorkerTaskMessageType
} from 'wtd-core';
Expand All @@ -18,7 +17,7 @@ import {

declare const self: DedicatedWorkerGlobalScope;

class OBJLoaderWorker extends WorkerTaskDirectorDefaultWorker implements WorkerTaskDirectorWorker {
class OBJLoaderWorker extends WorkerTaskDefaultWorker {

private localData = {
objLoader: undefined as OBJLoader | undefined,
Expand Down
Loading

0 comments on commit fb27fc4

Please sign in to comment.