22 lines
615 B
TypeScript
22 lines
615 B
TypeScript
import { mapResponse } from '@ngrx/operators';
|
|
import { EventInstance } from '@ngrx/signals/events';
|
|
import { Observable } from 'rxjs';
|
|
import { describeError } from '../notifications';
|
|
|
|
type Creator = (message: string) => EventInstance<string, string>;
|
|
|
|
/** Runs a command and turns its outcome into a succeeded/failed event. */
|
|
export function runCommand<T>(
|
|
command$: Observable<T>,
|
|
successMessage: string,
|
|
succeeded: Creator,
|
|
failed: Creator,
|
|
) {
|
|
return command$.pipe(
|
|
mapResponse({
|
|
next: () => succeeded(successMessage),
|
|
error: (error) => failed(describeError(error)),
|
|
}),
|
|
);
|
|
}
|