Files
task-stream/libs/web/tracker/application/src/lib/stores/command.ts
2026-10-03 13:40:26 +02:00

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)),
}),
);
}