Restructure web module to include JS code for multithreaded and non-multithreaded

This commit is contained in:
Maximilian Ammann 2022-09-18 11:42:55 +02:00
parent f675192442
commit dd6c7481e2
6 changed files with 75 additions and 98 deletions

View File

@ -72,7 +72,7 @@ let baseConfig = {
let config = {
...baseConfig,
entryPoints: multithreaded ? ['src/sync/index.ts'] : ['src/unsync/index.ts'],
entryPoints:['src/index.ts'],
incremental: argv.watch,
plugins: [
inlineWorker({

66
web/lib/src/index.ts Normal file
View File

@ -0,0 +1,66 @@
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 './sync/multithreaded-pool.worker.js';
// @ts-ignore esbuild plugin is handling this
import PoolWorker from './unsync/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(await maplibre.create_map(() => {
return workerPath ? new Worker(workerPath, {
type: 'module'
}) : MultithreadedPoolWorker();
}))
} else {
const memory = new WebAssembly.Memory({initial: 1024, shared: false})
await maplibre.default(wasmPath, memory);
let callback = [undefined]
let map = await maplibre.create_map(() => {
let worker = workerPath ? new Worker(workerPath, {
type: 'module'
}) : PoolWorker();
worker.onmessage = (message) => {
callback[0](message)
}
return worker;
})
let clonedMap = maplibre.clone_map(map)
callback[0] = (message) => {
// @ts-ignore TODO unsync_main_entry may not be defined
maplibre.unsync_main_entry(clonedMap, message.data[0], new Uint8Array(message.data[1]))
}
await maplibre.run(map)
}
}

View File

@ -1,42 +0,0 @@
import init, {create_map, run} from "../wasm/maplibre"
import {Spector} from "spectorjs"
import {checkRequirements, checkWasmFeatures} from "../browser";
import {preventDefaultTouchActions} from "../canvas";
// @ts-ignore esbuild plugin is handling this
import PoolWorker from './multithreaded-pool.worker.js';
const initializeSharedModule = async (wasmPath) => {
let MEMORY_PAGES = 16 * 1024
const memory = new WebAssembly.Memory({initial: 1024, maximum: MEMORY_PAGES, shared: true})
// @ts-ignore
await init(wasmPath, memory)
}
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();
await initializeSharedModule(wasmPath);
let map = await create_map(() => {
return workerPath ? new Worker(workerPath, {
type: 'module'
}) : PoolWorker();
})
await run(map)
}

View File

@ -1,7 +1,7 @@
import init, {sync_worker_entry} from "../wasm/maplibre"
import * as maplibre from "../wasm/maplibre"
onmessage = async message => {
const initialised = init(message.data[0], message.data[1]).catch(err => {
const initialised = maplibre.default(message.data[0], message.data[1]).catch(err => {
// Propagate to main `onerror`:
setTimeout(() => {
throw err;
@ -13,6 +13,7 @@ onmessage = async message => {
self.onmessage = async message => {
// This will queue further commands up until the module is fully initialised:
await initialised;
await sync_worker_entry(message.data);
// @ts-ignore TODO may not exist
await maplibre.sync_worker_entry(message.data);
};
}

View File

@ -1,49 +0,0 @@
import init, {run, create_map, clone_map, unsync_main_entry} from "../wasm/maplibre"
import {Spector} from "spectorjs"
import {checkRequirements, checkWasmFeatures} from "../browser";
import {preventDefaultTouchActions} from "../canvas";
// @ts-ignore esbuild plugin is handling this
import PoolWorker from './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();
const memory = new WebAssembly.Memory({initial: 1024, shared: false})
await init(wasmPath, memory);
let callback = [undefined]
let map = await create_map(() => {
let worker = workerPath ? new Worker(workerPath, {
type: 'module'
}) : PoolWorker();
worker.onmessage = (message) => {
callback[0](message)
}
return worker;
})
let clonedMap = clone_map(map)
callback[0] = (message) => {
unsync_main_entry(clonedMap, message.data[0], new Uint8Array(message.data[1]))
}
run(map)
}

View File

@ -1,9 +1,9 @@
import init, {unsync_worker_entry} from "../wasm/maplibre"
import * as maplibre from "../wasm/maplibre"
onmessage = async message => {
const memory = new WebAssembly.Memory({initial: 1024, shared: false})
let module = message.data[0];
const initialised = init(module, memory).catch(err => {
const initialised = maplibre.default(module, memory).catch(err => {
// Propagate to main `onerror`:
setTimeout(() => {
throw err;
@ -17,6 +17,7 @@ onmessage = async message => {
await initialised;
let procedure_ptr = message.data[0];
let input = message.data[1];
await unsync_worker_entry(procedure_ptr, input);
// @ts-ignore TODO
await maplibre.unsync_worker_entry(procedure_ptr, input);
};
}