From 62884c174398094d584756bad23069958c9831d0 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 15 Aug 2020 15:47:44 +0200 Subject: [PATCH] Made Thread silent (cached bg code) --- Thread.bundle.js | 21 ++++++++++++++++----- Thread.ts | 31 +++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Thread.bundle.js b/Thread.bundle.js index 970100a..8678236 100644 --- a/Thread.bundle.js +++ b/Thread.bundle.js @@ -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, ` @@ -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); diff --git a/Thread.ts b/Thread.ts index 5fcc359..628fce7 100644 --- a/Thread.ts +++ b/Thread.ts @@ -2,22 +2,30 @@ export default class Thread { public fileName: string; public worker: Worker; private imports: Array; + 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) { - 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 }, ); } @@ -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) { + } } /**