1.0.0 • Published 6 years ago

callbag-from-verbose v1.0.0

Weekly downloads
5
License
Unlicense
Repository
github
Last release
6 years ago

callbag-from-verbose

A more verbose way to create callbags

Concepts

3 actions

ACTIONVERBOSECALLBAG
Introduce A to BintroduceAtoB(A, B)B(START, A)
Send B a messagesend(B, message)B(DATA, message)
Stop Bstop(B)B(STOP)

3 events

ACTIONVERBOSECALLBAG
B meet AB = {meet: A => ...}B = (t, p) => {if(t === 0){...}}
B receives a messageB = {receive: message => ...}B = (t, p) => {if(t === 1){...}}
B is asked to stopB = {stop: () => ...}B = (t, p) => {if(t === 2){...}}

Simple example

Orginal code

Taken from the 'Callback Heaven' talk from Andre Staltz (video)https://www.youtube.com/watch?v=HssczgaY9BM

const producer = (type, payload) => {
  if (type === 0) {
    const other = payload
    let i = 0;
    const handle = setInterval(() => {
      other(1, i++);
    }, 1000);
    other(0, (t, p) => {
      if (t === 1) i = p
      if (t === 2) clearInterval(handle);
    })
  }
};


const consumer = (type, payload) => {
  if (type === 0) {
    const other = payload
    setTimeout(() => other(1, 17), 3500)
    setTimeout(() => other(2), 7500)
  }
  if (type === 1) {
    console.log(payload)
  }
}

producer(0, consumer)

Verbose code

const {
    introduceAtoB,
    send,
    stop,
    fromVerboseCallbag
} = require('callbag-from-verbose')

const verboseProducer = fromVerboseCallbag({
  meet: other => {
    let i = 0;
    const handle = setInterval(() => {
      send(other, i++)
    }, 1000);
    introduceAtoB(
      fromVerboseCallbag({
        receive: message => (i = message),
        stop: () => clearInterval(handle)
      }),
      other
    )
  }
})

const verboseConsumer = fromVerboseCallbag({
  meet: other => {
    setTimeout(() => send(other, 17), 3500)
    setTimeout(() => stop(other), 7500)
  },
  receive: message => console.log(message)
})

introduceAtoB(verboseConsumer, verboseProducer)
1.0.0

6 years ago