Giter VIP home page Giter VIP logo

callbag-pull-when's Introduction

callbag-pull-when

Callbag operator that pulls (samples) from source when provided listenable emits. It's sampleWhen's version - this one works with pullable source, while the other one works with listenables.

It works similar to Rx's sample.

Example

import forEach from 'callbag-for-each'
import fromIter from 'callbag-from-iter'
import pipe from 'callbag-pipe'
import pullWhen from 'callbag-pull-when'
import timer from 'callbag-timer'

pipe(
  fromIter([10, 20, 30, 40]),
  pullWhen(timer(1000, 2000)),
  forEach(value => {
    // will log 10 after 1 second, and 20, 30, 40 after 2 seconds from each other
    console.log(value)
  }),
)

callbag-pull-when's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mperktold

callbag-pull-when's Issues

Pull sampler when input source emits

First of all, I wanted to say I like how this operator allows to interfrate pullable and listenable sources to form powerful complex pipelines.
In this sense, I propose a feature request which would make it even more powerful.

With the current implementation, we can provide a listenable sampler that tells us when to pull, e.g. an interval that emits every second.

However, there is no easy way to pull the source based on its emissions, like pulling it whenever it another item whenever we get one etc.

But this can easily be achieved by pulling the sampler whenever the input emits, possible passing along the data as well.

This simple (andfully backwards-compatible) change would open some cool possibilities.
For example, you could can pull another item after each emission, but first waiting a certain amount of time:

pipe(
  range(1, 100),
  pullWhen(pipe(
    fromIter(function* () {       // some infinite pullable
      while (true) yield 0;
    }),
    switchMap(_ => timer(1000)),
    startWith(0)                  // start immediately
  )),
  observe(x => console.log(x))    // logs 1, 2 ... with pauses of 1 sec in between
)

Another possibility would be to wait for n emissions before pulling n more items.

If you want, I can create a pull request for this.

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.