diff --git a/client/src/components/Editor.vue b/client/src/components/Editor.vue index af85e04..2634da9 100644 --- a/client/src/components/Editor.vue +++ b/client/src/components/Editor.vue @@ -119,6 +119,7 @@ import { watchEffect, onMounted, computed, + onBeforeUnmount, watch, } from "vue"; import { @@ -240,6 +241,11 @@ onMounted(() => { computeOffset(null); throttledRefresCellProba(); }); + +onBeforeUnmount(() => { + searchWorker.destroy(); + runWorker.destroy(); +}); function onZoomIn() { zoom.value = zoom.value + 0.1; } @@ -324,7 +330,7 @@ searchWorker.on("search-result", (data) => { }); watchEffect(() => { - // if (!focus.value || !dir.value) return; + if (!focus.value || !dir.value) return; throttledRefresSimpleSearch(); }); diff --git a/client/src/search-worker/index.ts b/client/src/search-worker/index.ts index 76207d1..b5f8575 100644 --- a/client/src/search-worker/index.ts +++ b/client/src/search-worker/index.ts @@ -8,7 +8,7 @@ export type Events = { } export default class SearchWorker extends EventEmitter { - public worker?: Worker; + public worker: Worker; private busy: boolean = false; private queue: { type: string, data: string }[] = []; private sharedBuffer: SharedArrayBuffer; @@ -30,7 +30,7 @@ export default class SearchWorker extends EventEmitter { this.worker = new Worker(new URL('./search-worker', import.meta.url), { type: 'module' }); this.worker.postMessage(this.sharedBuffer); - this.worker?.addEventListener('message', (evt) => this.onMessage(evt)); + this.worker.addEventListener('message', (evt) => this.onMessage(evt)); } search(grid: Grid, coords: Vec, dir: Direction) { @@ -54,7 +54,7 @@ export default class SearchWorker extends EventEmitter { } this.busy = true; this.sharedArray[0] = 0; - this.worker?.postMessage({ type, data }); + this.worker.postMessage({ type, data }); } onMessage(event: MessageEvent) { @@ -83,4 +83,9 @@ export default class SearchWorker extends EventEmitter { if (!this.busy) return; this.sharedArray[0] = 1; } + + destroy() { + this.worker.terminate(); + this.removeAllListeners(); + } } \ No newline at end of file