Skip to content

Commit

Permalink
cleanup workers
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Nicolle committed May 18, 2023
1 parent e61620f commit a83b398
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion client/src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ import {
watchEffect,
onMounted,
computed,
onBeforeUnmount,
watch,
} from "vue";
import {
Expand Down Expand Up @@ -240,6 +241,11 @@ onMounted(() => {
computeOffset(null);
throttledRefresCellProba();
});
onBeforeUnmount(() => {
searchWorker.destroy();
runWorker.destroy();
});
function onZoomIn() {
zoom.value = zoom.value + 0.1;
}
Expand Down Expand Up @@ -324,7 +330,7 @@ searchWorker.on("search-result", (data) => {
});
watchEffect(() => {
// if (!focus.value || !dir.value) return;
if (!focus.value || !dir.value) return;
throttledRefresSimpleSearch();
});
</script>
Expand Down
11 changes: 8 additions & 3 deletions client/src/search-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type Events = {
}

export default class SearchWorker extends EventEmitter<Events> {
public worker?: Worker;
public worker: Worker;
private busy: boolean = false;
private queue: { type: string, data: string }[] = [];
private sharedBuffer: SharedArrayBuffer;
Expand All @@ -30,7 +30,7 @@ export default class SearchWorker extends EventEmitter<Events> {

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) {
Expand All @@ -54,7 +54,7 @@ export default class SearchWorker extends EventEmitter<Events> {
}
this.busy = true;
this.sharedArray[0] = 0;
this.worker?.postMessage({ type, data });
this.worker.postMessage({ type, data });
}

onMessage(event: MessageEvent) {
Expand Down Expand Up @@ -83,4 +83,9 @@ export default class SearchWorker extends EventEmitter<Events> {
if (!this.busy) return;
this.sharedArray[0] = 1;
}

destroy() {
this.worker.terminate();
this.removeAllListeners();
}
}

0 comments on commit a83b398

Please sign in to comment.