maplibre-rs/web/lib/src/index.ts
Max Ammann c12f3f1127
Improve error handling (#222)
* Introduce proper error types for a lot of functions, remove main catch-all type

* Fix compilation and clippy issues

* Map, unmap and drop always

* Remove not required parentheses

* Fix running on non-vulkan platforms

* Add and use thiserror crate
2022-12-12 20:06:12 +01:00

65 lines
2.1 KiB
TypeScript

import * as maplibre from "./wasm/maplibre"
import {Spector} from "spectorjs"
import {checkRequirements, checkWasmFeatures} from "./browser";
import {preventDefaultTouchActions} from "./canvas";
// @ts-ignore esbuild plugin is handling this
import MultithreadedPoolWorker from './multithreaded/multithreaded-pool.worker.js';
// @ts-ignore esbuild plugin is handling this
import PoolWorker from './singlethreaded/pool.worker.js';
export const startMapLibre = async (wasmPath: string | undefined, workerPath: string | undefined) => {
await checkWasmFeatures()
let message = checkRequirements();
if (message) {
console.error(message)
alert(message)
return
}
if (WEBGL) {
let spector = new Spector()
spector.displayUI()
}
preventDefaultTouchActions();
if (MULTITHREADED) {
const MEMORY = 209715200; // 200MB
const PAGES = 64 * 1024;
const memory = new WebAssembly.Memory({initial: 1024, maximum: MEMORY / PAGES, shared: true})
await maplibre.default(wasmPath, memory)
await maplibre.run_maplibre(() => {
return workerPath ? new Worker(workerPath, {
type: 'module'
}) : MultithreadedPoolWorker();
});
} else {
const memory = new WebAssembly.Memory({initial: 1024, shared: false})
await maplibre.default(wasmPath, memory);
await maplibre.run_maplibre((ptr) => {
let worker: Worker = workerPath ? new Worker(workerPath, {
type: 'module'
}) : PoolWorker();
worker.onmessage = (message: MessageEvent) => {
// WARNING: Do not modify data passed from Rust!
let in_transfer = message.data;
const main_entry = maplibre["singlethreaded_main_entry"];
if (!main_entry) {
throw Error("singlethreaded_main_entry is not defined. Maybe the Rust build used the wrong build configuration.")
}
main_entry(ptr, in_transfer)
}
return worker;
});
}
}