The core of RxMetics is basically the E() function (short for Expression),
which turns any given function to a similar function that accepts observables as well,
and returns an observable:
1import { E } from 'rxmetics';
2import { interval } from 'rxjs';
3
4
5const f = (x, y) => `2 * ${x} + ${y} ^ 2 = ${2 * x + y * y}`;
6const f$ = E(f);
7
8f$(interval(700), interval(500)).subscribe(console.log);
9
10// Result:
11// > 2 * 0 + 0 ^ 2 = 0
12// > 2 * 0 + 1 ^ 2 = 1
13// > 2 * 1 + 1 ^ 2 = 3
14// > 2 * 1 + 2 ^ 2 = 6
15// > 2 * 1 + 3 ^ 2 = 11
16// > 2 * 2 + 3 ^ 2 = 13
17// > ...
For any function f(), you can always provide non-observable values to E(f)()
as parameters as well. However, the return type will always be an observable:
1import { E } from 'rxmetics';
2import { interval } from 'rxjs';
3
4const f = (x, y) => 2 * x + y * y;
5
6E(f)(1, 2).subscribe(console.log); // --> result is 6
7E(f)(1, interval(100))... // --> this is acceptable
8E(f)(interval(100), 5)... // --> this is also acceptable
9E(f)(interval(100), interval(200)) // --> this is also acceptable
For any function f(), the observable returned by E(f) will start emitting
values after all of its observable arguments have at least emitted once. Afterwards,
it will re-calculate the value and emit the result for each new emission of its
observable arguments:
1import { E } from 'rxmetics';
2import { Subject } from 'rxjs';
3
4
5const f$ = E((x, y, z) => (2 * x + y * y) / z);
6const x$ = new Subject();
7const y$ = new Subject();
8
9f$(x$, y$, 5).subscribe(console.log);
10x$.next(5); // --> no logs
11y$.next(6); // --> 9.2
12x$.next(4); // --> 8.8