Skip to content

Commit 2c4d373

Browse files
committed
Construct the web workers in child classes since they are not transpiled during build otherwise
1 parent e3feb27 commit 2c4d373

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/lib/worker/GraphWorkerWrapper.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,23 @@ import Graph from 'graphology';
44
import type { Bom } from '$lib/cyclonedx/models';
55

66
abstract class GraphWorkerWrapper<PayloadType> {
7-
private _worker?: Worker;
8-
private readonly _workerUrl: URL;
7+
private readonly _worker: Worker;
98
private readonly _onGraph: (graph: AbstractGraph) => void;
109

11-
protected constructor(workerUrl: URL, onGraph: (graph: AbstractGraph) => void) {
12-
this._workerUrl = workerUrl;
10+
protected constructor(worker: Worker, onGraph: (graph: AbstractGraph) => void) {
11+
this._worker = worker;
12+
this._worker.onmessage = ({
13+
data: { payload }
14+
}: MessageEvent<PostMessage<SerializedGraph>>) => {
15+
if (payload) {
16+
this._onGraph(new Graph().import(payload));
17+
}
18+
};
1319
this._onGraph = onGraph;
1420
}
1521

16-
get worker() {
17-
if (!this._worker) {
18-
this._worker = new Worker(this._workerUrl, {
19-
type: 'module'
20-
});
21-
22-
this._worker.onmessage = ({
23-
data: { payload }
24-
}: MessageEvent<PostMessage<SerializedGraph>>) => {
25-
if (payload) {
26-
this._onGraph(new Graph().import(payload));
27-
}
28-
};
29-
}
30-
return this._worker;
31-
}
32-
3322
sendMessage(payload: PayloadType) {
34-
this.worker.postMessage({
23+
this._worker.postMessage({
3524
payload: payload
3625
});
3726
}
@@ -45,12 +34,22 @@ abstract class GraphWorkerWrapper<PayloadType> {
4534

4635
export class TreeGenerationWorkerWrapper extends GraphWorkerWrapper<Bom> {
4736
constructor(onGraph: (graph: AbstractGraph) => void) {
48-
super(new URL('$lib/worker/tree-generation.ts', import.meta.url), onGraph);
37+
super(
38+
new Worker(new URL('$lib/worker/tree-generation.ts', import.meta.url), {
39+
type: 'module'
40+
}),
41+
onGraph
42+
);
4943
}
5044
}
5145

5246
export class LayoutGraphWorkerWrapper extends GraphWorkerWrapper<SerializedGraph> {
5347
constructor(onGraph: (graph: AbstractGraph) => void) {
54-
super(new URL('$lib/worker/layout-graph.ts', import.meta.url), onGraph);
48+
super(
49+
new Worker(new URL('$lib/worker/layout-graph.ts', import.meta.url), {
50+
type: 'module'
51+
}),
52+
onGraph
53+
);
5554
}
5655
}

0 commit comments

Comments
 (0)