bc.mjs raw

   1  // TinyJS Runtime — Bus Bridge (Shell SW)
   2  // Routes bus messages via postMessage to/from page clients.
   3  // The page relays to satellite SW iframes.
   4  
   5  let _onMessage = null;
   6  
   7  export function Open(name, onMessage) {
   8    _onMessage = onMessage;
   9    // Bus messages from the page arrive as strings starting with '{'.
  10    self.addEventListener('message', (ev) => {
  11      const d = ev.data;
  12      if (typeof d === 'string' && d.length > 0 && d[0] === '{' && _onMessage) {
  13        _onMessage(d);
  14      }
  15    });
  16    return 1;
  17  }
  18  
  19  export function Send(bcId, msg) {
  20    self.clients.matchAll({ type: 'window' }).then(clients => {
  21      for (const c of clients) {
  22        try { c.postMessage(msg); } catch(e) {}
  23      }
  24    });
  25  }
  26  
  27  export function Close(bcId) {}
  28