Skip to content

Commit

Permalink
Made Thread silent (cached bg code)
Browse files Browse the repository at this point in the history
  • Loading branch information
duart38 committed Aug 15, 2020
1 parent 3fb7bce commit 62884c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
21 changes: 16 additions & 5 deletions Thread.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,23 @@ System.register("Thread", [], function (exports_1, context_1) {
execute: function () {
Thread = class Thread {
constructor(operation, imports) {
this.folderName = "./tmp_threads";
imports?.forEach((v) => {
if (v.endsWith(".ts'") || v.endsWith('.ts"')) {
throw new Error("Threaded imports do no support typescript files");
}
});
this.imports = imports || [];
this.fileName = this.createFile();
this.populateFile(operation);
this.worker = new Worker(new URL(this.fileName, context_1.meta.url).href, {
type: "module",
});
this.init();
this.cleanUp();
}
createFile() {
Deno.mkdirSync("./tmp_threads", { recursive: true });
return Deno.makeTempFileSync({ prefix: "deno_thread_", suffix: ".js", dir: "./tmp_threads" });
Deno.mkdirSync(this.folderName, { recursive: true });
return Deno.makeTempFileSync({ prefix: "deno_thread_", suffix: ".js", dir: this.folderName });
}
populateFile(code) {
Deno.writeTextFileSync(this.fileName, `
Expand All @@ -130,8 +136,13 @@ onmessage = function(e) {
`);
}
init() {
addEventListener("unload", () => Deno.removeSync(this.fileName));
async cleanUp() {
await Deno.remove(this.fileName);
try {
await Deno.remove(this.folderName);
}
catch (error) {
}
}
postMessage(msg) {
this.worker.postMessage(msg);
Expand Down
31 changes: 23 additions & 8 deletions Thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ export default class Thread<T> {
public fileName: string;
public worker: Worker;
private imports: Array<string>;
private folderName: string = "./tmp_threads";
/**
*
* @param operation The method to be used in the thread
* @param imports Modules to import in the worker. only JS files allowed (over the net import allowed)
*/
constructor(operation: (e: MessageEvent) => T, imports?: Array<string>) {
imports?.forEach((v)=> {
if(v.endsWith(".ts'") || v.endsWith('.ts"')) throw new Error("Threaded imports do no support typescript files");
})
imports?.forEach((v) => {
if (v.endsWith(".ts'") || v.endsWith('.ts"')) {
throw new Error("Threaded imports do no support typescript files");
}
});
this.imports = imports || [];
this.fileName = this.createFile();
this.populateFile(operation);
this.worker = new Worker(new URL(this.fileName, import.meta.url).href, {
type: "module",
});
this.init();
this.cleanUp();
}
private createFile(): string {
Deno.mkdirSync("./tmp_threads", { recursive: true });
Deno.mkdirSync(this.folderName, { recursive: true });
return Deno.makeTempFileSync(
{ prefix: "deno_thread_", suffix: ".js", dir: "./tmp_threads" },
{ prefix: "deno_thread_", suffix: ".js", dir: this.folderName },
);
}

Expand All @@ -36,8 +44,15 @@ onmessage = function(e) {
);
}

private init() {
addEventListener("unload", () => Deno.removeSync(this.fileName));
/**
* Attempt to cleanup files
*/
private async cleanUp() {
await Deno.remove(this.fileName);
try { // attempt to clean the folder in case it is empty
await Deno.remove(this.folderName);
} catch (error) {
}
}

/**
Expand Down

0 comments on commit 62884c1

Please sign in to comment.