Reputation: 359
I find the following pattern usefull and versatile:
effectXY$ = createEffect(() =>
this.actions$.pipe(
ofType(actionX, actionY),
switchMap(() =>
this.myApi.something()
.pipe(
map(() => actionZ())
)
)
)
);
effectXZ$ = createEffect(() =>
this.actions$.pipe(
ofType(
actionX,
actionZ
),
pairwise(),
// wait for both actions to be dispatched in the correct order
filter( ([prevAction, currentAction]) =>
prevAction.type === actionX.type &&
currentAction.type === actionZ.type
),
map(() => actionA())
)
);
What is happening is that actionA
is dispatchen only if actionX
and actionZ
have been dispatched in that specific order. Doing this i also avoid to create lots of other actions to mimic this pattern, but i miss whatever implications this might lead to
edit: reminder that there is no guarantee that the actions are consecutive (with no other actions being dispatched in between)
Upvotes: 1
Views: 275
Reputation: 3677
There is no obvious anti-pattern here. You're doing everything by the book.
You use your Effect to observe the actions$
stream, using RxJs operators you pick from it a specific kind of event that matches your criteria, and return an Action when that event happens.
Without more context as to what you're actually trying to accomplish in your app with this code, I don't see grounds to criticize it from the raw theory standpoint.
Upvotes: 1