Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/super-behaviour-subject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {BehaviorSubject} from 'rxjs';

export type SuperBehaviourSubjectNext<T> = T | ((previousValue: T) => T);

export class SuperBehaviourSubject<T> extends BehaviorSubject<T> {
next(value: SuperBehaviourSubjectNext<T>): void {
if (typeof value === 'function') {
// @ts-ignore
return super.next(value(this._value)); // _value of BehaviorSubject must became not so private
}
super.next(value);
}
}
36 changes: 16 additions & 20 deletions src/app/todo.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,31 @@ export class TodoFacade {

createTodo(description: string) {
const todo = { description: description, done: false };
const todos = this.todoState.getTodos();
this.todoState.setTodos(
[...todos, todo]
);
this.todoState.setTodos((todos) => [...todos, todo]);
}

deleteTodo(todo: Todo) {
const todos = this.todoState.getTodos();
this.todoState.setTodos(
[...todos.filter(x => x !== todo)]
);
this.todoState.setTodos((todos) => [...todos.filter(x => x !== todo)]);
}

toggleTodo(todo: Todo) {
const todos = this.todoState.getTodos();
todo = todos.find(x => x.description === todo.description);
todo.done = !todo.done;
this.todoState.setTodos(
[...todos]
this.todoState.setTodos((todos) => todos.map(todoItem => {
if (todoItem.description === todo.description) {
todoItem.done = !todoItem.done;
}
return todoItem;
})
);
}

toggleAllTodos() {
const todos = this.todoState.getTodos();
const allDone = todos.every(todo => todo.done);
todos.forEach(todo => todo.done = !allDone);
this.todoState.setTodos(
[...todos]
);
this.todoState.setTodos((todos) => {
const isAllDone = todos.every(todo => todo.done);
return todos.map(todo => {
todo.done = !isAllDone;
return todo;
});
});
}

deleteAllTodos() {
Expand All @@ -68,5 +64,5 @@ export class TodoFacade {

filterTodos(value: string) {
this.todoState.setFilter(value);
};
}
}
9 changes: 3 additions & 6 deletions src/app/todo.state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {BehaviorSubject, Observable} from "rxjs";
import {Injectable} from "@angular/core";
import {SuperBehaviourSubject, SuperBehaviourSubjectNext} from './super-behaviour-subject';

export interface Todo {
description: string;
Expand All @@ -21,7 +22,7 @@ const sampleTodos: Todo[] = [
@Injectable({"providedIn": "root"})
export class TodoState {

private readonly todos$ = new BehaviorSubject<Todo[]>(sampleTodos);
private readonly todos$ = new SuperBehaviourSubject<Todo[]>(sampleTodos);
private readonly filter$ = new BehaviorSubject<string>('');
private readonly updating$ = new BehaviorSubject<boolean>(false);

Expand All @@ -37,11 +38,7 @@ export class TodoState {
return this.updating$.asObservable();
}

getTodos(): Todo[] {
return this.todos$.getValue();
}

setTodos(todos: Todo[]): void {
setTodos(todos: SuperBehaviourSubjectNext<Todo[]>): void {
this.todos$.next(todos);
}

Expand Down