Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 943 Bytes

README.md

File metadata and controls

32 lines (23 loc) · 943 Bytes

Access DOM in Web Worker

index.js

// Create Web Worker
const worker = new Worker('worker.js');

// Create proxy
const targetProxy = new TargetProxy(worker);

// Gave access to window object
targetProxy.register('window', window);

worker.js

// link proxy object
const window: any = WorkerProxyObject('window');

// eval native alert function in GUI thread from worker thread
window.alert('Hello From Worker');

// direct access to DOM from worker
window.document.body.innerText = 'Hello From Worker';

How it works

In two words Proxy & Promise. For more details, please see worker.ts file (less than 100 lines of code).

Fell free to conribute