Create a writable store inside a factory function. Each call creates a separate store.
Our methods update the store without exposing set or update to components.
Return an object with subscribe and our methods. The writable helper supplies the store's subscription behavior for us.
import { writable } from 'svelte/store';
export function createTodos() {
const { subscribe, update } = writable([]);
function addTodo(todo) {
update((items) => [...items, todo]);
}
function checkTodo(index, completed) {
update((items) => items.map((todo, i) =>
i === index ? { ...todo, completed } : todo,
));
}
return { subscribe, addTodo, checkTodo };
}