1.0.4 • Published 8 months ago
rw-indirect-store v1.0.4
rw-indirect-store
import {ref} from 'rw-indirect'
import {defineStore, getStores, clearStore, clearAllStores} from 'rw-indirect-store'
const useTodoStore = defineStore('todos', () => {
const todos = ref([]);
function addTodo(value) {
todos.value.push({
id: window.crypto.randomUUID(),
value,
completed: false
});
}
function removeTodo(id) {
const index = todos.value.findIndex(todo => todo.id === id);
if (index !== -1) {
todos.value.splice(index, 1);
}
}
return {
todos,
addTodo,
removeTodo
};
});
const todoStore = useTodoStore()
const state = todoStore.$state()
todoStore.addTodo('drive')
todoStore.$reset()
todoStore.$undo()
todoStore.$redo()
todoStore.$clearHistory()
todoStore.$patch({
todos: [...state.todos, ...]
})
todoStore.$patch(state => {
state.todos.push(...)
})
const unsubscribe = todoStore.$subscribe((state) => {
console.log('state:', state);
});
const unsubscribeTodos = todoStore.$subscribeKey('todos', (newValue, oldValue) => {
console.log('todos change:', newValue, oldValue);
});
const unsubscribeAction = todoStore.$onAction((action) => {
if (action.type === 'start') {
console.log(`start execution: ${action.name}`);
} else if (action.type === 'success') {
console.log(`${action.name} time consuming ${action.duration}ms`);
}
});
// getStores()
// clearStore(id)
// clearAllStores()