-
-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove web worker replacement (#976)
- Loading branch information
Showing
9 changed files
with
120 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type {WorkerInterface, WorkerGlobalScopeInterface, MessageListener} from '../../../src/util/web_worker'; | ||
import MaplibreWorker from '../../../src/source/worker'; | ||
|
||
export 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() { } | ||
} | ||
|
||
(global as any).Worker = function Worker(_: string) { | ||
const parentListeners = []; | ||
const workerListeners = []; | ||
const parentBus = new MessageBus(workerListeners, parentListeners); | ||
const workerBus = new MessageBus(parentListeners, workerListeners); | ||
|
||
parentBus.target = workerBus; | ||
workerBus.target = parentBus; | ||
|
||
new MaplibreWorker(workerBus); | ||
|
||
return parentBus; | ||
}; |