Reputation: 72
I'm new with NgRx.
I made some code, but the result is not exactly what intended.
I would like to intercept every action sent with the dispatch()
function to the store.
My goals are the following:
I have 2 problems in my code:
ofType()
with this array. If a new action is implemented, I have to manually update this array. Is there a way to retrieve the registered actions directly from the store ?Below is the code:
app.module.ts
//to register the Effect to intercept the actions
imports: [
EffectsModule.forRoot([InterceptorEffects]),
]
app.component.ts
//here I react to the changes of the store
_interceptorSubscription: Subscription;
this._interceptorSubscription = _store.pipe(select((state, props) => console.log("Got state from store.")) );
interceptor.effect.ts
//the Effect that should intercept every action
@Injectable({ providedIn: "root" })
export class InterceptorEffects {
private readonly _interceptAllAction$ = createEffect(() =>
this._actions.pipe(
ofType(...[JobActions.downloadResultAction, JobActions.deleteJobAction]),
tap(() => console.log("Intercepting action..."))
)
, { dispatch: false } );
constructor(private _actions: Actions) {}
}
Thank you for any advice.
Upvotes: 0
Views: 586
Reputation: 15505
May I ask why you need this? To answer the question:
AFAIK you can't do this. An action first reaches all reducers, and then it's handled by effects. If you want to register actions BEFORE they reach a reducer, take a look at meta reducers.
To react to all actions in an effect, don't use the ofType
operator, and simply subscribe to the Actions stream.
Upvotes: 1