Skip to content

Commit

Permalink
Wrap worker instantiation in try/catch; fix for non-served HTML files (
Browse files Browse the repository at this point in the history
…#108)

* Wrap worker instantiation in try/catch

* bump version

* check for file:// in url

* bump version

* remove console.log
  • Loading branch information
kylebarron authored Feb 26, 2024
1 parent b8b2cde commit 36b179a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"examples/*"
],
"name": "@geoarrow/deck.gl-layers",
"version": "0.3.0-beta.12",
"version": "0.3.0-beta.14",
"type": "module",
"description": "",
"source": "src/index.ts",
Expand Down
34 changes: 26 additions & 8 deletions src/solid-polygon-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,29 @@ export class GeoArrowSolidPolygonLayer<
return null;
}

const pool = Pool<FunctionThread>(
() => spawn(BlobWorker.fromText(workerText)),
8,
);
this.state.earcutWorkerPool = pool;
return this.state.earcutWorkerPool;
// Some environments are not able to execute `importScripts`
// E.g. on a non-served HTML file (e.g. from lonboard export) you get
// Uncaught DOMException: Failed to execute 'importScripts' on
// 'WorkerGlobalScope': The script at
// 'blob:null/4ffb0b98-d1bd-4d9e-be52-998f50829723' failed to load.
//
// Additionally, it appears impossible to _catch_ this exception (at least
// on Chrome), so we'll hack around this by additionally checking if the
// current file is served from file://
if (window?.location?.href.startsWith("file://")) {
return null;
}

try {
const pool = Pool<FunctionThread>(
() => spawn(BlobWorker.fromText(workerText)),
8,
);
this.state.earcutWorkerPool = pool;
return this.state.earcutWorkerPool;
} catch (err) {
return null;
}
}

async finalizeState(context: LayerContext): Promise<void> {
Expand Down Expand Up @@ -513,7 +530,7 @@ export class GeoArrowSolidPolygonLayer<
recordBatchIdx,
tableOffsets: this.state.tableOffsets,

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

const layer = new SolidPolygonLayer(this.getSubLayerProps(props));
const finalProps = this.getSubLayerProps(props);
const layer = new SolidPolygonLayer(finalProps);
layers.push(layer);
}

Expand Down

0 comments on commit 36b179a

Please sign in to comment.