twpipe

two-way communication

Usage no npm install needed!

<script type="module">
  import twpipe from 'https://cdn.skypack.dev/twpipe';
</script>

README

twpipe

Build status Test coverage NPM version NPM Downloads Prettier Conventional Commits

Two-way communication (with pending queue, timeout features) for iframe / web worker / ...

Installation

npm install twpipe
# or use yarn
yarn add twpipe

Usage

  • worker.js
import { createPipeB } from 'twpipe'

const pipe = createPipeB(
  self,
  {
    worker: (a, b) => {
      return Promise.resolve(a + b)
    }
  },
  'xx-worker'
)

pipe.main('main').then(console.log) // => main
  • main.js
import { createPipe } from 'twpipe'
const worker = new Worker('./worker.js')

const pipe = createPipe(
  worker,
  {
    main: () => 'main'
  },
  'xx-worker'
)

pipe.rpc('worker', 1, 2).then(console.log) // => 3
pipe.rpc('404') // throw error async

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git checkout -b fix-which-bug
  • Start your magic work now
  • Make sure npm test passes
  • Commit your changes:
    git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
  • Push to the branch: git push
  • Submit a pull request :)

Authors

This library is written and maintained by 余聪, yucong@yuanfudao.com.

License

MIT - 余聪 🐟