Skip to content
Myr

memory.ts

This file

Path

packages/project/src/code-store/memory.ts

49 lines · 1.4 KiB · TypeScript

Last changed by

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

History of this file

Take it

Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c

lines 1–49why, one step back
1import { Context, Effect, Layer, Option, Ref } from "effect";
23import { CodeStore, fittingKey } from "../code-store.ts";4import type { CodeStoreApi } from "../code-store.ts";56export class CodeStoreCounts extends Context.Service<7 CodeStoreCounts,8 {9 readonly counts: Effect.Effect<{10 readonly gets: number;11 readonly puts: number;12 }>;13 }14>()("project/CodeStoreCounts") {}1516export const CodeStoreMemory = Layer.effectContext(17 Effect.gen(function* () {18 const kept = yield* Ref.make<ReadonlyMap<string, string>>(new Map());19 const counts = yield* Ref.make({ gets: 0, puts: 0 });2021 const store: CodeStoreApi = {22 get: (key) =>23 Effect.gen(function* () {24 const fit = yield* fittingKey(key);2526 yield* Ref.update(counts, ({ gets, puts }) => ({27 gets: gets + 1,28 puts,29 }));3031 return Option.fromUndefinedOr((yield* Ref.get(kept)).get(fit));32 }),33 put: (key, value) =>34 Effect.gen(function* () {35 const fit = yield* fittingKey(key);3637 yield* Ref.update(counts, ({ gets, puts }) => ({38 gets,39 puts: puts + 1,40 }));41 yield* Ref.update(kept, (all) => new Map([...all, [fit, value]]));42 }),43 };4445 return Context.make(CodeStore, store).pipe(46 Context.add(CodeStoreCounts, { counts: Ref.get(counts) })47 );48 })49);