import * as Either from "@effect/data/Either" import * as Equal from "@effect/data/Equal" import * as HashMap from "@effect/data/HashMap" import * as List from "@effect/data/List" import * as Option from "@effect/data/Option" import type * as Deferred from "@effect/io/Deferred" import type { FiberId } from "@effect/io/FiberId" import type * as Request from "@effect/io/Request" import type * as RequestBlock from "@effect/io/RequestBlock" import type * as RequestResolver from "@effect/io/RequestResolver" /** @internal */ export const empty: RequestBlock.RequestBlock = { _tag: "Empty" } /** * Combines this collection of blocked requests with the specified collection * of blocked requests, in parallel. * * @internal */ export const par = ( self: RequestBlock.RequestBlock, that: RequestBlock.RequestBlock ): RequestBlock.RequestBlock => ({ _tag: "Par", left: self, right: that }) /** * Combines this collection of blocked requests with the specified collection * of blocked requests, in sequence. * * @internal */ export const seq = ( self: RequestBlock.RequestBlock, that: RequestBlock.RequestBlock ): RequestBlock.RequestBlock => ({ _tag: "Seq", left: self, right: that }) /** * Constructs a collection of blocked requests from the specified blocked * request and data source. * * @internal */ export const single = ( dataSource: RequestResolver.RequestResolver, blockedRequest: Request.Entry ): RequestBlock.RequestBlock => ({ _tag: "Single", dataSource, blockedRequest }) /** @internal */ export const MapRequestResolversReducer = ( f: (dataSource: RequestResolver.RequestResolver) => RequestResolver.RequestResolver ): RequestBlock.RequestBlock.Reducer> => ({ emptyCase: () => empty, parCase: (left, right) => par(left, right), seqCase: (left, right) => seq(left, right), singleCase: (dataSource, blockedRequest) => single(f(dataSource), blockedRequest) }) type BlockedRequestsCase = ParCase | SeqCase interface ParCase { readonly _tag: "ParCase" } interface SeqCase { readonly _tag: "SeqCase" } /** * Transforms all data sources with the specified data source aspect, which * can change the environment type of data sources but must preserve the * request type of each data source. * * @internal */ export const mapRequestResolvers = ( self: RequestBlock.RequestBlock, f: (dataSource: RequestResolver.RequestResolver) => RequestResolver.RequestResolver ): RequestBlock.RequestBlock => reduce(self, MapRequestResolversReducer(f)) /** * Folds over the cases of this collection of blocked requests with the * specified functions. * * @internal */ export const reduce = ( self: RequestBlock.RequestBlock, reducer: RequestBlock.RequestBlock.Reducer ): Z => { let input: List.List> = List.of(self) let output = List.empty>() while (List.isCons(input)) { const current: RequestBlock.RequestBlock = input.head switch (current._tag) { case "Empty": { output = List.cons(Either.right(reducer.emptyCase()), output) input = input.tail break } case "Par": { output = List.cons(Either.left({ _tag: "ParCase" }), output) input = List.cons(current.left, List.cons(current.right, input.tail)) break } case "Seq": { output = List.cons(Either.left({ _tag: "SeqCase" }), output) input = List.cons(current.left, List.cons(current.right, input.tail)) break } case "Single": { const result = reducer.singleCase(current.dataSource, current.blockedRequest) output = List.cons(Either.right(result), output) input = input.tail break } } } const result = List.reduce(output, List.empty(), (acc, current) => { switch (current._tag) { case "Left": { const left = List.unsafeHead(acc) const right = List.unsafeHead(List.unsafeTail(acc)) const tail = List.unsafeTail(List.unsafeTail(acc)) switch (current.left._tag) { case "ParCase": { return List.cons(reducer.parCase(left, right), tail) } case "SeqCase": { return List.cons(reducer.seqCase(left, right), tail) } } } case "Right": { return List.cons(current.right, acc) } } }) if (List.isNil(result)) { throw new Error("BUG: BlockedRequests.reduce - please report an issue at https://github.com/Effect-TS/query/issues") } return result.head } /** * Flattens a collection of blocked requests into a collection of pipelined * and batched requests that can be submitted for execution. * * @internal */ export const flatten = ( self: RequestBlock.RequestBlock ): List.List> => { let current = List.of(self) let updated = List.empty>() // eslint-disable-next-line no-constant-condition while (1) { const [parallel, sequential] = List.reduce( current, [parallelCollectionEmpty(), List.empty>()] as const, ([parallel, sequential], blockedRequest) => { const [par, seq] = step(blockedRequest) return [ parallelCollectionCombine(parallel, par), List.appendAll(sequential, seq) ] } ) updated = merge(updated, parallel) if (List.isNil(sequential)) { return List.reverse(updated) } current = sequential } throw new Error( "BUG: BlockedRequests.flatten - please report an issue at https://github.com/Effect-TS/query/issues" ) } /** * Takes one step in evaluating a collection of blocked requests, returning a * collection of blocked requests that can be performed in parallel and a list * of blocked requests that must be performed sequentially after those * requests. */ const step = ( requests: RequestBlock.RequestBlock ): readonly [ParallelCollection, List.List>] => { let current: RequestBlock.RequestBlock = requests let parallel = parallelCollectionEmpty() let stack = List.empty>() let sequential = List.empty>() // eslint-disable-next-line no-constant-condition while (1) { switch (current._tag) { case "Empty": { if (List.isNil(stack)) { return [parallel, sequential] as const } current = stack.head stack = stack.tail break } case "Par": { stack = List.cons(current.right, stack) current = current.left break } case "Seq": { const left = current.left const right = current.right switch (left._tag) { case "Empty": { current = right break } case "Par": { const l = left.left const r = left.right current = par(seq(l, right), seq(r, right)) break } case "Seq": { const l = left.left const r = left.right current = seq(l, seq(r, right)) break } case "Single": { current = left sequential = List.cons(right, sequential) break } } break } case "Single": { parallel = parallelCollectionCombine( parallel, parallelCollectionMake(current.dataSource, current.blockedRequest) ) if (List.isNil(stack)) { return [parallel, sequential] } current = stack.head stack = stack.tail break } } } throw new Error( "BUG: BlockedRequests.step - please report an issue at https://github.com/Effect-TS/query/issues" ) } /** * Merges a collection of requests that must be executed sequentially with a * collection of requests that can be executed in parallel. If the collections * are both from the same single data source then the requests can be * pipelined while preserving ordering guarantees. */ const merge = ( sequential: List.List>, parallel: ParallelCollection ): List.List> => { if (List.isNil(sequential)) { return List.of(parallelCollectionToSequentialCollection(parallel)) } if (parallelCollectionIsEmpty(parallel)) { return sequential } const seqHeadKeys = sequentialCollectionKeys(sequential.head) const parKeys = parallelCollectionKeys(parallel) if ( seqHeadKeys.length === 1 && parKeys.length === 1 && Equal.equals(seqHeadKeys[0], parKeys[0]) ) { return List.cons( sequentialCollectionCombine( sequential.head, parallelCollectionToSequentialCollection(parallel) ), sequential.tail ) } return List.cons(parallelCollectionToSequentialCollection(parallel), sequential) } // // circular // /** @internal */ export const EntryTypeId: Request.EntryTypeId = Symbol.for( "@effect/io/RequestBlock.Entry" ) as Request.EntryTypeId /** @internal */ class EntryImpl> implements Request.Entry { readonly [EntryTypeId] = blockedRequestVariance constructor( readonly request: A, readonly result: Deferred.Deferred, Request.Request.Success>, readonly listeners: Request.Listeners, readonly ownerId: FiberId, readonly state: { completed: boolean } ) {} } /** @internal */ const blockedRequestVariance = { _R: (_: never) => _ } /** @internal */ export const isEntry = (u: unknown): u is Request.Entry => { return typeof u === "object" && u != null && EntryTypeId in u } /** @internal */ export const makeEntry = >( options: { readonly request: A readonly result: Deferred.Deferred, Request.Request.Success> readonly listeners: Request.Listeners readonly ownerId: FiberId readonly state: { completed: boolean } } ): Request.Entry => new EntryImpl(options.request, options.result, options.listeners, options.ownerId, options.state) /** @internal */ export const RequestBlockParallelTypeId = Symbol.for( "@effect/io/RequestBlockParallel" ) const parallelVariance = { _R: (_: never) => _ } class ParallelImpl implements ParallelCollection { readonly [RequestBlockParallelTypeId] = parallelVariance constructor( readonly map: HashMap.HashMap< RequestResolver.RequestResolver, Array> > ) {} } /** @internal */ export const parallelCollectionEmpty = (): ParallelCollection => new ParallelImpl(HashMap.empty()) /** @internal */ export const parallelCollectionMake = ( dataSource: RequestResolver.RequestResolver, blockedRequest: Request.Entry ): ParallelCollection => new ParallelImpl(HashMap.make([dataSource, Array.of(blockedRequest)])) /** @internal */ export const parallelCollectionCombine = ( self: ParallelCollection, that: ParallelCollection ): ParallelCollection => new ParallelImpl(HashMap.reduce(self.map, that.map, (map, value, key) => HashMap.set( map, key, Option.match(HashMap.get(map, key), { onNone: () => value, onSome: (a) => [...a, ...value] }) ))) /** @internal */ export const parallelCollectionIsEmpty = (self: ParallelCollection): boolean => HashMap.isEmpty(self.map) /** @internal */ export const parallelCollectionKeys = ( self: ParallelCollection ): Array> => Array.from(HashMap.keys(self.map)) as any /** @internal */ export const parallelCollectionToSequentialCollection = ( self: ParallelCollection ): SequentialCollection => sequentialCollectionMake(HashMap.map(self.map, (x) => Array.of(x)) as any) /** @internal */ export const parallelCollectionToChunk = ( self: ParallelCollection ): Array< readonly [ RequestResolver.RequestResolver, Array> ] > => Array.from(self.map) as any /** @internal */ export const SequentialCollectionTypeId = Symbol.for( "@effect/io/RequestBlockSequential" ) /** @internal */ const sequentialVariance = { _R: (_: never) => _ } class SequentialImpl implements SequentialCollection { readonly [SequentialCollectionTypeId] = sequentialVariance constructor( readonly map: HashMap.HashMap< RequestResolver.RequestResolver, Array>> > ) {} } /** @internal */ export const sequentialCollectionMake = ( map: HashMap.HashMap< RequestResolver.RequestResolver, Array>> > ): SequentialCollection => new SequentialImpl(map) /** @internal */ export const sequentialCollectionCombine = ( self: SequentialCollection, that: SequentialCollection ): SequentialCollection => new SequentialImpl(HashMap.reduce(that.map, self.map, (map, value, key) => HashMap.set( map, key, Option.match(HashMap.get(map, key), { onNone: () => [], onSome: (a) => [...a, ...value] }) ))) /** @internal */ export const sequentialCollectionIsEmpty = (self: SequentialCollection): boolean => HashMap.isEmpty(self.map) /** @internal */ export const sequentialCollectionKeys = ( self: SequentialCollection ): Array> => Array.from(HashMap.keys(self.map)) as any /** @internal */ export const sequentialCollectionToChunk = (self: SequentialCollection): Array< readonly [ RequestResolver.RequestResolver, Array>> ] > => Array.from(self.map) as any /** @internal */ export type RequestBlockParallelTypeId = typeof RequestBlockParallelTypeId /** @internal */ export interface ParallelCollection extends ParallelCollection.Variance { readonly map: HashMap.HashMap< RequestResolver.RequestResolver, Array> > } /** @internal */ export declare namespace ParallelCollection { /** @internal */ export interface Variance { readonly [RequestBlockParallelTypeId]: { readonly _R: (_: never) => R } } } /** @internal */ export type SequentialCollectionTypeId = typeof SequentialCollectionTypeId /** @internal */ export interface SequentialCollection extends SequentialCollection.Variance { readonly map: HashMap.HashMap< RequestResolver.RequestResolver, Array>> > } /** @internal */ export declare namespace SequentialCollection { /** @internal */ export interface Variance { readonly [SequentialCollectionTypeId]: { readonly _R: (_: never) => R } } }