@datorama/akita-ng-effects
Version:
A Reactive State Management extension dealing with side effects.
58 lines • 2.03 kB
JavaScript
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { Actions } from './actions';
import * as i0 from "@angular/core";
import * as i1 from "./actions";
export class ModuleManager {
constructor(actions$) {
this.actions$ = actions$;
this.effectInstanceSources = new WeakSet();
this.destroyEffects$ = new Subject();
}
subscribeToEffects(effectInstance) {
for (let key in effectInstance) {
const property = effectInstance[key];
if (property.isEffect === true) {
property.pipe(takeUntil(this.destroyEffects$)).subscribe((actionOrSkip) => {
this.dispatchAction(property, actionOrSkip);
});
}
}
}
has(effect) {
return this.effectInstanceSources.has(effect);
}
add(effect) {
this.effectInstanceSources.add(effect);
}
dispatchAction(property, actionOrSkip) {
if (property.dispatchAction && this.checkAction(actionOrSkip)) {
this.actions$.dispatch(actionOrSkip);
}
}
checkAction(action) {
if (action.type) {
return true;
}
throw new TypeError('Make sure to provide a valid action type or set the option {dispatch: false}');
}
ngOnDestroy() {
// modules aren't supposed to be destroyed; might not be needed
this.destroyEffects$.next();
this.effectInstanceSources = new WeakSet();
}
}
ModuleManager.ctorParameters = () => [
{ type: Actions }
];
ModuleManager.ɵprov = i0.ɵɵdefineInjectable({ factory: function ModuleManager_Factory() { return new ModuleManager(i0.ɵɵinject(i1.Actions)); }, token: ModuleManager, providedIn: "root" });
ModuleManager.decorators = [
{ type: Injectable, args: [{
providedIn: 'root',
},] }
];
ModuleManager.ctorParameters = () => [
{ type: Actions }
];
//# sourceMappingURL=module-manager.service.js.map