Search code examples
angularngrx

NGRX Effect trigger for all actions but some


I created an effect that controls the session time. After the time the Logout action is called. On my app there are some poolings to update counters that I want to ignore. How should I do?

The application is too big and listing all actions is not an option.

    @Injectable()
    export class ApplicationEffects {
    
        APPLICATION_TIMEOUT_TIME = 1000 * 5;
    
        @Effect()
        extendApplicationTimeout$ = createEffect(() => this.actions$
            .switchMap( ( action: Action ) => Observable.timer(this.APPLICATION_TIMEOUT_TIME) )
            .map(() => new ApplicationActions.LogOut()));
    
        constructor( private actions$: Actions ) {}
    }

Solution

  • Haven’t tried this, but looked at the code for ofType. You can probably create your own operator based on that. Something like this:

    export function ofAnyTypeExcept(...disAllowedTypes: Array<string | ActionCreator<string, Creator>>): OperatorFunction<Action, Action> {
    return filter((action: Action) =>
    disAllowedTypes.every((typeOrActionCreator) => {
      if (typeof typeOrActionCreator === 'string') {
        // Comparing the string to type
        return typeOrActionCreator !== action.type;
      }
    
      // We are filtering by ActionCreator
      return typeOrActionCreator.type !== action.type;
     })
    );