# callbag-expr

> Expressions with callbags

Latest version **0.2.0** (published 2020-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install callbag-expr
pnpm add callbag-expr
yarn add callbag-expr
bun add callbag-expr
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-11-04 |
| First published | 2020-10-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Eugene Ghanizadeh Khoub |
| Maintainers | lorean.victor |
| Keywords | callbag, expression, reactive, programming |

## Links

- npm: https://www.npmjs.com/package/callbag-expr
- Repository: https://github.com/loreanvictor/callbag-expr
- Homepage: https://github.com/loreanvictor/callbag-expr#readme
- Issues: https://github.com/loreanvictor/callbag-expr/issues
- npm.io page: https://npm.io/package/callbag-expr

## Dependencies (1)

- [callbag](https://npm.io/package/callbag.md) ^1.3.0

## Recent versions

- 0.2.0 (latest) — 2020-11-04
- 0.1.5 — 2020-10-23
- 0.1.4 — 2020-10-21
- 0.1.3 — 2020-10-20
- 0.1.2 — 2020-10-19
- 0.1.1 — 2020-10-18
- 0.1.0 — 2020-10-17
- 0.0.4 — 2020-10-17
- 0.0.3 — 2020-10-16
- 0.0.2 — 2020-10-16
- 0.0.1 — 2020-10-16

## README

# callbag-expr

[![tests](https://img.shields.io/github/workflow/status/loreanvictor/callbag-expr/Test%20and%20Report%20Coverage?label=tests&logo=mocha&logoColor=green&style=flat-square)](https://github.com/loreanvictor/callbag-expr/actions?query=workflow%3A%22Test+and+Report+Coverage%22)
[![coverage](https://img.shields.io/codecov/c/github/loreanvictor/callbag-expr?logo=codecov&style=flat-square)](https://codecov.io/gh/loreanvictor/callbag-expr)
[![version](https://img.shields.io/npm/v/callbag-expr?logo=npm&style=flat-square)](https://www.npmjs.com/package/callbag-expr)

Create expressions based on callbags.

```bash
npm i callbag-expr
```

<br>

👉 Expressions with callbags:

```ts
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!](https://stackblitz.com/edit/callbag-expr-demo?devtoolsheight=33&embed=1&file=index.ts)

<br>

👉 Conditional expressions:

```ts
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!](https://stackblitz.com/edit/callbag-expr-demo2?devtoolsheight=33&embed=1&file=index.ts)

<br>

👉 Passive tracking, i.e. using latest value from callbag without re-evaluating when it emits:

```ts
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!](https://stackblitz.com/edit/callbag-expr-demo3?devtoolsheight=33&embed=1&file=index.ts)

<br>

## Gotchas

⚠️⚠️ Don't create new callbags in the expression:

```ts
// WRONG:
expr($ => $(interval(1000)));

// CORRECT:
const i = interval(1000);
expr($ => $(i));
```

<br>

⚠️⚠️ 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).
```ts
// WRONG:
const a = interval(1000);
expr($ => $(a));         // --> initially will be `undefined`
```
```ts
// CORRECT:
const a = interval(1000);
expr($ => $(a, 0));     // --> initially will be 0
```
```ts
// CORRECT:
const a = makeBehaviorSubject(42);
expr($ => $(a));        // --> initially will be 42
```

<br>

⚠️⚠️ Make sure some callbag is actively tracked:

```ts
// 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);
```

<br>

## Contribution

Play nice and respectful. Useful commands for development:

```bash
git clone https://github.com/loreanvictor/callbag-expr.git
```
```bash
npm i               # --> install dependencies
```
```bash
npm start           # --> run samples/index.ts
```
```bash
npm test            # --> run tests
```
```bash
npm run cov:view    # --> view coverage
```

<br><br>

_Inspired by [RxJS Autorun](https://github.com/kosich/rxjs-autorun)._

---
_Source: https://npm.io/package/callbag-expr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
