I developed a custom operator called waitFor
that is being used in my effects like this:
public effect$: Observable<Action> = createEffect(() => {
return this.actions$.pipe(
ofType(myAction),
waitFor<ReturnType<typeof myAction>>([anotherAction]),
...etc
);
});
This operator basically waits for specific actions to be dispatched before continuing execution based on correlationId's. However, the main focus here is different.
Although ofType
takes the source observable and uses it as the return type, I'm having difficulty achieving the same behavior in my implementation. As shown above, I am using
ReturnType<typeof myAction>>
and the following in my waitFor
method:
export function waitFor<A extends Action>(actionsToWaitFor$: Array<Actions>): OperatorFunction<A, A> {
Currently, when I call waitFor
like this:
public effect$: Observable<Action> = createEffect(() => {
return this.actions$.pipe(
ofType(myAction),
waitFor([anotherAction]),
...etc
);
});
The inferred type is Action
, but I want it to default to
ReturnType<typeof theSourceObservable>
. To achieve this, I believe I would need something like this in my waitFor
method:
export function waitFor<A extends ReturnType<typeof sourceObservable?!>>(actionsToWaitFor$: Array<Actions>): OperatorFunction<A, A> {
This is how the waitFor
function is implemented:
export function waitFor<A extends Action>(actionsToWaitFor$: Array<Actions>): OperatorFunction<A, A> {
return (source$) => {
return source$.pipe(
switchMap((action: A & { correlationId: string}) => {
// Use zip() to wait for all actions
// Omit map((action) => action)
// So the original action is always returned
})
);
};
}
According to the ofType
source code, it seems like I need to incorporate Extract
.
Update
An example of StackBlitz can be found here