Skip to content
Myr

standing-posture.ts

This file

Path

packages/project/src/standing-posture.ts

120 lines · 3.5 KiB · TypeScript

Last changed by

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

History of this file

Take it

Merge build/1102 into mainoscabrielbefore adoption084383d

line 1why, one step back
1import type { ProjectListing } from "@repo/protocol/domain/front-page";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 2–4why, one step back
2import type { ProjectName } from "@repo/protocol/ids";
3import type { Posture } from "@repo/protocol/posture";4import { Array as Arr, Cause, Context, DateTime, Effect, Option } from "effect";

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 5–7why, one step back
5
6import { Projects } from "./projects.ts";7

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 8–11why, one step back
8export interface StandingEntry {
9 readonly posture: Posture;10 readonly until?: DateTime.Utc;11 /**

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

lines 12–13why, one step back
12 * A store that applied a later event of the project keeps nothing from this
13 * read.

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 14–26why, one step back
14 */
15 readonly seen: number;16}1718export interface StandingPosturesApi {19 readonly get: (20 projects: readonly ProjectName[]21 ) => Effect.Effect<ReadonlyMap<ProjectName, StandingEntry>>;22 readonly put: (23 entries: ReadonlyMap<ProjectName, StandingEntry>24 ) => Effect.Effect<void>;25}26

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 27why, one step back
27/** The default keeps nothing, so every list reads each project. */

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 28–37why, one step back
28export const StandingPostures = Context.Reference<StandingPosturesApi>(
29 "project/StandingPostures",30 {31 defaultValue: () => ({32 get: () => Effect.succeed(new Map()),33 put: () => Effect.void,34 }),35 }36);37

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 38–39why, one step back
38const READS_AT_ONCE = 8;
39

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 40–50why, one step back
40const orWarn = <A, E>(
41 effect: Effect.Effect<A, E>,42 fallback: A,43 warning: string44): Effect.Effect<A> =>45 Effect.catchCause(effect, (cause) =>46 Cause.hasInterruptsOnly(cause)47 ? Effect.interrupt48 : Effect.as(Effect.logWarning(warning, cause), fallback)49 );50

Merge build/1102 into mainoscabrielbefore adoption084383d

line 51why, one step back
51/**

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

lines 52–54why, one step back
52 * The projection's posture moves only on a `PostureChanged` entry, but a
53 * standing posture also moves by the clock and without a decision, so a list54 * reads each project's own record.

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 55–59why, one step back
55 */
56export const withStandingPosture = (57 listed: readonly ProjectListing[]58): Effect.Effect<readonly ProjectListing[], never, Projects> =>59 Effect.gen(function* () {

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 60–63why, one step back
60 if (listed.length === 0) {
61 return listed;62 }63

Merge build/1102 into mainoscabrielbefore adoption084383d

line 64why, one step back
64 const projects = yield* Projects;

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 65–67why, one step back
65 const store = yield* StandingPostures;
66 const now = yield* DateTime.now;67 const names = Arr.dedupe(listed.map(({ project }) => project));

Merge build/1102 into mainoscabrielbefore adoption084383d

line 68why, one step back
68

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 69–98why, one step back
69 const kept = yield* orWarn(
70 Effect.suspend(() => store.get(names)),71 new Map<ProjectName, StandingEntry>(),72 "the kept standing postures were not read: every project is read"73 );7475 const fresh = new Map(76 [...kept].filter(77 ([, { until }]) =>78 until === undefined || DateTime.isLessThan(now, until)79 )80 );8182 const read = yield* Effect.forEach(83 names.filter((project) => !fresh.has(project)),84 (project) =>85 orWarn(86 Effect.suspend(() => projects.get(project)).pipe(87 Effect.flatMap((api) => api.standing()),88 Effect.map(({ posture, seen, until }) =>89 Option.some([90 project,91 until === undefined92 ? { posture, seen }93 : { posture, seen, until },94 ] as const)95 )96 ),97 Option.none(),98 `${project}'s posture is the projection's: its ledger was not read`

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 99–101why, one step back
99 ),
100 { concurrency: READS_AT_ONCE }101 );

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 102–119why, one step back
102
103 const found = new Map<ProjectName, StandingEntry>(Arr.getSomes(read));104105 if (found.size > 0) {106 yield* orWarn(107 Effect.suspend(() => store.put(found)),108 undefined,109 "the standing postures read were not kept"110 );111 }112113 return listed.map((listing): ProjectListing => {114 const entry = found.get(listing.project) ?? fresh.get(listing.project);115116 return entry === undefined117 ? listing118 : { ...listing, posture: entry.posture };119 });

Merge build/1102 into mainoscabrielbefore adoption084383d

line 120why, one step back
120 });