first commit
This commit is contained in:
21
libs/web/tracker/application/src/lib/stores/command.ts
Normal file
21
libs/web/tracker/application/src/lib/stores/command.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
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)),
|
||||
}),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user