Skip to content

Commit

Permalink
Intergrate MainEventProxy and WorkerEventProxy for OffscreenCanvas
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Nov 14, 2023
1 parent cea9c8c commit 4f745d2
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 37 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"dependencies": {
"lil-gui": "~0.19.0",
"three": "~0.158.0",
"wtd-core": "~3.0.0-next.0",
"wtd-three-ext": "~3.0.0-next.0",
"wtd-core": "~3.0.0-next.2",
"wtd-three-ext": "~3.0.0-next.2",
"wwobjloader2": "6.2.0-next.4"
},
"devDependencies": {
Expand Down
31 changes: 21 additions & 10 deletions packages/examples/src/com/WorkerCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
WorkerTaskMessageConfig
} from 'wtd-core';
import { recalcAspectRatio, updateText } from '../worker/ComWorkerCommon.js';
import { OffscreenPayload } from 'wtd-core';

/**
* Hello World example using a classic worker
Expand Down Expand Up @@ -53,27 +54,37 @@ class HelloWorldStandardWorkerExample {

try {
const offscreenCom1 = this.canvasCom1.transferControlToOffscreen();
const offscreenPayload1 = new OffscreenPayload({
event: {
type: 'init',
drawingSurface: offscreenCom1,
width: this.canvasCom1.clientWidth,
height: this.canvasCom1.clientHeight
}
});
const payload1 = new RawPayload({
port: channel.port1,
drawingSurface: offscreenCom1,
width: this.canvasCom1.clientWidth,
height: this.canvasCom1.clientHeight
port: channel.port1
});

const offscreenCom2 = this.canvasCom2.transferControlToOffscreen();
const offscreenPayload2 = new OffscreenPayload({
event: {
type: 'init',
drawingSurface: offscreenCom2,
width: this.canvasCom2.clientWidth,
height: this.canvasCom2.clientHeight
}
});
const payload2 = new RawPayload({
port: channel.port2,
drawingSurface: offscreenCom2,
width: this.canvasCom2.clientWidth,
height: this.canvasCom2.clientHeight
port: channel.port2
});
const promisesinit = [];
promisesinit.push(this.workerTaskCom1.initWorker({
message: WorkerTaskMessage.fromPayload(payload1),
message: WorkerTaskMessage.fromPayload([offscreenPayload1, payload1]),
transferables: [channel.port1, offscreenCom1]
}));
promisesinit.push(this.workerTaskCom2.initWorker({
message: WorkerTaskMessage.fromPayload(payload2),
message: WorkerTaskMessage.fromPayload([offscreenPayload2, payload2]),
transferables: [channel.port2, offscreenCom2],
}));
await Promise.all(promisesinit);
Expand Down
11 changes: 7 additions & 4 deletions packages/examples/src/worker/Com1Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import {
WorkerTaskWorker
} from 'wtd-core';
import { getOffScreenCanvas, updateText } from './ComWorkerCommon.js';
import { OffscreenPayload } from 'wtd-core';

export class Com1Worker implements WorkerTaskWorker, InterComWorker {

private icph = new InterComPortHandler();
private offScreenCanvas?: HTMLCanvasElement;

init(message: WorkerTaskMessageConfig): void {
// register the default com-routing function for inter-worker communication
const payload = message.payloads?.[0];
this.icph.registerPort('com2', payload, message => comRouting(this, message));
this.offScreenCanvas = getOffScreenCanvas(payload);
const payloadOffscreen = message.payloads?.[0] as OffscreenPayload;
this.offScreenCanvas = getOffScreenCanvas(payloadOffscreen);
updateText({
text: 'Worker 1: init',
width: this.offScreenCanvas?.width ?? 0,
Expand All @@ -29,6 +28,10 @@ export class Com1Worker implements WorkerTaskWorker, InterComWorker {
log: true
});

// register the default com-routing function for inter-worker communication
const payloadPort = message.payloads?.[1];
this.icph.registerPort('com2', payloadPort, message => comRouting(this, message));

// send initComplete to main
const initComplete = WorkerTaskMessage.createFromExisting({} as WorkerTaskMessageConfig, WorkerTaskCommandResponse.INIT_COMPLETE);
const payloadResponse = new RawPayload({ hello: 'Worker 1 initComplete!' });
Expand Down
11 changes: 7 additions & 4 deletions packages/examples/src/worker/Com2Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import {
WorkerTaskWorker
} from 'wtd-core';
import { getOffScreenCanvas, updateText } from './ComWorkerCommon.js';
import { OffscreenPayload } from 'wtd-core';

export class Com2Worker implements WorkerTaskWorker, InterComWorker {

private icph = new InterComPortHandler();
private offScreenCanvas?: HTMLCanvasElement;

init(message: WorkerTaskMessageConfig): void {
// register the default com-routing function for inter-worker communication
const payload = message.payloads?.[0];
this.icph.registerPort('com1', payload, message => comRouting(this, message));
this.offScreenCanvas = getOffScreenCanvas(payload);
const payloadOffscreen = message.payloads?.[0] as OffscreenPayload;
this.offScreenCanvas = getOffScreenCanvas(payloadOffscreen);
updateText({
text: 'Worker 2: init',
width: this.offScreenCanvas?.width ?? 0,
Expand All @@ -29,6 +28,10 @@ export class Com2Worker implements WorkerTaskWorker, InterComWorker {
log: true
});

// register the default com-routing function for inter-worker communication
const payloadPort = message.payloads?.[1];
this.icph.registerPort('com1', payloadPort, message => comRouting(this, message));

// send initComplete to main
const initComplete = WorkerTaskMessage.createFromExisting({} as WorkerTaskMessageConfig, WorkerTaskCommandResponse.INIT_COMPLETE);
const payloadResponse = new RawPayload({ hello: 'Worker 2 initComplete!' });
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/src/worker/ComWorkerCommon.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Payload, RawPayload } from 'wtd-core';
import { OffscreenPayload } from 'wtd-core';

export const getOffScreenCanvas = (payload?: Payload) => {
return payload ? (payload as RawPayload).message.raw.drawingSurface as HTMLCanvasElement : undefined;
export const getOffScreenCanvas = (offScreenPayload?: OffscreenPayload) => {
return offScreenPayload ? offScreenPayload.message.drawingSurface as HTMLCanvasElement : undefined;
};

export const updateText = (params: {
Expand Down
2 changes: 1 addition & 1 deletion packages/wtd-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wtd-core",
"version": "3.0.0-next.0",
"version": "3.0.0-next.2",
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/wtd-core/src/WorkerTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ export class WorkerTask {
*/
sentMessage(message: WorkerTaskMessage, transferables?: Transferable[]) {
if (this.isWorkerExecuting() && this.worker) {
message.cmd = WorkerTaskCommandRequest.INTERMEDIATE;
message.workerId = this.workerId;
this.worker.postMessage(message, transferables!);
} else {
throw new Error('You can only sent message if Worker executing.');
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/wtd-core/src/WorkerTaskMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ export class WorkerTaskMessage {
return instance;
}

static fromPayload(payload: Payload) {
static fromPayload(payloads: Payload | Payload[], cmd?: WorkerTaskCommands) {
const wtm = new WorkerTaskMessage({});
wtm.addPayload(payload);
if (cmd) {
wtm.setCommand(cmd);
}
wtm.addPayload(payloads);
return wtm;
}
}
14 changes: 12 additions & 2 deletions packages/wtd-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ import {
fillTransferables,
extractDelegate
} from './utiilies.js';

import type {
OffscreenPayloadAdditions,
OffscreenPayloadMessage
} from './offscreen/OffscreenPayload.js';
import {
OffscreenPayload
} from './offscreen/OffscreenPayload.js';
export * from './offscreen/MainEventProxy.js';
export {
WorkerExecutionPlan,
WorkerInitPlan,
Expand Down Expand Up @@ -98,5 +105,8 @@ export {
DataPayloadHandler,
applyProperties,
fillTransferables,
extractDelegate
extractDelegate,
OffscreenPayloadAdditions,
OffscreenPayloadMessage,
OffscreenPayload
};
Loading

0 comments on commit 4f745d2

Please sign in to comment.