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 15, 2024
1 parent 59ff322 commit f114b80
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock.json
package-lock.json
node_modules
20 changes: 17 additions & 3 deletions example/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { Workio } from "../src/Workio.js"

const a = new Workio((name) => {
return `Hello, ${name} from Workio!`
}, { as: Function })()
const ExampleWorker = new Workio(() => {

function echo(name) {
return "hello, " + name
};

return { echo }

}, { as: "worker" })

console.log(ExampleWorker)

const instance = new ExampleWorker();

setTimeout(async () => {
console.log(await instance.echo("wow"))
}, 1000)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "The Web Worker Framework",
"main": "/build/workio.cjs",
"type": "module",
"directories": {
"test": "test"
},
Expand All @@ -23,4 +24,4 @@
"url": "https://github.com/workio-js/workio/issues"
},
"homepage": "https://github.com/workio-js/workio#readme"
}
}
30 changes: 26 additions & 4 deletions src/TaskPool.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
export class TaskPool extends Proxy {
export class TaskPool {
constructor() {
super({

})
this.pool = {};
this.nextId = 0;
this.vacantId = [];
}
newTask({ resolve }) {
let currentId = null
if(this.vacantId.length) {
currentId = this.vacantId[0]
this.vacantId.shift()
} else {
currentId = this.nextId
this.nextId++;
}
this.pool[currentId] = resolve

return { id: currentId }
}
setResponse({ taskId, returnValue }) {
this.pool[taskId](returnValue)
this.pool[taskId] = undefined;
if(taskId + 1 === this.nextId) {
this.nextId--;
} else {
this.vacantId.push(taskId);
}
}
}
8 changes: 4 additions & 4 deletions src/Workio.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Workio {
*
* @param { Object } [config]
* @param { String } [config.shared]
* @param { Object } [config.as]
* @param { String } [config.as]
*/

constructor(workerFn, config) {
Expand All @@ -20,7 +20,7 @@ export class Workio {
};
if(config) {
switch(config.as) {
case Worker:
case "worker":
case undefined:
return class extends WorkioInstance {
/**
Expand All @@ -31,10 +31,10 @@ export class Workio {
}
};

case Function:
case "function":
return new WorkioFunction(workerFn, config);

case Object:
case "object":
return new WorkioObject(workerFn, config);
}
}
Expand Down
43 changes: 29 additions & 14 deletions src/WorkioInstance.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { AM_I_NODE } from "./AM_I_NODE";
import { getScriptURL } from "./getScriptURL";
import { AM_I_NODE } from "./AM_I_NODE.js";
import { getScriptURL } from "./getScriptURL.js";
import { TaskPool } from "./TaskPool.js";
// import { Worker } from "node:worker_threads"

export class WorkioInstance {

constructor(workerFn, config, constructorArgs) {

const workerInstance = new Worker(getScriptURL(`
const workerInstance = new Worker(((scriptString) => getScriptURL(scriptString))(`
(async () => {
// init
let sudo = crypto.randomUUID();
self.postMessage({ sudo })
Expand All @@ -35,23 +37,25 @@ export class WorkioInstance {
self.postMessage({ prepareMethod: Object.keys(publicFunctionInterface), sudo })
self.${AM_I_NODE? "on" : "addEventListener"}("message", ({ data }) => {
self.addEventListener("message", async ({ data }) => {
if("task" in data) {
self.postMessage({ returnValue: publicFunctionInterface[data.task](...data.args) })
self.postMessage({ returnValue: await publicFunctionInterface[data.task](...data.args), taskId: data.taskId, sudo })
}
}, { passive: true });
})()
`), { type: "module" });

const taskIdPool = {}; // キーは整数値

workerInstance.postMessage({ constructorArgs });
const personalTaskPool = new TaskPool();
const proxiedMethodInterface = {};

let sudo = null;
let hasPMI = false;

workerInstance.postMessage({ constructorArgs });

workerInstance[AM_I_NODE? "on" : "addEventListener"]("message", ({ data }) => {
if("sudo" in data) {
workerInstance.addEventListener("message", ({ data }) => {
if(data.sudo) {
if(sudo === null) {
sudo = data.sudo;
}
Expand All @@ -60,18 +64,29 @@ export class WorkioInstance {
workerInstance.terminate();
return;
}
if("prepareMethod" in data) {
if(data.prepareMethod) {
data.prepareMethod.forEach(index => {
this[index] = function() {
this[index] = async function() {
return new Promise((resolve, reject) => {
workerInstance.postMessage({ task: index, args: arguments })
const task = personalTaskPool.newTask({ resolve });
workerInstance.postMessage({ task: index, args: [...arguments], taskId: task.id })
})
}
})
}
if(data.returnValue) {
personalTaskPool.setResponse(data) // { taskId, returnValue }
}
}
}
}, { passive: true });

// return new Proxy(this, {
// get(target, prop, receiver) {
// if(prop in proxiedMethodInterface) {
// return proxiedMethodInterface[prop]
// }
// }
// })
}
}
2 changes: 1 addition & 1 deletion src/getScriptURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const scriptURLCache = new Map()

export function getScriptURL(scriptStr) {
if(!scriptURLCache.has(scriptStr)) {
scriptURLCache.set(scriptStr, URL.createObjectURL(new Blob([scriptStr], { type: "application/javascript" })))
scriptURLCache.set(scriptStr, URL.createObjectURL(new File([scriptStr], "workioscript.js", { type: "application/javascript" })))
}
return scriptURLCache.get(scriptStr);
};

0 comments on commit f114b80

Please sign in to comment.