diff --git a/package.json b/package.json index 58760bf2574..f433637a757 100644 --- a/package.json +++ b/package.json @@ -135,9 +135,6 @@ "ts-node": "^10.4.0", "typescript": "^4.5.5" }, - "browser": { - "./src/util/web_worker.ts": "./build/web_worker_replacement.ts" - }, "scripts": { "generate-shaders": "node --loader ts-node/esm --experimental-specifier-resolution=node build/generate-shaders.ts", "generate-struct-arrays": "node --loader ts-node/esm --experimental-specifier-resolution=node build/generate-struct-arrays.ts", diff --git a/src/source/worker.ts b/src/source/worker.ts index daa9f0a4702..d87e8105970 100644 --- a/src/source/worker.ts +++ b/src/source/worker.ts @@ -17,7 +17,6 @@ import type { TileParameters } from '../source/worker_source'; -import type {WorkerGlobalScopeInterface} from '../util/web_worker'; import type {Callback} from '../types/callback'; import type {LayerSpecification} from '../style-spec/types'; import type {PluginState} from './rtl_text_plugin'; @@ -26,7 +25,7 @@ import type {PluginState} from './rtl_text_plugin'; * @private */ export default class Worker { - self: WorkerGlobalScopeInterface; + self: any; actor: Actor; layerIndexes: {[_: string]: StyleLayerIndex}; availableImages: {[_: string]: Array}; @@ -49,7 +48,7 @@ export default class Worker { }; referrer: string; - constructor(self: WorkerGlobalScopeInterface) { + constructor(self: any) { this.self = self; this.actor = new Actor(self, this); diff --git a/src/util/web_worker.ts b/src/util/web_worker.ts index 836f5fbd365..766594a8a54 100644 --- a/src/util/web_worker.ts +++ b/src/util/web_worker.ts @@ -1,97 +1,5 @@ -// When Rollup builds the main bundle this file is replaced with ./build/web_worker_replacement.js -// See package.json 'browser' field and rollup documentation. -// This file is intended for use in the GL-JS test suite when they run on node since node doesn't support workers. -// It implements a MessageBus main thread interface +import maplibregl from '../index'; -import MaplibreWorker from '../source/worker'; - -import type {WorkerSource} from '../source/worker_source'; - -type MessageListener = ( - a: { - data: any; - target: any; - } -) => unknown; - -// The main thread interface. Provided by Worker in a browser environment, -// and MessageBus below in a node environment. -export interface WorkerInterface { - addEventListener(type: 'message', listener: MessageListener): void; - removeEventListener(type: 'message', listener: MessageListener): void; - postMessage(message: any): void; - terminate(): void; -} - -export interface WorkerGlobalScopeInterface { - importScripts(...urls: Array): void; - registerWorkerSource: ( - b: string, - a: { - new(...args: any): WorkerSource; - } - ) => void; - registerRTLTextPlugin: (_: any) => void; +export default function () { + return new Worker(maplibregl.workerUrl); } - -class MessageBus implements WorkerInterface, WorkerGlobalScopeInterface { - addListeners: Array; - postListeners: Array; - target: MessageBus; - registerWorkerSource: any; - registerRTLTextPlugin: any; - - constructor(addListeners: Array, postListeners: Array) { - this.addListeners = addListeners; - this.postListeners = postListeners; - } - - addEventListener(event: 'message', callback: MessageListener) { - if (event === 'message') { - this.addListeners.push(callback); - } - } - - removeEventListener(event: 'message', callback: MessageListener) { - const i = this.addListeners.indexOf(callback); - if (i >= 0) { - this.addListeners.splice(i, 1); - } - } - - postMessage(data: any) { - setTimeout(() => { - try { - for (const listener of this.postListeners) { - listener({data, target: this.target}); - } - } catch (e) { - console.error(e); - } - }, 0); - } - - terminate() { - this.addListeners.splice(0, this.addListeners.length); - this.postListeners.splice(0, this.postListeners.length); - } - - importScripts() { } -} - -export default function workerFactory(): WorkerInterface { - const parentListeners = [], - workerListeners = [], - parentBus = new MessageBus(workerListeners, parentListeners), - workerBus = new MessageBus(parentListeners, workerListeners); - - parentBus.target = workerBus; - workerBus.target = parentBus; - - new workerFactory.Worker(workerBus); - - return parentBus; -} - -// expose to allow stubbing in unit tests -workerFactory.Worker = MaplibreWorker; diff --git a/src/util/worker_pool.ts b/src/util/worker_pool.ts index 2c007aa7dfb..070abc48000 100644 --- a/src/util/worker_pool.ts +++ b/src/util/worker_pool.ts @@ -1,5 +1,4 @@ import webWorkerFactory from './web_worker'; -import type {WorkerInterface} from './web_worker'; import browser from './browser'; export const PRELOAD_POOL_ID = 'mapboxgl_preloaded_worker_pool'; @@ -14,13 +13,13 @@ export default class WorkerPool { active: { [_ in number | string]: boolean; }; - workers: Array; + workers: Array; constructor() { this.active = {}; } - acquire(mapId: number | string): Array { + acquire(mapId: number | string): Array { if (!this.workers) { // Lazily look up the value of mapboxgl.workerCount so that // client code has had a chance to set it.