events.ts
This file
Path
apps/web/src/client/events.ts
61 lines · 1.6 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162// Over JSON the platform replays the kept events in one response and ends; only the framed `/rpc` keeps the stream open.import { ProjectEvent } from "@repo/protocol/domain/event";import type { LandingView } from "@repo/protocol/domain/landing";import type { ProjectName } from "@repo/protocol/ids";import { Effect, Stream } from "effect";import { Atom } from "effect/reactivity";
import { ContractsClient } from "../surfaces.ts";import { keys } from "./keys.ts";
export { askStates, familyMarks, lastSeq, stampsFrom, whoseMove,} from "../features/shared/ask-progress.ts";
export type { AskProgress, Family, Mark, Move, Round,} from "../features/shared/ask-progress.ts";
const RECENT = 20;
export const projectEventsAtom = Atom.family((project: ProjectName) => Atom.withServerValueInitial( ContractsClient.runtime.factory.withReactivity(keys.events(project))( ContractsClient.runtime.atom( Effect.gen(function* () { const client = yield* ContractsClient;
const events: readonly ProjectEvent[] = yield* Stream.runCollect( client("watch", { project, since: 0 }) );
return events; }) ) ) ));
export const recentLandings = ( events: readonly ProjectEvent[]): readonly LandingView[] => events .flatMap((event) => ProjectEvent.guards.Landed(event) ? [event.landing] : [] ) .toReversed() .slice(0, RECENT);
export const recentLandingsAtom = Atom.family((project: ProjectName) => Atom.withServerValueInitial( Atom.mapResult(projectEventsAtom(project), recentLandings) ));