🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

callbag-expr

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callbag-expr

Expressions with callbags

0.2.0
latest
Source
npm
Version published
Weekly downloads
6
Maintainers
1
Weekly downloads
 
Created
Source

callbag-expr

tests coverage version

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

► TRY IT!


👉 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)
)

► TRY IT!


👉 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)
)

► TRY IT!


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.

Keywords

callbag

FAQs

Package last updated on 04 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts