Open History to see the last commit
1import * as Arr from "effect/Array";
2import type { OrchestrationShellSnapshot, OrchestrationShellStreamEvent } from "@t3tools/contracts";
3
4/**
5 * Apply a single shell stream event to an existing snapshot, returning a new
6 * snapshot with the event's changes applied. This is a pure reducer that both
7 * web and mobile can use to keep their local shell snapshot in sync.
8 *
9 * Returns the original snapshot reference unchanged if the event is not
10 * recognized (forward-compatible).
11 */
12export function applyShellStreamEvent(
13 snapshot: OrchestrationShellSnapshot,
14 event: OrchestrationShellStreamEvent,
15): OrchestrationShellSnapshot {
16 switch (event.kind) {
17 case "project-upserted": {
18 const projects = snapshot.projects.some((p) => p.id === event.project.id)
19 ? Arr.map(snapshot.projects, (p) => (p.id === event.project.id ? event.project : p))
20 : Arr.append(snapshot.projects, event.project);
21 return { ...snapshot, projects, snapshotSequence: event.sequence };
22 }
23 case "project-removed":
24 return {
25 ...snapshot,
26 projects: Arr.filter(snapshot.projects, (p) => p.id !== event.projectId),
27 snapshotSequence: event.sequence,
28 };
29 case "thread-upserted": {
30 const threads = snapshot.threads.some((t) => t.id === event.thread.id)
31 ? Arr.map(snapshot.threads, (t) => (t.id === event.thread.id ? event.thread : t))
32 : Arr.append(snapshot.threads, event.thread);
33 return { ...snapshot, threads, snapshotSequence: event.sequence };
34 }
35 case "thread-removed":
36 return {
37 ...snapshot,
38 threads: Arr.filter(snapshot.threads, (t) => t.id !== event.threadId),
39 snapshotSequence: event.sequence,
40 };
41 default:
42 return snapshot;
43 }
44}