mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
fix: basic web worker message passing (#3893)
Removes OP_HOST_GET_WORKER_LOADED, OP_HOST_POLL_WORKER, OP_HOST_RESUME_WORKER and ready/messageBuffer in cli/js/workers.ts.
This commit is contained in:
parent
700f5e45ef
commit
ed680552a2
7 changed files with 56 additions and 167 deletions
|
@ -42,11 +42,8 @@ export let OP_QUERY_PERMISSION: number;
|
|||
export let OP_REVOKE_PERMISSION: number;
|
||||
export let OP_REQUEST_PERMISSION: number;
|
||||
export let OP_CREATE_WORKER: number;
|
||||
export let OP_HOST_GET_WORKER_LOADED: number;
|
||||
export let OP_HOST_POST_MESSAGE: number;
|
||||
export let OP_HOST_POLL_WORKER: number;
|
||||
export let OP_HOST_CLOSE_WORKER: number;
|
||||
export let OP_HOST_RESUME_WORKER: number;
|
||||
export let OP_HOST_GET_MESSAGE: number;
|
||||
export let OP_WORKER_POST_MESSAGE: number;
|
||||
export let OP_WORKER_GET_MESSAGE: number;
|
||||
|
|
|
@ -29,7 +29,7 @@ function createWorker(
|
|||
hasSourceCode: boolean,
|
||||
sourceCode: Uint8Array,
|
||||
name?: string
|
||||
): { id: number; loaded: boolean } {
|
||||
): { id: number } {
|
||||
return sendSync(dispatch.OP_CREATE_WORKER, {
|
||||
specifier,
|
||||
hasSourceCode,
|
||||
|
@ -38,22 +38,6 @@ function createWorker(
|
|||
});
|
||||
}
|
||||
|
||||
async function hostGetWorkerLoaded(id: number): Promise<any> {
|
||||
return await sendAsync(dispatch.OP_HOST_GET_WORKER_LOADED, { id });
|
||||
}
|
||||
|
||||
async function hostPollWorker(id: number): Promise<any> {
|
||||
return await sendAsync(dispatch.OP_HOST_POLL_WORKER, { id });
|
||||
}
|
||||
|
||||
function hostCloseWorker(id: number): void {
|
||||
sendSync(dispatch.OP_HOST_CLOSE_WORKER, { id });
|
||||
}
|
||||
|
||||
function hostResumeWorker(id: number): void {
|
||||
sendSync(dispatch.OP_HOST_RESUME_WORKER, { id });
|
||||
}
|
||||
|
||||
function hostPostMessage(id: number, data: any): void {
|
||||
const dataIntArray = encodeMessage(data);
|
||||
sendSync(dispatch.OP_HOST_POST_MESSAGE, { id }, dataIntArray);
|
||||
|
@ -85,8 +69,6 @@ export interface WorkerOptions {
|
|||
export class WorkerImpl extends EventTarget implements Worker {
|
||||
private readonly id: number;
|
||||
private isClosing = false;
|
||||
private messageBuffer: any[] = [];
|
||||
private ready = false;
|
||||
public onerror?: (e: any) => void;
|
||||
public onmessage?: (data: any) => void;
|
||||
public onmessageerror?: () => void;
|
||||
|
@ -125,14 +107,13 @@ export class WorkerImpl extends EventTarget implements Worker {
|
|||
}
|
||||
*/
|
||||
|
||||
const { id, loaded } = createWorker(
|
||||
const { id } = createWorker(
|
||||
specifier,
|
||||
hasSourceCode,
|
||||
sourceCode,
|
||||
options?.name
|
||||
);
|
||||
this.id = id;
|
||||
this.ready = loaded;
|
||||
this.poll();
|
||||
}
|
||||
|
||||
|
@ -158,27 +139,19 @@ export class WorkerImpl extends EventTarget implements Worker {
|
|||
}
|
||||
|
||||
async poll(): Promise<void> {
|
||||
// If worker has not been immediately executed
|
||||
// then let's await it's readiness
|
||||
if (!this.ready) {
|
||||
const result = await hostGetWorkerLoaded(this.id);
|
||||
|
||||
if (result.error) {
|
||||
if (!this.handleError(result.error)) {
|
||||
throw new Error(result.error.message);
|
||||
}
|
||||
return;
|
||||
while (!this.isClosing) {
|
||||
const data = await hostGetMessage(this.id);
|
||||
if (data == null) {
|
||||
log("worker got null message. quitting.");
|
||||
break;
|
||||
}
|
||||
if (this.onmessage) {
|
||||
const event = { data };
|
||||
this.onmessage(event);
|
||||
}
|
||||
}
|
||||
|
||||
// drain messages
|
||||
for (const data of this.messageBuffer) {
|
||||
hostPostMessage(this.id, data);
|
||||
}
|
||||
this.messageBuffer = [];
|
||||
this.ready = true;
|
||||
this.run();
|
||||
|
||||
/*
|
||||
while (true) {
|
||||
const result = await hostPollWorker(this.id);
|
||||
|
||||
|
@ -194,32 +167,14 @@ export class WorkerImpl extends EventTarget implements Worker {
|
|||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
postMessage(data: any): void {
|
||||
if (!this.ready) {
|
||||
this.messageBuffer.push(data);
|
||||
return;
|
||||
}
|
||||
|
||||
hostPostMessage(this.id, data);
|
||||
}
|
||||
|
||||
terminate(): void {
|
||||
throw new Error("Not yet implemented");
|
||||
}
|
||||
|
||||
private async run(): Promise<void> {
|
||||
while (!this.isClosing) {
|
||||
const data = await hostGetMessage(this.id);
|
||||
if (data == null) {
|
||||
log("worker got null message. quitting.");
|
||||
break;
|
||||
}
|
||||
if (this.onmessage) {
|
||||
const event = { data };
|
||||
this.onmessage(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue