https://github.com/nichoth/pull-with-latest
Combine a stream with the latest value from another stream
https://github.com/nichoth/pull-with-latest
Last synced: 11 months ago
JSON representation
Combine a stream with the latest value from another stream
- Host: GitHub
- URL: https://github.com/nichoth/pull-with-latest
- Owner: nichoth
- Created: 2017-04-05T00:19:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-04T23:44:50.000Z (over 8 years ago)
- Last Synced: 2024-12-19T01:32:41.141Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# pull with latest
Take two streams, and return a new stream that emits every value from the first stream with the most recent value from the other stream. This operator is sometimes called 'sample'.
## install
npm install pull-with-latest
## example
```js
var test = require('tape')
var withLatest = require('../')
var S = require('pull-stream')
test('pass null for the default predicate', function (t) {
t.plan(2)
var predicate = null
S(
withLatest.apply(null, [predicate].concat(sources())),
S.collect(function (err, res) {
t.error(err)
t.deepEqual(res, [
[2, 'a'],
[4, 'b'],
[6, 'c']
], 'should combine the streams ok')
})
)
})
test('use predicate function', function (t) {
t.plan(2)
var predicate = function (a, b) {
return { a: a, b: b }
}
S(
withLatest.apply(null, [predicate].concat(sources())),
S.collect(function (err, res) {
t.error(err)
t.deepEqual(res, [
{ a:2, b:'a' },
{ a:4, b:'b' },
{ a:6, b:'c' }
], 'should combine the streams ok')
})
)
})
function sources () {
var sampler = S(
S.values(['a','b','c']),
S.asyncMap(function (ev, cb) {
setTimeout(function () {
cb(null, ev)
}, 210)
})
)
var otherStream = S(
S.values([1,2,3,4,5,6]),
S.asyncMap(function (ev, cb) {
setTimeout(function () {
cb(null, ev)
}, 100)
})
)
return [otherStream, sampler]
}
```