worker.js (931B)
1 importScripts("/common/gc.js"); 2 3 var c; 4 5 async function handler(e, reply) { 6 if (e.data.ping) { 7 c.postMessage(e.data.ping); 8 return; 9 } 10 if (e.data.blob) { 11 (() => { 12 c.postMessage({blob: new Blob(e.data.blob)}); 13 })(); 14 await garbageCollect(); 15 } 16 c = new BroadcastChannel(e.data.channel); 17 let messages = []; 18 c.onmessage = e => { 19 if (e.data === 'ready') { 20 // Ignore any 'ready' messages from the other thread since there could 21 // be some race conditions between this BroadcastChannel instance 22 // being created / ready to receive messages and the message being sent. 23 return; 24 } 25 messages.push(e.data); 26 if (e.data == 'done') 27 reply(messages); 28 }; 29 c.postMessage('from worker'); 30 } 31 32 onmessage = e => handler(e, postMessage); 33 34 onconnect = e => { 35 let port = e.ports[0]; 36 port.onmessage = e => handler(e, msg => port.postMessage(msg)); 37 };