import * as Chunk from "@effect/data/Chunk" import { pipe } from "@effect/data/Function" import * as HashMap from "@effect/data/HashMap" import * as Option from "@effect/data/Option" import type * as DataSource from "@effect/query/DataSource" import type * as BlockedRequest from "@effect/query/internal_effect_untraced/blockedRequest" /** @internal */ export const SequentialTypeId: unique symbol = Symbol.for("@effect/query/Sequential") /** @internal */ export type SequentialTypeId = typeof SequentialTypeId /** * A `Sequential` maintains a mapping from data sources to batches of * requests from those data sources that must be executed sequentially. * * @internal */ export interface Sequential extends Sequential.Variance { readonly map: HashMap.HashMap< DataSource.DataSource, Chunk.Chunk>> > } /** @internal */ export declare namespace Sequential { /** @internal */ export interface Variance { readonly [SequentialTypeId]: { readonly _R: (_: never) => R } } } /** @internal */ const sequentialVariance = { _R: (_: never) => _ } class SequentialImpl implements Sequential { readonly [SequentialTypeId] = sequentialVariance constructor( readonly map: HashMap.HashMap< DataSource.DataSource, Chunk.Chunk>> > ) {} } /** * Constructs a new mapping from data sources to batches of requests from those * data sources that must be executed sequentially. * * @internal */ export const make = ( map: HashMap.HashMap< DataSource.DataSource, Chunk.Chunk>> > ): Sequential => new SequentialImpl(map) /** * Combines this collection of batches of requests that must be executed * sequentially with that collection of batches of requests that must be * executed sequentially to return a new collection of batches of requests * that must be executed sequentially. * * @internal */ export const combine = ( self: Sequential, that: Sequential ): Sequential => new SequentialImpl(HashMap.reduceWithIndex(that.map, self.map, (map, value, key) => HashMap.set( map, key, pipe( HashMap.get(map, key), Option.match( () => Chunk.empty(), Chunk.concat(value) ) ) ))) /** * Returns whether this collection of batches of requests is empty. * * @internal */ export const isEmpty = (self: Sequential): boolean => HashMap.isEmpty(self.map) /** * Returns a collection of the data sources that the batches of requests in * this collection are from. * * @internal */ export const keys = ( self: Sequential ): Chunk.Chunk> => Chunk.fromIterable(HashMap.keys(self.map)) as any /** * Converts this collection of batches requests that must be executed * sequentially to an `Iterable` containing mappings from data sources to * batches of requests from those data sources. * * @internal */ export const toChunk = ( self: Sequential ): Chunk.Chunk< readonly [ DataSource.DataSource, Chunk.Chunk>> ] > => Chunk.fromIterable(self.map) as any