import { NgModule, Type } from '@angular/core'; import { Action, Dispatcher, Store } from '@ngrx/store'; import { Subject, Observable } from 'rxjs'; import 'rxjs/operator/withLatestFrom'; import 'rxjs/operator/switchMap'; export interface Interception { intercept(store: Store, action: Action): Observable; } export class Interceptor extends Subject { constructor( private _store: Store, private _dispatcher: Dispatcher, private _interceptor: Interception, ) { super(); this.withLatestFrom(_store) .switchMap( ([action, store]) => _interceptor.intercept(store, action) ) .subscribe(_dispatcher); } dispatch(action: Action) { this.next(action); } } export function _interceptorFactory(store: Store, dispatcher: Dispatcher, interception: Interception) { return new Interceptor(store, dispatcher, interception); } @NgModule() export class InterceptModule { static provideInterceptor(type: Type) { return { ngModule: InterceptModule, providers: [ type, { provide: Interceptor, useFactory: _interceptorFactory, deps: [ Store, Dispatcher, type ] } ] } } }