/callbag-pausable

⏯️ Callbag Pausable is a callbag that will convert any callbag stream into one that can be paused and resumed via data or talkback events.

Primary LanguageJavaScriptMIT LicenseMIT

⏯️ 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)