Skip to content

Commit 3d9215c

Browse files
authored
fix: preserve externally provided earcut pool (#218) (#219)
1 parent b68d6e2 commit 3d9215c

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/deck.gl-geoarrow/src/layers/solid-polygon-layer.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export class GeoArrowSolidPolygonLayer<
199199
triangles: Uint32Array | null;
200200
earcutWorkerPool: Pool<FunctionThread> | null;
201201
earcutWorkerRequest: Promise<string> | null;
202+
ownsEarcutWorkerPool: boolean;
202203
};
203204

204205
override initializeState(_context: LayerContext): void {
@@ -211,6 +212,7 @@ export class GeoArrowSolidPolygonLayer<
211212
? null
212213
: fetch(this.props.earcutWorkerUrl).then((resp) => resp.text()),
213214
earcutWorkerPool: this.props.earcutWorkerPool || null,
215+
ownsEarcutWorkerPool: false,
214216
};
215217
}
216218

@@ -243,14 +245,17 @@ export class GeoArrowSolidPolygonLayer<
243245
this.props.earcutWorkerPoolSize || 1,
244246
);
245247
this.state.earcutWorkerPool = pool;
248+
this.state.ownsEarcutWorkerPool = true;
246249
return this.state.earcutWorkerPool;
247250
} catch (_err) {
248251
return null;
249252
}
250253
}
251254

252255
override async finalizeState(_context: LayerContext): Promise<void> {
253-
await this.state?.earcutWorkerPool?.terminate();
256+
if (this.state?.ownsEarcutWorkerPool) {
257+
await this.state?.earcutWorkerPool?.terminate();
258+
}
254259
console.log("terminated");
255260
}
256261

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { FunctionThread, Pool } from "threads";
2+
import { describe, expect, it, vi } from "vitest";
3+
import { GeoArrowSolidPolygonLayer } from "../src/layers/solid-polygon-layer.js";
4+
5+
describe("GeoArrowSolidPolygonLayer earcut worker pool lifecycle", () => {
6+
it("does not terminate an externally provided worker pool", async () => {
7+
const terminate = vi.fn(async () => {});
8+
9+
const externalPool = {
10+
terminate,
11+
} as unknown as Pool<FunctionThread>;
12+
13+
const layer = new GeoArrowSolidPolygonLayer({
14+
id: "test-layer",
15+
data: null as never,
16+
earcutWorkerPool: externalPool,
17+
});
18+
19+
layer.initializeState({} as never);
20+
await layer.finalizeState({} as never);
21+
22+
expect(terminate).not.toHaveBeenCalled();
23+
});
24+
});

0 commit comments

Comments
 (0)