UNPKG

@code-workers.io/ngx-effect

Version:

NgxEffect let you easily handle your RxJs subscriptions

48 lines (44 loc) 1.74 kB
import { __classPrivateFieldGet } from 'tslib'; import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import { Subscription, tap, catchError, EMPTY } from 'rxjs'; var _NgxEffect_sub; /** * Keeps track of your subscriptions and unsubscribes them automatically when destroyed. * * @example */ class NgxEffect { constructor(e) { this.e = e; _NgxEffect_sub.set(this, new Subscription()); } register(obsOrSub$, operation) { if (obsOrSub$ instanceof Subscription) { __classPrivateFieldGet(this, _NgxEffect_sub, "f").add(obsOrSub$); return; } __classPrivateFieldGet(this, _NgxEffect_sub, "f").add(obsOrSub$ .pipe( // execute operation/ side effect tap(operation), catchError((err) => { this.e.handleError(err); return EMPTY; })) .subscribe()); } ngOnDestroy() { __classPrivateFieldGet(this, _NgxEffect_sub, "f").unsubscribe(); } } _NgxEffect_sub = new WeakMap(); NgxEffect.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: NgxEffect, deps: [{ token: i0.ErrorHandler }], target: i0.ɵɵFactoryTarget.Injectable }); NgxEffect.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: NgxEffect }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: NgxEffect, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: i0.ErrorHandler }]; } }); /** * Generated bundle index. Do not edit. */ export { NgxEffect }; //# sourceMappingURL=code-workers.io-ngx-effect.mjs.map