Code Monkey home page Code Monkey logo

pull-with-latest's Introduction

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

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]
}

pull-with-latest's People

Contributors

nichoth avatar

Stargazers

Andrejs Agejevs avatar Robin Larsson avatar André Staltz avatar

Watchers

James Cloos avatar  avatar  avatar

pull-with-latest's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.