callbag-expr
v0.2.0
Published
Expressions with callbags
Downloads
3,122
Maintainers
Readme
callbag-expr
Create expressions based on callbags.
npm i callbag-expr
👉 Expressions with callbags:
import interval from 'callbag-interval'
import pipe from 'callbag-pipe'
import subscribe from 'callbag-subscribe'
import expr from 'callbag-expr'
const a = interval(1000)
pipe(
expr($ => 'Hellow: ' + $(a, 0)),
subscribe(console.log)
)
👉 Conditional expressions:
import interval from 'callbag-interval'
import pipe from 'callbag-pipe'
import subscribe from 'callbag-subscribe'
import expr from 'callbag-expr'
const a = interval(1000)
const b = interval(600)
const c = interval(3000)
pipe(
expr($ => {
if($(c, 0) % 2) return 'A = ' + $(a)
else return 'B = ' + $(b)
}),
subscribe(console.log)
)
👉 Passive tracking, i.e. using latest value from callbag without re-evaluating when it emits:
import interval from 'callbag-interval'
import pipe from 'callbag-pipe'
import subscribe from 'callbag-subscribe'
import expr from 'callbag-expr'
const a = interval(1000)
const b = interval(300)
pipe(
expr(($, _) => $(a) + _(b)),
subscribe(console.log)
)
Gotchas
⚠️⚠️ Don't create new callbags in the expression:
// WRONG:
expr($ => $(interval(1000)));
// CORRECT:
const i = interval(1000);
expr($ => $(i));
⚠️⚠️ Make sure your callbags have initial value, or provide initial value. If you don't, $(a)
might default to undefined
(so take that into account).
// WRONG:
const a = interval(1000);
expr($ => $(a)); // --> initially will be `undefined`
// CORRECT:
const a = interval(1000);
expr($ => $(a, 0)); // --> initially will be 0
// CORRECT:
const a = makeBehaviorSubject(42);
expr($ => $(a)); // --> initially will be 42
⚠️⚠️ Make sure some callbag is actively tracked:
// WRONG:
const a = interval(1000);
const b = interval(1000);
//
// --> a is not actively tracked, b is also not tracked initially,
// --> so the expression is never re-evaluated.
//
expr($ => _(a, 0) > 5 ? $(b) : 32);
Contribution
Play nice and respectful. Useful commands for development:
git clone https://github.com/loreanvictor/callbag-expr.git
npm i # --> install dependencies
npm start # --> run samples/index.ts
npm test # --> run tests
npm run cov:view # --> view coverage
Inspired by RxJS Autorun.