skip to content

rxjs-autorun

 

📖 About

Automatically re-evaluate an expression whenever Observable in it emits

🔗 Links:
See the homepage: github.com/kosich/rxjs-autorun
Read an intro article: "RxJS Autorun Intro"

📦 Install:

npm i rxjs-autorun

🛸 Examples

Simple mapping

computed will run the () => a + '!' expression whenever a emits a distinctive value:

1
2
3
4
5
6
7
8
9
10
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { computed, $ } = require('rxjs-autorun');

const a = timer(100); // 0 in 100ms
const c = computed(() => $(a) + '!️');

a.subscribe(rxObserver('a'));
c.subscribe(rxObserver('a + !'));
0msastartcomplete00 a + !startcomplete0!️0!️

Combine two streams

computed will run the () => a + b expression whenever a or b emit a distinctive value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { rxObserver } = require('api/v0.3');
const { timer, of } = require('rxjs');
const { map } = require('rxjs/operators');
const { computed, $, _ } = require('rxjs-autorun');


const a = timer(0, 100); // 0, 1, 2, …
const b = timer(0, 330).pipe(map(i => i % 2 ? '🐇' : '🦔' )); // 🦔, 🐇, 🦔, …
const c = computed(() => $(a) + $(b));

a.subscribe(rxObserver('a'));
b.subscribe(rxObserver('b'));
c.subscribe(rxObserver('a + b'));
⚠️ Execution time is limited to 1000ms
0msastart00 11 22 33 44 55 66 77 88 99 1010 bstart🦔🦔 🐇🐇 🦔🦔 🐇🐇 a + bstart0🦔0🦔 1🦔1🦔 2🦔2🦔 3🦔3🦔 3🐇3🐇 4🐇4🐇 5🐇5🐇 6🐇6🐇 6🦔6🦔 7🦔7🦔 8🦔8🦔 9🦔9🦔 9🐇9🐇 10🐇10🐇

Combine a with latest b

computed will run the () => a + b expression whenever a emits a distinctive value, with latest value from b:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { rxObserver } = require('api/v0.3');
const { timer, of } = require('rxjs');
const { map } = require('rxjs/operators');
const { computed, $, _ } = require('rxjs-autorun');


const a = timer(0, 100); // 0, 1, 2, …
const b = timer(0, 330).pipe(map(i => i % 2 ? '🐇' : '🦔' )); // 🦔, 🐇, 🦔, …
const c = computed(() => $(a) + _(b));

a.subscribe(rxObserver('a'));
b.subscribe(rxObserver('b'));
c.subscribe(rxObserver('a + latest b'));
⚠️ Execution time is limited to 1000ms
0msastart00 11 22 33 44 55 66 77 88 99 1010 bstart🦔🦔 🐇🐇 🦔🦔 🐇🐇 a + latest bstart0🦔0🦔 1🦔1🦔 2🦔2🦔 3🦔3🦔 4🐇4🐇 5🐇5🐇 6🐇6🐇 7🦔7🦔 8🦔8🦔 9🦔9🦔 10🐇10🐇

🛠 Check out the docs for more: github.com/kosich/rxjs-autorun

This is one of my libs that I'd like to share with you