1.0.1 • Published 4 years ago

pull-routing v1.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

pull-routing

A special pull-stream sink that multiplexes to several sinks based on router rules.

Usage

var Router = require('pull-routing')
const pull = require('pull-stream')

const router = Router()

router.addRoute(
  x => x % 2 === 0,
  pull.drain(x => console.log('EVEN', x))
)

router.addRoute(
  x => x % 2 === 1,
  pull.drain(x => console.log('ODD', x))
)

pull(
  pull.values([0,1,2,3]),
  router
)

outputs

EVEN 0
ODD 1
EVEN 2
ODD 3

API

const Router = require('pull-routing')

const router = Router()

Create a new router. Acts like a pull-stream sink.

router.addRoute(fn, sink)

Adds a route to the router. For data received by the router, if fn(data) returns true, the data will go to sink (which can be a sink OR through stream).

Behaviour Notes

  • If a sink attached to a route ends or errors, that route is removed from the router.
  • If the source feeding the router ends or errors, it is propagated to all sinks attached to the router.
  • If more than one route matches incoming data, the route that was added earlier wins.

Install

With npm installed, run

$ npm install pull-routing

License

MIT