Open History to see the last commit
1export interface Todo {
2 id: string
3 text: string
4 completed: boolean
5 createdAt: Date
6 updatedAt: Date
7}
8
9export interface TodoInput {
10 text: string
11 completed?: boolean
12}
13
14export interface TodoUpdate {
15 text?: string
16 completed?: boolean
17}
18
19// In-memory storage for the demo
20const todosStore = new Map<string, Todo>()
21
22// Helper function to generate IDs
23function generateId(): string {
24 return Math.random().toString(36).substring(2) + Date.now().toString(36)
25}
26
27export const todoService = {
28 getAll(): Array<Todo> {
29 return Array.from(todosStore.values()).sort(
30 (a, b) =>
31 new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
32 )
33 },
34
35 getById(id: string): Todo | undefined {
36 return todosStore.get(id)
37 },
38
39 create(input: TodoInput): Todo {
40 const now = new Date()
41 const todo: Todo = {
42 id: generateId(),
43 text: input.text,
44 completed: input.completed ?? false,
45 createdAt: now,
46 updatedAt: now,
47 }
48 todosStore.set(todo.id, todo)
49 return todo
50 },
51
52 update(id: string, updates: TodoUpdate): Todo | null {
53 const existing = todosStore.get(id)
54 if (!existing) {
55 return null
56 }
57
58 const updated: Todo = {
59 ...existing,
60 ...updates,
61 updatedAt: new Date(),
62 }
63 todosStore.set(id, updated)
64 return updated
65 },
66
67 delete(id: string): boolean {
68 return todosStore.delete(id)
69 },
70
71 clear(): void {
72 todosStore.clear()
73 },
74
75 // For demo purposes - simulate network delays
76 async withDelay<T>(fn: () => T, delay = 500): Promise<T> {
77 await new Promise((resolve) => setTimeout(resolve, delay))
78 return fn()
79 },
80
81 // For demo purposes - simulate random failures
82 simulateFailure(probability = 0.2): void {
83 if (Math.random() < probability) {
84 throw new Error(`Simulated network failure`)
85 }
86 },
87}
88
89// Add some initial data for demo
90todoService.create({ text: `Learn TanStack DB` })
91todoService.create({ text: `Build offline-first app` })
92todoService.create({ text: `Test multi-tab coordination`, completed: true })