Skip to content

Commit 36b179a

Browse files
authored
Wrap worker instantiation in try/catch; fix for non-served HTML files (#108)
* Wrap worker instantiation in try/catch * bump version * check for file:// in url * bump version * remove console.log
1 parent b8b2cde commit 36b179a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"examples/*"
55
],
66
"name": "@geoarrow/deck.gl-layers",
7-
"version": "0.3.0-beta.12",
7+
"version": "0.3.0-beta.14",
88
"type": "module",
99
"description": "",
1010
"source": "src/index.ts",

src/solid-polygon-layer.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,29 @@ export class GeoArrowSolidPolygonLayer<
155155
return null;
156156
}
157157

158-
const pool = Pool<FunctionThread>(
159-
() => spawn(BlobWorker.fromText(workerText)),
160-
8,
161-
);
162-
this.state.earcutWorkerPool = pool;
163-
return this.state.earcutWorkerPool;
158+
// Some environments are not able to execute `importScripts`
159+
// E.g. on a non-served HTML file (e.g. from lonboard export) you get
160+
// Uncaught DOMException: Failed to execute 'importScripts' on
161+
// 'WorkerGlobalScope': The script at
162+
// 'blob:null/4ffb0b98-d1bd-4d9e-be52-998f50829723' failed to load.
163+
//
164+
// Additionally, it appears impossible to _catch_ this exception (at least
165+
// on Chrome), so we'll hack around this by additionally checking if the
166+
// current file is served from file://
167+
if (window?.location?.href.startsWith("file://")) {
168+
return null;
169+
}
170+
171+
try {
172+
const pool = Pool<FunctionThread>(
173+
() => spawn(BlobWorker.fromText(workerText)),
174+
8,
175+
);
176+
this.state.earcutWorkerPool = pool;
177+
return this.state.earcutWorkerPool;
178+
} catch (err) {
179+
return null;
180+
}
164181
}
165182

166183
async finalizeState(context: LayerContext): Promise<void> {
@@ -513,7 +530,7 @@ export class GeoArrowSolidPolygonLayer<
513530
recordBatchIdx,
514531
tableOffsets: this.state.tableOffsets,
515532

516-
id: `${this.props.id}-geoarrow-point-${recordBatchIdx}`,
533+
id: `${this.props.id}-geoarrow-solid-polygon-multi-${recordBatchIdx}`,
517534
data: {
518535
// @ts-expect-error passed through to enable use by function accessors
519536
data: table.batches[recordBatchIdx],
@@ -553,7 +570,8 @@ export class GeoArrowSolidPolygonLayer<
553570
});
554571
}
555572

556-
const layer = new SolidPolygonLayer(this.getSubLayerProps(props));
573+
const finalProps = this.getSubLayerProps(props);
574+
const layer = new SolidPolygonLayer(finalProps);
557575
layers.push(layer);
558576
}
559577

0 commit comments

Comments
 (0)