Planning event-sourced task manager
The plan is to implement a single-file HTML/JS app that stores only an append-only event log in localStorage and derives all UI state by replaying those events in memory on startup to demonstrate event sourcing. Next, I'll build the UI, event handling, and replay logic accordingly.
Below is a minimal, self‑contained event‑sourced task manager implemented in plain HTML/JS. It never persists “current state”; it only appends immutable events to localStorage. On startup, it replays the event log in memory to reconstruct the UI.
Save this as index.html and open it in a browser:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Event-Sourced Task Manager</title>
<style>
body { font-family: system-ui, sans-serif; background:#f5f5f5; margin:0; padding:20px; }
h1 { margin:0 0 1rem; }
#app { max-width: 720px; margin: 0 auto; background:#fff; border-radius: 8px; padding: 16px; box-shadow: 0 4px 15px rgba(0,0,0,0.08); }
.row { display: flex; gap:8px; margin-bottom: 12px; }
input[type="text"] { flex:1; padding:8px; font-size: 1rem; }
button { padding: 8px 12px; font-size: 0.95rem; cursor: pointer; }
ul { list-style: none; padding: 0; }
li { display: flex; align-items: center; gap: 8px; padding:8px; border-bottom:1px solid #eee; }
li:last-child { border-bottom:none; }
.task-title { flex: 1; }
.done { text-decoration: line-through; color: #888; }
.pill { padding:2px 6px; border-radius: 4px; font-size: 0.75rem; color:#444; background:#eef; }
details { margin-top: 20px; }
pre { white-space: pre-wrap; word-break: break-word; background:#f0f0f0; padding:12px; border-radius:4px; }
.muted { color:#777; font-size: 0.9rem; }
</style>
</head>
<body>
<div id="app">
<h1>Event-Sourced Task Manager</h1>
<div class="row">
<input id="taskInput" type="text" placeholder="What needs to be done?">
<button id="addBtn">Add Task</button>
</div>
<ul id="taskList"></ul>
<details>
<summary>Show Event Log</summary>
<div class="muted">Only events are persisted. The task list is rebuilt on startup by replaying these.</div>
<pre id="eventLogPre"></pre>
</details>
</div>
<script>
// ---- Event store & replay ----
const STORAGE_KEY = 'taskEvents';
const eventLog = loadEvents(); // persisted immutable log
let tasks = replay(eventLog); // derived state in memory
function loadEvents() {
const raw = localStorage.getItem(STORAGE_KEY);
try { return raw ? JSON.parse(raw) : []; }
catch { return []; }
}
function persistEvents() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(eventLog));
renderEventLog();
}
function appendEvent(evt) {
eventLog.push(evt);
persistEvents();
applyEvent(tasks, evt); // also update in-memory state incrementally
render();
}
// ---- Aggregate logic: apply events to state ----
function replay(events) {
const state = new Map(); // id -> task
events.forEach(e => applyEvent(state, e));
return state;
}
function applyEvent(state, event) {
switch (event.type) {
case 'TaskCreated': {
state.set(event.id, { id: event.id, title: event.title, completed: false });
break;
}
case 'TaskRenamed': {
const t = state.get(event.id);
if (t) t.title = event.title;
break;
}
case 'TaskCompleted': {
const t = state.get(event.id);
if (t) t.completed = true;
break;
}
case 'TaskReopened': {
const t = state.get(event.id);
if (t) t.completed = false;
break;
}
case 'TaskDeleted': {
state.delete(event.id);
break;
}
}
}
// ---- UI actions -> events ----
const inputEl = document.getElementById('taskInput');
document.getElementById('addBtn').onclick = () => {
const title = inputEl.value.trim();
if (!title) return;
appendEvent({ type: 'TaskCreated', id: crypto.randomUUID(), title, ts: Date.now() });
inputEl.value = '';
inputEl.focus();
};
function toggleTask(id, checked) {
appendEvent({ type: checked ? 'TaskCompleted' : 'TaskReopened', id, ts: Date.now() });
}
function deleteTask(id) {
appendEvent({ type: 'TaskDeleted', id, ts: Date.now() });
}
function renameTask(id, newTitle) {
appendEvent({ type: 'TaskRenamed', id, title: newTitle, ts: Date.now() });
}
// ---- Rendering ----
function render() {
const listEl = document.getElementById('taskList');
listEl.innerHTML = '';
// sort by creation time in log order (optional)
const orderedIds = Array.from(tasks.values()).sort((a,b)=>0).map(t => t.id);
orderedIds.forEach(id => {
const t = tasks.get(id);
const li = document.createElement('li');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = t.completed;
checkbox.onchange = () => toggleTask(t.id, checkbox.checked);
const titleSpan = document.createElement('span');
titleSpan.className = 'task-title' + (t.completed ? ' done' : '');
titleSpan.contentEditable = true;
titleSpan.textContent = t.title;
titleSpan.onblur = () => {
const newTitle = titleSpan.textContent.trim();
if (newTitle && newTitle !== t.title) {
renameTask(t.id, newTitle);
} else {
// revert visual text if unchanged or empty
titleSpan.textContent = t.title;
}
};
const status = document.createElement('span');
status.className = 'pill';
status.textContent = t.completed ? 'Done' : 'Open';
const delBtn = document.createElement('button');
delBtn.textContent = 'Delete';
delBtn.onclick = () => deleteTask(t.id);
li.append(checkbox, titleSpan, status, delBtn);
listEl.appendChild(li);
});
renderEventLog();
}
function renderEventLog() {
document.getElementById('eventLogPre').textContent = JSON.stringify(eventLog, null, 2);
}
// Initial render on page load
render();
</script>
</body>
</html>
How it works
- Events only are persisted:
TaskCreated, TaskRenamed, TaskCompleted, TaskReopened, TaskDeleted.
- On startup, the app reads the persisted event log from
localStorage and replays it to reconstruct the current tasks in memory.
- UI actions emit new events that get appended to the log; the log is then re‑persisted.
- The displayed tasks are always the derived state from the event stream.
You can inspect the event log by expanding the “Show Event Log” section. Clearing your browser’s localStorage will erase all data (since no current state is stored separately).