Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ihasq committed Jan 16, 2024
1 parent f114b80 commit 00380de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
7 changes: 4 additions & 3 deletions example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const ExampleWorker = new Workio(() => {
return "hello, " + name
};

return { echo }
return { echo, close }

}, { as: "worker" })

console.log(ExampleWorker)

const instance = new ExampleWorker();

setTimeout(async () => {
(async () => {
console.log(await instance.echo("wow"))
}, 1000)
await instance.close()
})()
13 changes: 10 additions & 3 deletions src/TaskPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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--;
Expand Down
74 changes: 41 additions & 33 deletions src/WorkioInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,67 @@ 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 });
})()
`), { 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 })
})
}
}
})
}
}

0 comments on commit 00380de

Please sign in to comment.