1.0.3 • Published 6 years ago

callbag-pausable v1.0.3

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

⏯️ callbag-pausable

NPM Version NPM Downloads Build Status codecov.io

Callbag operator that allows data and talkbacks to pass through it freely until it receives a data or talkback message of PAUSE, in which case it stops the downflow of data until it receives another data or talkback message of RESUME.

Think of it like a valve on a pipe.

Usage

import interval from 'callbag-interval'
import observe from 'callbag-observe'
import pipe from 'callbag-pipe'
import pausable, { PAUSE, RESUME } from 'callbag-pausable'

const source = pipe(interval(100), pausable)

setTimeout(() => {
  console.log('PAUSING')
  source(1, PAUSE)
}, 400)
setTimeout(() => {
  console.log('RESUMING')
  source(1, RESUME)
}, 1000)

observe(console.log)(source) // 0
                             // 1
                             // 2
                             // PAUSING
                             // RESUMING
                             // 9
                             // 10
                             // 11
                             // ...

Or, as a talkback to an existing callbag (assuming callbag$ has pausable somewhere in its pipe):

const sendTalkbackValue = (callbag$, value) => {
  callbag$(0, (type, talkback) => {
    if (type === 0) {
      talkback(1, value) // send value
      talkback(2)        // terminate
    }
  })
}
const pause = callbag$ => sendTalkbackValue(callbag$, PAUSE)
const resume = callbag$ => sendTalkbackValue(callbag$, RESUME)
1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago