memory.ts
This file
Path
apps/platform/src/auth/identities/memory.ts
46 lines · 1.3 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import { Handle } from "@repo/protocol/ids";import { Effect, Layer, Option, Ref } from "effect";
import { handleKey } from "../accounts.ts";import { Identities, IdentityTaken } from "../identities.ts";
const keyOf = (provider: string, subject: string) => `${provider}\n${subject}`;
export const IdentitiesMemory = { layer: Layer.effect( Identities, Effect.gen(function* () { const links = yield* Ref.make(new Map<string, string>());
return Identities.of({ handleOf: (provider, subject) => Ref.get(links).pipe( Effect.map((linked) => Option.map( Option.fromUndefinedOr(linked.get(keyOf(provider, subject))), (key) => Handle.make(key) ) ) ), link: (provider, subject, handle) => Ref.modify(links, (linked) => { const key = keyOf(provider, subject);
const held = linked.get(key);
if (held === undefined) { return [true, new Map(linked).set(key, handleKey(handle))]; }
return [held === handleKey(handle), linked]; }).pipe( Effect.filterOrFail( (linked) => linked, () => new IdentityTaken({ provider, subject }) ), Effect.asVoid ), }); }) ),};