Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created October 23, 2024 09:46
Show Gist options
  • Save Armenvardanyan95/7f26917991790c8bfe5c18ecac9dcba1 to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/7f26917991790c8bfe5c18ecac9dcba1 to your computer and use it in GitHub Desktop.
// bad actions lead to harder
// to understand reducers/effects
const loadTodos$ = createEffect(() => {
const actions = inject(Actions);
const todoService = inject(TodoService);
return actions.pipe(
// this sounds like a command
// it gives us no idea about when
// the todos will be loaded
ofType(TodoActions.loadTodos),
switchMap(() => todoService().getTodos().pipe(
map(todos => TodoActions.loadTodosSuccess(todos)),
)),
);
}, {functional: true});
const deleteTodo$ = createEffect(() => {
const actions = inject(Actions);
const todoService = inject(TodoService);
return actions.pipe(
ofType(TodoActions.deleteTodo),
switchMap(todoId => todoService().deleteTodo(todoId).pipe(
switchMap(todos => [
TodoActions.deleteTodoSuccess(todos),
// we are forced to also
// dispatch this other command
// to refetch todos after deleting one
// very tight coupling
TodoActions.loadTodos(),
]),
)),
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment