// ets_tracing: off import { collect_ } from "../Collections/Immutable/Chunk/api/collect.js" import { filterEffect_ } from "../Collections/Immutable/Chunk/api/filterEffect.js" import { mapEffect_ } from "../Collections/Immutable/Chunk/api/mapEffect.js" import { zip_ } from "../Collections/Immutable/Chunk/api/zip.js" import * as Chunk from "../Collections/Immutable/Chunk/core.js" import { succeed } from "../Effect/core.js" import * as exclForEach from "../Effect/excl-forEach.js" import { BackPressureStrategy, createQueue, makeBoundedQueue as makeBounded, unsafeCreateQueue as unsafeCreate } from "../Effect/excl-forEach.js" import { identity, pipe, tuple } from "../Function/index.js" import * as O from "../Option/index.js" import { Bounded, Unbounded } from "../Support/MutableQueue/index.js" import { DroppingStrategy, SlidingStrategy } from "./core.js" import * as T from "./effect-api.js" import type { Queue, XQueue } from "./xqueue.js" import { concreteQueue, XQueueInternal } from "./xqueue.js" export { createQueue, makeBounded, unsafeCreate, BackPressureStrategy } /** * Creates a sliding queue */ export function makeSliding(capacity: number): T.UIO> { return T.chain_( T.succeedWith(() => new Bounded(capacity)), exclForEach.createQueue(new SlidingStrategy()) ) } /** * Creates a unbouded queue */ export function makeUnbounded(): T.UIO> { return T.chain_( T.succeedWith(() => new Unbounded()), exclForEach.createQueue(new DroppingStrategy()) ) } /** * Creates a dropping queue */ export function makeDropping(capacity: number): T.UIO> { return T.chain_( T.succeedWith(() => new Bounded(capacity)), exclForEach.createQueue(new DroppingStrategy()) ) } function takeRemainderLoop( self: XQueue, n: number ): T.Effect> { concreteQueue(self) if (n <= 0) { return T.succeed(Chunk.empty()) } else { return T.chain_(self.take, (a) => T.map_(takeRemainderLoop(self, n - 1), (_) => Chunk.append_(_, a)) ) } } /** * Takes between min and max number of values from the queue. If there * is less than min items available, it'll block until the items are * collected. * * @ets_data_first takeBetween_ */ export function takeBetween(min: number, max: number) { return ( self: XQueue ): T.Effect> => takeBetween_(self, min, max) } /** * Takes between min and max number of values from the queue. If there * is less than min items available, it'll block until the items are * collected. */ export function takeBetween_( self: XQueue, min: number, max: number ): T.Effect> { concreteQueue(self) if (max < min) { return T.succeed(Chunk.empty()) } else { return pipe( self.takeUpTo(max), T.chain((bs) => { const remaining = min - Chunk.size(bs) if (remaining === 1) { return T.map_(self.take, (b) => Chunk.append_(bs, b)) } else if (remaining > 1) { return T.map_(takeRemainderLoop(self, remaining), (list) => Chunk.concat_(bs, list) ) } else { return T.succeed(bs) } }) ) } } /** * Creates a new queue from this queue and another. Offering to the composite queue * will broadcast the elements to both queues; taking from the composite queue * will dequeue elements from both queues and apply the function point-wise. * * Note that using queues with different strategies may result in surprising behavior. * For example, a dropping queue and a bounded queue composed together may apply `f` * to different elements. * * @ets_data_first bothWithM_ */ export function bothWithM( that: XQueue, f: (b: B, c: C) => T.Effect ) { return (self: XQueue) => bothWithM_(self, that, f) } /** * Creates a new queue from this queue and another. Offering to the composite queue * will broadcast the elements to both queues; taking from the composite queue * will dequeue elements from both queues and apply the function point-wise. * * Note that using queues with different strategies may result in surprising behavior. * For example, a dropping queue and a bounded queue composed together may apply `f` * to different elements. */ export function bothWithM_< RA, RB, EA, EB, RA1, RB1, EA1, EB1, A1 extends A, C, B, R3, E3, D, A >( self: XQueue, that: XQueue, f: (b: B, c: C) => T.Effect ): XQueue { concreteQueue(self) concreteQueue(that) return new BothWithM(self, that, f) } class BothWithM< RA, RB, EA, EB, RA1, RB1, EA1, EB1, A1 extends A, C, B, R3, E3, D, A > extends XQueueInternal { constructor( readonly self: XQueueInternal, readonly that: XQueueInternal, readonly f: (b: B, c: C) => T.Effect ) { super() } awaitShutdown: T.UIO = T.chain_( this.self.awaitShutdown, () => this.that.awaitShutdown ) capacity: number = Math.min(this.self.capacity, this.that.capacity) isShutdown: T.UIO = this.self.isShutdown offer(a: A1): T.Effect { return T.zipWithPar_(this.self.offer(a), this.that.offer(a), (x, y) => x && y) } offerAll(as: Iterable): T.Effect { return T.zipWithPar_( this.self.offerAll(as), this.that.offerAll(as), (x, y) => x && y ) } shutdown: T.UIO = T.zipWithPar_( this.self.shutdown, this.that.shutdown, () => undefined ) size: T.UIO = T.zipWithPar_(this.self.size, this.that.size, (x, y) => Math.max(x, y) ) take: T.Effect = T.chain_( T.zipPar_(this.self.take, this.that.take), ({ tuple: [b, c] }) => this.f(b, c) ) takeAll: T.Effect> = T.chain_( T.zipPar_(this.self.takeAll, this.that.takeAll), ({ tuple: [bs, cs] }) => mapEffect_(zip_(bs, cs), ({ tuple: [b, c] }) => this.f(b, c)) ) takeUpTo(max: number): T.Effect> { return T.chain_( T.zipPar_(this.self.takeUpTo(max), this.that.takeUpTo(max)), ({ tuple: [bs, cs] }) => mapEffect_(zip_(bs, cs), ({ tuple: [b, c] }) => this.f(b, c)) ) } } /** * Like `bothWithM`, but uses a pure function. * * @ets_data_first bothWith_ */ export function bothWith( that: XQueue, f: (b: B, c: C) => D ) { return (self: XQueue) => bothWithM_(self, that, (b, c) => T.succeed(f(b, c))) } /** * Like `bothWithM`, but uses a pure function. */ export function bothWith_( self: XQueue, that: XQueue, f: (b: B, c: C) => D ) { return bothWithM_(self, that, (b, c) => T.succeed(f(b, c))) } /** * Like `bothWith`, but tuples the elements instead of applying a function. * * @ets_data_first both_ */ export function both( that: XQueue ) { return (self: XQueue) => bothWith_(self, that, (b, c) => tuple(b, c)) } /** * Like `bothWith`, but tuples the elements instead of applying a function. */ export function both_( self: XQueue, that: XQueue ) { return bothWith_(self, that, (b, c) => tuple(b, c)) } /** * Transforms elements enqueued into and dequeued from this queue with the * specified effectual functions. * * @ets_data_first dimap_ */ export function dimap(f: (c: C) => A, g: (b: B) => D) { return (self: XQueue) => dimap_(self, f, g) } /** * Transforms elements enqueued into and dequeued from this queue with the * specified effectual functions. */ export function dimap_( self: XQueue, f: (c: C) => A, g: (b: B) => D ) { return dimapM_( self, (c: C) => succeed(f(c)), (b) => succeed(g(b)) ) } /** * Transforms elements enqueued into and dequeued from this queue with the * specified effectual functions. * * @ets_data_first dimapM_ */ export function dimapM( f: (c: C) => T.Effect, g: (b: B) => T.Effect ) { return ( self: XQueue ): XQueue => dimapM_(self, f, g) } /** * Transforms elements enqueued into and dequeued from this queue with the * specified effectual functions. */ export function dimapM_( self: XQueue, f: (c: C) => T.Effect, g: (b: B) => T.Effect ): XQueue { concreteQueue(self) return new DimapM(self, f, g) } class DimapM extends XQueueInternal< RC & RA, RD & RB, EC | EA, ED | EB, C, D > { constructor( readonly self: XQueueInternal, readonly f: (c: C) => T.Effect, readonly g: (b: B) => T.Effect ) { super() } awaitShutdown: T.UIO = this.self.awaitShutdown capacity: number = this.self.capacity isShutdown: T.UIO = this.self.isShutdown offer(a: C): T.Effect { return T.chain_(this.f(a), (a) => this.self.offer(a)) } offerAll(as: Iterable): T.Effect { return T.chain_(T.forEach_(as, this.f), (as) => this.self.offerAll(as)) } shutdown: T.UIO = this.self.shutdown size: T.UIO = this.self.size take: T.Effect = T.chain_(this.self.take, this.g) takeAll: T.Effect> = T.chain_( this.self.takeAll, (a) => mapEffect_(a, this.g) ) takeUpTo(n: number): T.Effect> { return T.chain_(this.self.takeUpTo(n), (bs) => mapEffect_(bs, this.g)) } } /** * Transforms elements enqueued into this queue with an effectful function. */ export function contramapM_( self: XQueue, f: (c: C) => T.Effect ) { return dimapM_(self, f, succeed) } /** * Transforms elements enqueued into this queue with an effectful function. * * @ets_data_first contramapM_ */ export function contramapM(f: (c: C) => T.Effect) { return (self: XQueue) => contramapM_(self, f) } /** * Transforms elements enqueued into this queue with a pure function. */ export function contramap_( self: XQueue, f: (c: C) => A ) { return dimapM_(self, (c: C) => succeed(f(c)), succeed) } /** * Transforms elements enqueued into this queue with a pure function. * * @ets_data_first contramap_ */ export function contramap(f: (c: C) => A) { return (self: XQueue) => contramap_(self, f) } /** * Like `filterInput`, but uses an effectful function to filter the elements. * * @ets_data_first filterInputM_ */ export function filterInputM( f: (_: A1) => T.Effect ) { return ( self: XQueue ): XQueue => filterInputM_(self, f) } /** * Like `filterInput`, but uses an effectful function to filter the elements. */ export function filterInputM_( self: XQueue, f: (_: A1) => T.Effect ): XQueue { concreteQueue(self) return new FilterInputM(self, f) } class FilterInputM extends XQueueInternal< RA & R2, RB, EA | E2, EB, A1, B > { constructor( readonly self: XQueueInternal, readonly f: (_: A1) => T.Effect ) { super() } awaitShutdown: T.UIO = this.self.awaitShutdown capacity: number = this.self.capacity isShutdown: T.UIO = this.self.isShutdown offer(a: A1): T.Effect { return T.chain_(this.f(a), (b) => (b ? this.self.offer(a) : T.succeed(false))) } offerAll(as: Iterable): T.Effect { return pipe( as, T.forEach((a) => pipe( this.f(a), T.map((b) => (b ? O.some(a) : O.none)) ) ), T.chain((maybeAs) => { const filtered = collect_(maybeAs, identity) if (Chunk.isEmpty(filtered)) { return T.succeed(false) } else { return this.self.offerAll(filtered) } }) ) } shutdown: T.UIO = this.self.shutdown size: T.UIO = this.self.size take: T.Effect = this.self.take takeAll: T.Effect> = this.self.takeAll takeUpTo(n: number): T.Effect> { return this.self.takeUpTo(n) } } /** * Filters elements dequeued from the queue using the specified effectual * predicate. */ export function filterOutputM_( self: XQueue, f: (b: B) => T.Effect ): XQueue { concreteQueue(self) return new FilterOutputM(self, f) } class FilterOutputM extends XQueueInternal< RA, RB & RB1, EA, EB | EB1, A, B > { constructor( readonly self: XQueueInternal, readonly f: (b: B) => T.Effect ) { super() } awaitShutdown: T.UIO = this.self.awaitShutdown capacity: number = this.self.capacity isShutdown: T.UIO = this.self.isShutdown offer(a: A): T.Effect { return this.self.offer(a) } offerAll(as: Iterable): T.Effect { return this.self.offerAll(as) } shutdown: T.UIO = this.self.shutdown size: T.UIO = this.self.size take: T.Effect = T.chain_(this.self.take, (b) => { return T.chain_(this.f(b), (p) => { return p ? T.succeed(b) : this.take }) }) takeAll: T.Effect> = T.chain_( this.self.takeAll, (bs) => filterEffect_(bs, this.f) ) loop(max: number, acc: Chunk.Chunk): T.Effect> { return T.chain_(this.self.takeUpTo(max), (bs) => { if (Chunk.isEmpty(bs)) { return T.succeed(acc) } return T.chain_(filterEffect_(bs, this.f), (filtered) => { const length = Chunk.size(filtered) if (length === max) { return T.succeed(Chunk.concat_(acc, filtered)) } else { return this.loop(max - length, Chunk.concat_(acc, filtered)) } }) }) } takeUpTo(n: number): T.Effect> { return T.suspend(() => { return this.loop(n, Chunk.empty()) }) } } /** * Filters elements dequeued from the queue using the specified effectual * predicate. * * @ets_data_first filterOutputM_ */ export function filterOutputM(f: (b: B) => T.Effect) { return (self: XQueue) => filterOutputM_(self, f) } /** * Filters elements dequeued from the queue using the specified predicate. */ export function filterOutput_( self: XQueue, f: (b: B) => boolean ): XQueue { return filterOutputM_(self, (b) => T.succeed(f(b))) } /** * Filters elements dequeued from the queue using the specified predicate. * * @ets_data_first filterOutput_ */ export function filterOutput(f: (b: B) => boolean) { return (self: XQueue) => filterOutput_(self, f) } /** * Applies a filter to elements enqueued into this queue. Elements that do not * pass the filter will be immediately dropped. * * @ets_data_first filterInput_ */ export function filterInput(f: (_: A1) => boolean) { return ( self: XQueue ): XQueue => filterInput_(self, f) } /** * Applies a filter to elements enqueued into this queue. Elements that do not * pass the filter will be immediately dropped. */ export function filterInput_( self: XQueue, f: (_: A1) => boolean ): XQueue { return filterInputM_(self, (a) => T.succeed(f(a))) } /** * Transforms elements dequeued from this queue with a function. */ export function map_( self: XQueue, f: (b: B) => C ): XQueue { return mapM_(self, (_) => T.succeed(f(_))) } /** * Transforms elements dequeued from this queue with a function. * * @ets_data_first map_ */ export function map(f: (b: B) => C) { return (self: XQueue) => map_(self, f) } /** * Transforms elements dequeued from this queue with an effectful function. * * @ets_data_first mapM_ */ export function mapM(f: (b: B) => T.Effect) { return (self: XQueue) => mapM_(self, f) } /** * Transforms elements dequeued from this queue with an effectful function. */ export function mapM_( self: XQueue, f: (b: B) => T.Effect ) { return dimapM_(self, (a: A) => T.succeed(a), f) } /** * Take the head option of values in the queue. */ export function poll(self: XQueue) { concreteQueue(self) return T.map_(self.takeUpTo(1), (x) => Chunk.unsafeGet_(x, 0)) }