-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
299 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Deno", | ||
"type": "pwa-node", | ||
"request": "launch", | ||
"cwd": "${workspaceFolder}", | ||
"runtimeExecutable": "deno", | ||
"runtimeArgs": ["run", "--inspect-brk", "-A", "${file}"], | ||
"attachSimplePort": 9229 | ||
} | ||
] | ||
} | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Deno", | ||
"type": "pwa-node", | ||
"request": "launch", | ||
"cwd": "${workspaceFolder}", | ||
"runtimeExecutable": "deno", | ||
"runtimeArgs": ["run", "--inspect-brk", "-A", "${file}"], | ||
"attachSimplePort": 9229 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// .vscode/settings.json | ||
{ | ||
"deno.enable": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno", | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno", | ||
}, | ||
} | ||
"deno.enable": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,103 @@ | ||
const importMeta = { | ||
url: "file:///Users/duartasnel/Local/personal_projects/runLater/Thread.ts", | ||
main: import.meta.main | ||
url: "file:///Users/duartasnel/Local/personal_projects/runLater/Thread.ts", | ||
main: import.meta.main, | ||
}; | ||
class Thread { | ||
importsMod = []; | ||
stopped = false; | ||
constructor(operation, type, imports){ | ||
imports?.forEach((v)=>{ | ||
if (v.endsWith(".ts'") || v.endsWith('.ts"')) { | ||
throw new Error("Threaded imports do no support typescript files"); | ||
} | ||
}); | ||
this.imports = imports || []; | ||
this.filePath = this.createFile(); | ||
this.createImportsFolder(); | ||
this.populateFile(operation); | ||
this.workerURL = new URL(this.filePath, importMeta.url); | ||
this.worker = new Worker(this.workerURL.href.startsWith("http") ? "file:" + this.workerURL.pathname : this.workerURL.href, { | ||
type | ||
}); | ||
importsMod = []; | ||
stopped = false; | ||
constructor(operation, type, imports) { | ||
imports?.forEach((v) => { | ||
if (v.endsWith(".ts'") || v.endsWith('.ts"')) { | ||
throw new Error("Threaded imports do no support typescript files"); | ||
} | ||
}); | ||
this.imports = imports || []; | ||
this.filePath = this.createFile(); | ||
this.createImportsFolder(); | ||
this.populateFile(operation); | ||
this.workerURL = new URL(this.filePath, importMeta.url); | ||
this.worker = new Worker( | ||
this.workerURL.href.startsWith("http") | ||
? "file:" + this.workerURL.pathname | ||
: this.workerURL.href, | ||
{ | ||
type, | ||
}, | ||
); | ||
} | ||
createFile() { | ||
return Deno.makeTempFileSync({ | ||
prefix: "deno_thread_", | ||
suffix: ".js", | ||
}); | ||
} | ||
createImportsFolder() { | ||
Deno.mkdirSync(this.getTempFolder() + "threaded_imports", { | ||
recursive: true, | ||
}); | ||
} | ||
populateFile(code) { | ||
this.imports?.forEach((val) => this.copyDep(val)); | ||
Deno.writeTextFileSync( | ||
this.filePath, | ||
`\n${ | ||
this.importsMod.join("\n") | ||
}\nvar userCode = ${code.toString()}\n\nonmessage = function(e) {\n postMessage(userCode(e));\n}\n\n`, | ||
); | ||
} | ||
copyDep(str) { | ||
var importPathRegex = /('|"|`)(.+\.js)(\1)/ig; | ||
var importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; | ||
var matchedPath = importPathRegex.exec(str) || ""; | ||
var file = false; | ||
var fqfn = ""; | ||
if ( | ||
!matchedPath[0].includes("http://") && | ||
!matchedPath[0].includes("https://") | ||
) { | ||
file = true; | ||
fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); | ||
} | ||
createFile() { | ||
return Deno.makeTempFileSync({ | ||
prefix: "deno_thread_", | ||
suffix: ".js" | ||
}); | ||
var matchedIns = importInsRegex.exec(str) || ""; | ||
if (!matchedIns) { | ||
throw new Error( | ||
"The import instruction seems to be unreadable try formatting it, for example: \n" + | ||
"import { something } from './somet.js' \n ", | ||
); | ||
} | ||
createImportsFolder() { | ||
Deno.mkdirSync(this.getTempFolder() + "threaded_imports", { | ||
recursive: true | ||
}); | ||
if (file) { | ||
this.importsMod.push(`${matchedIns[0]} "${Deno.realPathSync(fqfn)}"`); | ||
} else { | ||
this.importsMod.push(`${matchedIns[0]} ${matchedPath[0]}`); | ||
} | ||
populateFile(code) { | ||
this.imports?.forEach((val)=>this.copyDep(val) | ||
); | ||
Deno.writeTextFileSync(this.filePath, `\n${this.importsMod.join("\n")}\nvar userCode = ${code.toString()}\n\nonmessage = function(e) {\n postMessage(userCode(e));\n}\n\n`); | ||
} | ||
copyDep(str) { | ||
var importPathRegex = /('|"|`)(.+\.js)(\1)/ig; | ||
var importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; | ||
var matchedPath = importPathRegex.exec(str) || ""; | ||
var file = false; | ||
var fqfn = ""; | ||
if (!matchedPath[0].includes("http://") && !matchedPath[0].includes("https://")) { | ||
file = true; | ||
fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); | ||
} | ||
var matchedIns = importInsRegex.exec(str) || ""; | ||
if (!matchedIns) throw new Error("The import instruction seems to be unreadable try formatting it, for example: \n" + "import { something } from './somet.js' \n "); | ||
if (file) { | ||
this.importsMod.push(`${matchedIns[0]} "${Deno.realPathSync(fqfn)}"`); | ||
} else { | ||
this.importsMod.push(`${matchedIns[0]} ${matchedPath[0]}`); | ||
} | ||
} | ||
getTempFolder() { | ||
let t = this.filePath; | ||
return t.replace(/(\/\w+.js)/ig, "/"); | ||
} | ||
postMessage(msg) { | ||
this.worker.postMessage(msg); | ||
return this; | ||
} | ||
stop() { | ||
this.stopped = true; | ||
this.worker.terminate(); | ||
} | ||
remove() { | ||
if (this.stopped == false) this.stop(); | ||
try { | ||
return Deno.remove(this.filePath, { | ||
recursive: true | ||
}); | ||
} catch (err) { | ||
console.error(`Failed to remove worker file: ${this.filePath}`); | ||
console.error(err); | ||
return Promise.reject(`Failed to remove worker file: ${this.filePath}`); | ||
} | ||
} | ||
onMessage(callback) { | ||
this.worker.onmessage = (e)=>callback(e.data) | ||
; | ||
return this; | ||
} | ||
getTempFolder() { | ||
let t = this.filePath; | ||
return t.replace(/(\/\w+.js)/ig, "/"); | ||
} | ||
postMessage(msg) { | ||
this.worker.postMessage(msg); | ||
return this; | ||
} | ||
stop() { | ||
this.stopped = true; | ||
this.worker.terminate(); | ||
} | ||
remove() { | ||
if (this.stopped == false) this.stop(); | ||
try { | ||
return Deno.remove(this.filePath, { | ||
recursive: true, | ||
}); | ||
} catch (err) { | ||
console.error(`Failed to remove worker file: ${this.filePath}`); | ||
console.error(err); | ||
return Promise.reject(`Failed to remove worker file: ${this.filePath}`); | ||
} | ||
} | ||
onMessage(callback) { | ||
this.worker.onmessage = (e) => callback(e.data); | ||
return this; | ||
} | ||
} | ||
export { Thread as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import Thread from "../Thread.ts" | ||
import Thread from "../Thread.ts"; | ||
|
||
let count = 13; | ||
|
||
function postMessage(e: any){} | ||
function postMessage(e: any) {} | ||
|
||
function tester(){ | ||
function tester() { | ||
let i = 0; | ||
setInterval(()=>{ | ||
postMessage(0) | ||
}, 500) | ||
setInterval(() => { | ||
postMessage(0); | ||
}, 500); | ||
|
||
return 0; | ||
} | ||
|
||
for(let i = 0; i < count; i++){ | ||
new Thread(tester, "module").onMessage((d)=>console.log(`thread -> ${i}`)).postMessage(0); | ||
for (let i = 0; i < count; i++) { | ||
new Thread(tester, "module").onMessage((d) => console.log(`thread -> ${i}`)) | ||
.postMessage(0); | ||
} |
Oops, something went wrong.