-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.ts
37 lines (28 loc) · 1.03 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Signal, signal, effect, computed } from "@preact/signals";
export type AddTodoFunction = (e: Event) => void;
export type RemoveTodoFunction = (index: number) => void;
export type AppStateType = {
todos: Signal<string[]>;
currentTodo: Signal<string>;
todoCount: Signal<number>;
addTodo: AddTodoFunction;
removeTodo: RemoveTodoFunction;
// dispose: () => void;
};
function createAppState(): AppStateType {
const todos = signal<string[]>([]);
const currentTodo = signal<string>("");
const todoCount = computed(() => todos.value.length);
const addTodo: AddTodoFunction = (e: Event): void => {
e.preventDefault();
todos.value = [...todos.value, currentTodo.value];
currentTodo.value = "";
};
const removeTodo: RemoveTodoFunction = (index: number): void => {
todos.value = todos.value.filter((_todo: unknown, i: number) =>
i !== index
); // todoCount.value = todoCount.value - 1;
};
return { todos, currentTodo, addTodo, removeTodo, todoCount };
}
export default createAppState();