worker-event-bridge

A channel to transmit DOM-based events setup and responses between a worker and the main (UI) threads.

Usage no npm install needed!

<script type="module">
  import workerEventBridge from 'https://cdn.skypack.dev/worker-event-bridge';
</script>

README

worker-event-bridge

main (UI) thread usage

var eventBridgeMain = require('worker-event-bridge/main')

var worker = new Worker('./worker.js')
eventBridgeMain(worker)

worker thread usage

var eventBridgeWorker = require('worker-event-bridge/worker')

eventBridgeWorker('select', 'change', function (val) {
  console.log(val) // reports selected value
})

A Cycle.js implementation example.

use in existing message channels

main.js

var eventBridgeMain = require('worker-event-bridge/main')

var worker = new Worker('./worker.js')
worker.addEventListener('message', function (evt) {
  evt.data.forEach(function (data) {
    if ( 'event' === data.cmd ) {
      eventBridgeMain.addListener(worker, data.event)
    }
  })
}, false)

worker.js

var eventBridgeWorker = require('worker-event-bridge/worker')

eventBridgeWorker.send('select', 'change')

self.addEventListener('message', function (evt) {
  eventBridge.receive(evt, 'select', 'change', function (val) {
    console.log(val)
  })
}, false)

license

MIT