Add conditional compilation

This commit is contained in:
Maximilian Ammann 2022-09-08 20:17:09 +02:00
parent 2f4c05f2b4
commit 156e63daf4
11 changed files with 74 additions and 53 deletions

View File

@ -72,7 +72,7 @@ let baseConfig = {
let config = {
...baseConfig,
entryPoints: ['src/index.ts'],
entryPoints: multithreaded ? ['src/sync/index.ts'] : ['src/unsync/index.ts'],
incremental: argv.watch,
plugins: [
inlineWorker({
@ -112,9 +112,10 @@ const emitTypeScript = () => {
"tsc",
"--",
"-m", "es2022",
"--project", multithreaded ? `${getLibDirectory()}/tsconfig.sync.json` : `${getLibDirectory()}/tsconfig.unsync.json`,
"-outDir", outDirectory,
"--declaration",
"--emitDeclarationOnly"
], {
cwd: '.',
stdio: 'inherit',

6
web/lib/src/canvas.ts Normal file
View File

@ -0,0 +1,6 @@
export const preventDefaultTouchActions = () => {
document.body.querySelectorAll("canvas").forEach(canvas => {
canvas.addEventListener("touchstart", e => e.preventDefault())
canvas.addEventListener("touchmove", e => e.preventDefault())
})
}

View File

@ -1,28 +0,0 @@
import init from "./wasm/maplibre";
export const initialize = async (wasmPath: string) => {
if (MULTITHREADED) {
await initializeSharedModule(wasmPath)
} else {
// @ts-ignore
await init(wasmPath)
}
}
export const initializeExisting = async (module: string, memory?: string) => {
if (MULTITHREADED) {
// @ts-ignore
await init(module, memory)
} else {
// @ts-ignore
await init(module)
}
}
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)
}

View File

@ -1,5 +0,0 @@
import {initializeExisting} from "./module";
onmessage = async message => {
}

View File

@ -1,18 +1,17 @@
import {create_pool_scheduler, run} from "./wasm/maplibre"
import {create_pool_scheduler, run} from "../wasm/maplibre"
import {Spector} from "spectorjs"
// @ts-ignore esbuild plugin is handling this
import MultithreadedPoolWorker from './multithreaded-pool.worker.js';
import {checkRequirements, checkWasmFeatures} from "../browser";
import init from "../wasm/maplibre";
import {preventDefaultTouchActions} from "../canvas";
// @ts-ignore esbuild plugin is handling this
import PoolWorker from './pool.worker.js';
import {initialize} from "./module";
import {checkRequirements, checkWasmFeatures} from "./browser";
const initializeSharedModule = async (wasmPath) => {
let MEMORY_PAGES = 16 * 1024
const preventDefaultTouchActions = () => {
document.body.querySelectorAll("canvas").forEach(canvas => {
canvas.addEventListener("touchstart", e => e.preventDefault())
canvas.addEventListener("touchmove", e => e.preventDefault())
})
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) => {
@ -32,14 +31,12 @@ export const startMapLibre = async (wasmPath: string | undefined, workerPath: st
preventDefaultTouchActions();
await initialize(wasmPath);
await initializeSharedModule(wasmPath);
const schedulerPtr = create_pool_scheduler(() => {
let CurrentWorker = MULTITHREADED ? MultithreadedPoolWorker : PoolWorker;
return workerPath ? new Worker(workerPath, {
type: 'module'
}) : CurrentWorker();
}) : PoolWorker();
})
await run(schedulerPtr)

View File

@ -1,5 +1,8 @@
import {worker_entry} from "./wasm/maplibre"
import {initializeExisting} from "./module";
import init, {worker_entry} from "../wasm/maplibre"
const initializeExisting = async (module: string, memory?: string) => {
await init(module, memory)
}
onmessage = async message => {
const initialised = initializeExisting(message.data[0], message.data[1]).catch(err => {

View File

@ -0,0 +1,35 @@
import init, {create_pool_scheduler, 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 './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();
await init(wasmPath);
const schedulerPtr = create_pool_scheduler(() => {
return workerPath ? new Worker(workerPath, {
type: 'module'
}) : PoolWorker();
})
await run(schedulerPtr)
}

View File

@ -0,0 +1,3 @@
onmessage = async message => {
}

View File

@ -13,5 +13,4 @@
"types": ["maplibre", "env"],
"typeRoots": ["@types"]
},
"include": ["./src/**/*"]
}
}

View File

@ -0,0 +1,5 @@
{
"extends": "./tsconfig",
"include": ["./src/**/*"],
"exclude": ["./src/unsync/*"]
}

View File

@ -0,0 +1,5 @@
{
"extends": "./tsconfig",
"include": ["./src/**/*"],
"exclude": ["./src/sync/*"]
}