askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
107 lines • 3.37 kB
JavaScript
/**
* Example: Using the Store Factory Pattern
*
* This demonstrates the modern, ergonomic approach to managing external state
* with createStore.
*/
import { createStore } from "../src/core/store.js";
import { createPrompt } from "../src/core/registry.js";
// Create a typed store (one line!)
export const taskStore = createStore({
tasks: [],
activeTaskId: null,
});
// Update anywhere - notifications are automatic!
export function addTask(task) {
taskStore.update((state) => {
state.tasks.push(task);
});
// No manual notify call needed! ✨
}
export function setActiveTask(taskId) {
taskStore.update((state) => {
state.activeTaskId = taskId;
});
}
export function updateTaskStatus(taskId, status) {
taskStore.update((state) => {
const task = state.tasks.find((t) => t.id === taskId);
if (task) {
task.status = status;
}
});
}
export function clearTasks() {
taskStore.reset();
}
// Use in components - clean and simple
export const TasksDisplay = createPrompt({
type: "tasksDisplay",
component: ({ node, options, events }) => {
// Automatically subscribes and re-renders on changes!
const { tasks, activeTaskId } = taskStore.use();
return null; // Simplified for example
},
});
export const taskListStore = createStore({
lists: new Map(),
activeListId: null,
});
export const taskDataStore = createStore({
tasks: new Map(),
});
// Cross-store operations
export function deleteTaskList(listId) {
const list = taskListStore.get().lists.get(listId);
if (!list)
return;
// Update multiple stores in one operation
taskListStore.update((state) => {
state.lists.delete(listId);
if (state.activeListId === listId) {
state.activeListId = null;
}
});
taskDataStore.update((state) => {
// Remove all tasks in this list
for (const taskId of list.taskIds) {
state.tasks.delete(taskId);
}
});
}
// Example 3: Direct State Access (no subscription)
// =================================================
export function getTaskById(taskId) {
// Use .get() for direct access without subscribing
return taskStore.get().tasks.find((t) => t.id === taskId);
}
export function getAllActiveTasks() {
const { tasks } = taskStore.get();
return tasks.filter((t) => t.status === "running");
}
// Example 4: Manual Subscription (advanced)
// ==========================================
export function setupTaskLogger() {
// Subscribe manually to log all changes
const unsubscribe = taskStore.subscribe(() => {
const state = taskStore.get();
console.log("Tasks changed:", {
count: state.tasks.length,
active: state.activeTaskId,
});
});
// Return cleanup function
return unsubscribe;
}
console.log("Store factory example loaded successfully!");
console.log("Available functions:", {
addTask: typeof addTask,
setActiveTask: typeof setActiveTask,
updateTaskStatus: typeof updateTaskStatus,
clearTasks: typeof clearTasks,
deleteTaskList: typeof deleteTaskList,
getTaskById: typeof getTaskById,
getAllActiveTasks: typeof getAllActiveTasks,
setupTaskLogger: typeof setupTaskLogger,
});
//# sourceMappingURL=store-factory-example.js.map