// ets_tracing: off // port of: https://github.com/zio/zio-query/blob/9dfe9ca0b1e3077fc56cf5c983082af3ca7a62e7/zio-query/shared/src/main/scala/zio/query/CompletedRequestMap.scala import "@effect-ts/system/Operator" import * as M from "@effect-ts/core/Collections/Immutable/HashMap" import type * as HS from "@effect-ts/core/Collections/Immutable/HashSet" import * as E from "@effect-ts/core/Either" import * as O from "@effect-ts/core/Option" import type { Request } from "../Request/index.js" /** * A `CompletedRequestMap` is a universally quantified mapping from requests * of type `Request[E, A]` to results of type `Either[E, A]` for all types `E` * and `A`. The guarantee is that for any request of type `Request[E, A]`, if * there is a corresponding value in the map, that value is of type * `Either[E, A]`. This is used by the library to support data sources that * return different result types for different requests while guaranteeing that * results will be of the type requested. */ export class CompletedRequestMap { readonly _tag = "CompletedRequestMap" constructor(public readonly map: M.HashMap, E.Either>) {} } export function concat( a: CompletedRequestMap, b: CompletedRequestMap ): CompletedRequestMap { return new CompletedRequestMap( M.mutate_(a.map, (m) => { for (const [k, v] of b.map) { M.set_(m, k, v) } }) ) } /** * Returns whether a result exists for the specified request. */ export function contains_(fa: CompletedRequestMap, request: any): boolean { return M.has_(fa.map, request) } /** * Returns whether a result exists for the specified request. * @ets_data_first contains_ */ export function contains(request: any): (fa: CompletedRequestMap) => boolean { return (fa) => contains_(fa, request) } /** * Appends the specified result to the completed requests map. */ export function insert_( fa: CompletedRequestMap, request: Request, result: E.Either ): CompletedRequestMap { return new CompletedRequestMap(M.set_(fa.map, request, result)) } /** * Appends the specified result to the completed requests map. * @ets_data_first insert_ */ export function insert( request: Request, result: E.Either ): (fa: CompletedRequestMap) => CompletedRequestMap { return (fa) => insert_(fa, request, result) } /** * Appends the specified optional result to the completed request map. */ export function insertOption_( fa: CompletedRequestMap, request: Request, result: E.Either> ): CompletedRequestMap { return new CompletedRequestMap( E.fold_( result, (e) => M.set_(fa.map, request, E.left(e)), O.fold( () => fa.map, (a) => M.set_(fa.map, request, E.right(a)) ) ) ) } /** * Appends the specified optional result to the completed request map. * @ets_data_first insertOption_ */ export function insertOption( request: Request, result: E.Either> ): (fa: CompletedRequestMap) => CompletedRequestMap { return (fa) => insertOption_(fa, request, result) } /** * Retrieves the result of the specified request if it exists. */ export function lookup_( fa: CompletedRequestMap, request: Request ): O.Option> { return M.get_(fa.map, request) } /** * Retrieves the result of the specified request if it exists. * @ets_data_first lookup_ */ export function lookup( request: Request ): (fa: CompletedRequestMap) => O.Option> { return (fa) => lookup_(fa, request) } /** * Collects all requests in a set. */ export function requests(fa: CompletedRequestMap): HS.HashSet> { return M.keySet(fa.map) } /** * An empty completed requests map. */ export const empty = new CompletedRequestMap(M.make())