Skip to content

Instantly share code, notes, and snippets.

@simontreny
Last active October 18, 2019 13:12
Show Gist options
  • Save simontreny/100a509e5a19e6639e206812fa24e6e8 to your computer and use it in GitHub Desktop.
Save simontreny/100a509e5a19e6639e206812fa24e6e8 to your computer and use it in GitHub Desktop.
import { Observable } from "./observable";
export interface Todo {
readonly text: string;
readonly completed: boolean;
}
export enum VisibilityFilter {
SHOW_ALL,
SHOW_COMPLETED,
SHOW_ACTIVE,
}
export class TodoService {
readonly todos = new Observable<Todo[]>([]);
readonly visibilityFilter = new Observable(VisibilityFilter.SHOW_ALL);
addTodo(text: string) {
this.todos.set([...this.todos.get(), { text, completed: false }]);
}
toggleTodo(index: number) {
this.todos.set(this.todos.get().map(
(todo, i) => (i === index ? { text: todo.text, completed: !todo.completed } : todo)
));
}
setVisibilityFilter(filter: VisibilityFilter) {
this.visibilityFilter.set(filter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment