// ets_tracing: off import * as A from "@effect-ts/core/Collections/Immutable/Chunk" import * as HM from "@effect-ts/core/Collections/Immutable/HashMap" import { _R } from "@effect-ts/core/Effect" import * as O from "@effect-ts/core/Option" import type * as DS from "../../DataSource/index.js" import type { BlockedRequest } from "../BlockedRequest/index.js" import type { BlockedRequests } from "../BlockedRequests/index.js" import { Sequential } from "../Sequential/index.js" /** * A `Parallel[R]` maintains a mapping from data sources to requests from * those data sources that can be executed in parallel. */ export class Parallel { readonly [_R]!: (r: R) => void constructor( public readonly map: HM.HashMap< DS.DataSource, A.Chunk> > ) {} } /** * Combines this collection of requests that can be executed in parallel * with that collection of requests that can be executed in parallel to * return a new collection of requests that can be executed in parallel. */ export function add(that: Parallel) { return (self: Parallel): Parallel => add_(self, that) } export function add_(self: Parallel, that: Parallel) { return new Parallel( HM.reduceWithIndex_(self.map, that.map, (map, k, v) => HM.set_( map, k, O.fold_( HM.get_(map, k), () => v, (_) => A.concat_(_, v) ) ) ) ) } /** * Returns whether this collection of requests is empty. */ export function isEmpty(self: Parallel) { return HM.isEmpty(self.map) } /** * Returns a collection of the data sources that the requests in this * collection are from. */ export function keys(self: Parallel): Iterable> { return HM.keys(self.map) } /** * Converts this collection of requests that can be executed in parallel to * a batch of requests in a collection of requests that must be executed * sequentially. */ export function sequential(self: Parallel): Sequential { return new Sequential(HM.mapWithIndex_(self.map, (_, v) => A.single(v))) } /** * Converts this collection of requests that can be executed in parallel to * an `Iterable` containing mappings from data sources to requests from * those data sources. */ export function toIterable( self: Parallel ): Iterable< readonly [DS.DataSource, A.Chunk>>] > { return self.map as any } /** * The empty collection of requests. */ export const empty = new Parallel(HM.make()) /** * Constructs a new collection of requests containing a mapping from the * specified data source to the specified request. */ export function apply( dataSource: DS.DataSource, blockedRequest: BlockedRequest ): Parallel { return new Parallel( HM.set_( HM.make(), dataSource as DS.DataSource, A.single(blockedRequest) ) ) }