Skip to content

Commit

Permalink
Remove worker node mock from src
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum committed Feb 7, 2022
1 parent d0fb34a commit 805f65f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 104 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions src/source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string>};
Expand All @@ -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);

Expand Down
98 changes: 3 additions & 95 deletions src/util/web_worker.ts
Original file line number Diff line number Diff line change
@@ -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<string>): 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<MessageListener>;
postListeners: Array<MessageListener>;
target: MessageBus;
registerWorkerSource: any;
registerRTLTextPlugin: any;

constructor(addListeners: Array<MessageListener>, postListeners: Array<MessageListener>) {
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;
5 changes: 2 additions & 3 deletions src/util/worker_pool.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,13 +13,13 @@ export default class WorkerPool {
active: {
[_ in number | string]: boolean;
};
workers: Array<WorkerInterface>;
workers: Array<any>;

constructor() {
this.active = {};
}

acquire(mapId: number | string): Array<WorkerInterface> {
acquire(mapId: number | string): Array<any> {
if (!this.workers) {
// Lazily look up the value of mapboxgl.workerCount so that
// client code has had a chance to set it.
Expand Down

0 comments on commit 805f65f

Please sign in to comment.