rxjs-autorun
Version:
Autorun expressions with RxJS Observables
60 lines (59 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.computed = exports.combined = exports.autorun = exports._ = exports.$ = void 0;
var operators_1 = require("rxjs/operators");
var core_1 = require("./core");
/**
* Function to track Observable inside rxjs-autorun expressions
*
* Also provides `.weak`, `.normal` (default), and `.strong` types of tracking
*/
exports.$ = core_1.forwardTracker('$');
/**
* Function to read latest Observable value (w/o tracking it) inside rxjs-autorun expressions
*
* Also provides `.weak`, `.normal` (default), and `.strong` types of tracking
*/
exports._ = core_1.forwardTracker('_');
/**
* Automatically run `fn` when tracked inner Observables emit
*
* ```js
* autorun(() => _(a) + $(b))
* ```
*
* @param fn Function that uses tracked (`$`) or untracked (`_`) Observables
* @returns RxJS Subscription of distinct execution results
*/
function autorun(fn) {
return combined(fn).subscribe();
}
exports.autorun = autorun;
/**
* Automatically run `fn` when tracked inner Observables emit
*
* ```js
* combined(() => _(a) + $(b))
* ```
*
* @param fn Function that uses tracked (`$`) or untracked (`_`) Observables
* @returns Observable of execution results
*/
function combined(fn) {
return core_1.runner(fn);
}
exports.combined = combined;
/**
* Automatically run `fn` when tracked inner Observables emit a **distinct value**
*
* ```js
* computed(() => _(a) + $(b))
* ```
*
* @param fn Function that uses tracked (`$`) or untracked (`_`) Observables
* @returns Observable of distinct execution results
*/
function computed(fn) {
return core_1.runner(fn, true).pipe(operators_1.distinctUntilChanged());
}
exports.computed = computed;