diff --git a/example/main.js b/example/main.js index a32e7be..33e1fa4 100644 --- a/example/main.js +++ b/example/main.js @@ -6,7 +6,7 @@ const ExampleWorker = new Workio(() => { return "hello, " + name }; - return { echo } + return { echo, close } }, { as: "worker" }) @@ -14,6 +14,7 @@ console.log(ExampleWorker) const instance = new ExampleWorker(); -setTimeout(async () => { +(async () => { console.log(await instance.echo("wow")) -}, 1000) \ No newline at end of file + await instance.close() +})() diff --git a/src/TaskPool.js b/src/TaskPool.js index a92878b..434128f 100644 --- a/src/TaskPool.js +++ b/src/TaskPool.js @@ -4,7 +4,7 @@ export class TaskPool { this.nextId = 0; this.vacantId = []; } - newTask({ resolve }) { + newTask({ resolve, reject }) { let currentId = null if(this.vacantId.length) { currentId = this.vacantId[0] @@ -13,12 +13,19 @@ export class TaskPool { currentId = this.nextId this.nextId++; } - this.pool[currentId] = resolve + this.pool[currentId] = { resolve, reject } return { id: currentId } } setResponse({ taskId, returnValue }) { - this.pool[taskId](returnValue) + this.pool[taskId].resolve(returnValue) + this.taskGC({ taskId }) + } + rejectResponse({ taskId }) { + this.pool[taskId].reject("a") + this.taskGC({ taskId }) + } + taskGC({ taskId }) { this.pool[taskId] = undefined; if(taskId + 1 === this.nextId) { this.nextId--; diff --git a/src/WorkioInstance.js b/src/WorkioInstance.js index b1325a2..af8148f 100644 --- a/src/WorkioInstance.js +++ b/src/WorkioInstance.js @@ -34,12 +34,14 @@ export class WorkioInstance { delete publicFunctionInterface[index] } } - - self.postMessage({ prepareMethod: Object.keys(publicFunctionInterface), sudo }) self.addEventListener("message", async ({ data }) => { if("task" in data) { - self.postMessage({ returnValue: await publicFunctionInterface[data.task](...data.args), taskId: data.taskId, sudo }) + if(data.task in publicFunctionInterface) { + self.postMessage({ returnValue: await publicFunctionInterface[data.task](...data.args), taskId: data.taskId, sudo }) + } else { + self.postMessage({ methodNotFound: true, taskId: data.taskId, sudo }) + } } }, { passive: true }); @@ -47,46 +49,52 @@ export class WorkioInstance { `), { type: "module" }); const personalTaskPool = new TaskPool(); - const proxiedMethodInterface = {}; let sudo = null; - let hasPMI = false; workerInstance.postMessage({ constructorArgs }); workerInstance.addEventListener("message", ({ data }) => { if(data.sudo) { - if(sudo === null) { - sudo = data.sudo; - } - if(sudo === data.sudo) { - if(data.close) { - workerInstance.terminate(); - return; - } - if(data.prepareMethod) { - data.prepareMethod.forEach(index => { - this[index] = async function() { - return new Promise((resolve, reject) => { - const task = personalTaskPool.newTask({ resolve }); - workerInstance.postMessage({ task: index, args: [...arguments], taskId: task.id }) - }) - } - }) - } - if(data.returnValue) { - personalTaskPool.setResponse(data) // { taskId, returnValue } - } + switch(sudo) { + case null: + sudo = data.sudo + break; + case data.sudo: + if(data.close) { + workerInstance.terminate(); + return; + } + // if(data.prepareMethod) { + // data.prepareMethod.forEach(index => { + // this[index] = async function() { + // return new Promise((resolve, reject) => { + // const task = personalTaskPool.newTask({ resolve }); + // workerInstance.postMessage({ task: index, args: [...arguments], taskId: task.id }) + // }) + // } + // }) + // } + if(data.returnValue) { + personalTaskPool.setResponse(data) // { taskId, returnValue } + } + if(data.methodNotFound) { + personalTaskPool.rejectResponse(data) + } + break; } } }, { passive: true }); - // return new Proxy(this, { - // get(target, prop, receiver) { - // if(prop in proxiedMethodInterface) { - // return proxiedMethodInterface[prop] - // } - // } - // }) + return new Proxy(this, { + get(target, prop, receiver) { + return function() { + return new Promise((resolve, reject) => { + const task = personalTaskPool.newTask({ resolve, reject }); + workerInstance.postMessage({ task: prop, args: [...arguments], taskId: task.id }) + }) + } + } + }) } } \ No newline at end of file