Rebuild PR #2013 on top of current origin/main without the duplicated commit lineage. Co-authored-by: codex <[email protected]>
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import * as Arr from "effect/Array";
|
|
import type { OrchestrationShellSnapshot, OrchestrationShellStreamEvent } from "@t3tools/contracts";
|
|
|
|
/**
|
|
* Apply a single shell stream event to an existing snapshot, returning a new
|
|
* snapshot with the event's changes applied. This is a pure reducer that both
|
|
* web and mobile can use to keep their local shell snapshot in sync.
|
|
*
|
|
* Returns the original snapshot reference unchanged if the event is not
|
|
* recognized (forward-compatible).
|
|
*/
|
|
export function applyShellStreamEvent(
|
|
snapshot: OrchestrationShellSnapshot,
|
|
event: OrchestrationShellStreamEvent,
|
|
): OrchestrationShellSnapshot {
|
|
switch (event.kind) {
|
|
case "project-upserted": {
|
|
const projects = snapshot.projects.some((p) => p.id === event.project.id)
|
|
? Arr.map(snapshot.projects, (p) => (p.id === event.project.id ? event.project : p))
|
|
: Arr.append(snapshot.projects, event.project);
|
|
return { ...snapshot, projects, snapshotSequence: event.sequence };
|
|
}
|
|
case "project-removed":
|
|
return {
|
|
...snapshot,
|
|
projects: Arr.filter(snapshot.projects, (p) => p.id !== event.projectId),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
case "thread-upserted": {
|
|
const threads = snapshot.threads.some((t) => t.id === event.thread.id)
|
|
? Arr.map(snapshot.threads, (t) => (t.id === event.thread.id ? event.thread : t))
|
|
: Arr.append(snapshot.threads, event.thread);
|
|
return { ...snapshot, threads, snapshotSequence: event.sequence };
|
|
}
|
|
case "thread-removed":
|
|
return {
|
|
...snapshot,
|
|
threads: Arr.filter(snapshot.threads, (t) => t.id !== event.threadId),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
default:
|
|
return snapshot;
|
|
}
|
|
}
|