import { Injectable } from '@angular/core' ; import { Actions , ofType } from '@ngrx/effects' ; import { Action , Store } from '@ngrx/store' ; import { Observable } from 'rxjs' ; import { first , map , pluck , timeout } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class ActionStatusResolverService { constructor ( private actions$ , private store ) { } resolve < T = Action > ( actionToDispatch , successType , failType , t = 30000 ) { setTimeout ( ( ) => { this . store . dispatch ( actionToDispatch ) ; } ) ; return this . actions$ . pipe ( ofType ( successType , failType ) , timeout ( t ) , first ( ) , map ( action => { if ( action . type === successType ) { return ( action as unknown ) as T ; } else { const a = action as any ; throw a . payload ? a . payload : a ; } } ) , pluck ( 'payload' ) ) ; } }