https://github.com/nichoth/pull-router
High level router using pull streams
https://github.com/nichoth/pull-router
Last synced: 10 days ago
JSON representation
High level router using pull streams
- Host: GitHub
- URL: https://github.com/nichoth/pull-router
- Owner: nichoth
- Created: 2016-12-09T00:45:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-11T02:10:36.000Z (over 9 years ago)
- Last Synced: 2025-02-14T06:48:50.694Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://github.com/nichoth/pull-router#readme
- Size: 8.79 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# pull router
High level router using pull streams.
## install
$ npm install pull-router
## example
```js
var S = require('pull-stream')
var Router = require('../')
var assert = require('assert')
// through streams go here
var TestRouter = Router([
['/foo', function (params, route) {
return S.map(function (ev) {
return { count: ev }
})
}]
])
var router = TestRouter([
// duplex streams go here
['/foo', function (params, route) {
return {
source: S.once(1),
sink: S.collect(function (err, res) {
// our source is piped through the transform
// we defined above
console.log(route.route) // => /foo
console.log(res[0]) // => { count: 1 }
assert.equal(res[0].count, 1)
}),
// in here we would do something to connect a view
// to the source and sink if we a in a browser
view: 'test'
}
}]
])
// `router()` returns a source stream that emits views and
// listens for route events (in a browser).
// In node call `router().push()` -- `router()` is an instance of
// pull-pushable.
// When the route changes, this stream will pipe the new route and
// unpipe the old one
var routeStream = router()
S(
routeStream,
S.drain(function onRoute (view) {
assert.equal(view, 'test')
})
)
routeStream.push('/foo')
routeStream.end()
```