memory.ts
This file
Path
packages/project/src/projects/memory.ts
56 lines · 1.9 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–56why, one step back1import type { ProjectName } from "@repo/protocol/ids";2import { ProjectExists, UnknownProject } from "@repo/protocol/refusal";3import { Context, Effect, Layer, Option, SynchronizedRef } from "effect";45import type { ProjectStore } from "../project-store.ts";6import { ProjectStoreMemory } from "../project-store/memory.ts";7import { projectService } from "../project.ts";8import type { ProjectApi, ProjectPorts } from "../project.ts";9import { Projects } from "../projects.ts";1011export const ProjectsMemory: Layer.Layer<12 Projects,13 never,14 Exclude<ProjectPorts, ProjectStore>15> = Layer.effect(16 Projects,17 Effect.gen(function* () {18 const ports = yield* Effect.context<Exclude<ProjectPorts, ProjectStore>>();19 const scope = yield* Effect.scope;2021 const made = yield* SynchronizedRef.make<22 ReadonlyMap<ProjectName, ProjectApi>23 >(new Map());2425 const build = (project: ProjectName) =>26 Effect.gen(function* () {27 // Fresh, or the build would reuse a store an enclosing layer made.28 const store = yield* Layer.buildWithScope(29 Layer.fresh(ProjectStoreMemory),30 scope31 );3233 return yield* projectService(project).pipe(34 Effect.provideContext(Context.merge(ports, store))35 );36 });3738 return Projects.of({39 create: (project) =>40 SynchronizedRef.modifyEffect(made, (projects) =>41 projects.has(project)42 ? Effect.fail(new ProjectExists({ project }))43 : Effect.map(44 build(project),45 (api) => [api, new Map(projects).set(project, api)] as const46 )47 ),48 get: (project) =>49 Effect.flatMap(SynchronizedRef.get(made), (projects) =>50 Effect.fromOption(Option.fromUndefinedOr(projects.get(project))).pipe(51 Effect.mapError(() => new UnknownProject({ project }))52 )53 ),54 });55 })56);