/*! * name: async-primitives * version: 1.7.0 * description: A collection of primitive functions for asynchronous operations * author: Kouji Matsui (@kekyo@mi.kekyo.net) * license: MIT * repository.url: https://github.com/kekyo/async-primitives.git * git.commit.hash: 9472fbd5310b92690d84aaafb897429a04c013c5 */ /** * Releasable interface for resources that can be released explicitly */ export interface Releasable extends Disposable { /** * Release the resource explicitly */ readonly release: () => void; } /** * Lock handle for managing acquired locks */ export interface LockHandle extends Releasable { /** * Indicates if the lock is still active */ readonly isActive: boolean; } /** * Waiter object */ export interface Waiter { /** * Wait to be triggered * @param signal Optional AbortSignal for cancelling the wait * @returns Promise that resolves when triggered, returns lock handle */ readonly wait: (signal?: AbortSignal) => Promise; } /** * Waitable object */ export interface Waitable { /** * Get waiter object * @returns Waiter object */ readonly waiter: Waiter; } /** * Mutex interface for promise-based mutex operations */ export interface Mutex extends Waitable { /** * Acquires the lock asynchronously * @param signal Optional AbortSignal for cancelling the lock acquisition * @returns Promise that resolves to a disposable lock handle */ readonly lock: (signal?: AbortSignal) => Promise; /** * Indicates if the lock is currently acquired */ readonly isLocked: boolean; /** * Number of pending lock requests */ readonly pendingCount: number; } /** * Conditional interface that can be automatically triggered */ export interface Conditional extends Waitable { /** * Trigger the conditional * @remarks This will resolve only one waiter */ readonly trigger: () => void; /** * Wait to be triggered * @param signal Optional AbortSignal for cancelling the wait * @returns Promise that resolves when triggered, returns dummy lock handle */ readonly wait: (signal?: AbortSignal) => Promise; } /** * Conditional interface that can be manually raise and drop */ export interface ManuallyConditional extends Conditional { /** * Raise the conditional * @remarks This will resolve all waiters */ readonly raise: () => void; /** * Drop the conditional * @remarks This will drop the conditional, all waiters will be blocked until the conditional is raised again */ readonly drop: () => void; } /** * Semaphore interface for managing limited concurrent access */ export interface Semaphore extends Waitable { /** * Acquires a semaphore resource asynchronously * @param signal Optional AbortSignal for cancelling the acquisition * @returns Promise that resolves to a disposable semaphore handle */ readonly acquire: (signal?: AbortSignal) => Promise; /** * Number of currently available resources */ readonly availableCount: number; /** * Number of pending acquisition requests */ readonly pendingCount: number; } /** * Lock policy for ReaderWriterLock */ export type ReaderWriterLockPolicy = 'read-preferring' | 'write-preferring'; /** * Options for creating a ReaderWriterLock */ export interface ReaderWriterLockOptions { /** * Lock policy (default: 'write-preferring' */ policy?: ReaderWriterLockPolicy; /** * Maximum consecutive calls before yielding control (default: 20) */ maxConsecutiveCalls?: number; } /** * Reader-Writer lock interface for managing concurrent read and exclusive write access */ export interface ReaderWriterLock { /** * Acquires a read lock asynchronously * @param signal Optional AbortSignal for cancelling the lock acquisition * @returns Promise that resolves to a disposable read lock handle */ readonly readLock: (signal?: AbortSignal) => Promise; /** * Acquires a write lock asynchronously * @param signal Optional AbortSignal for cancelling the lock acquisition * @returns Promise that resolves to a disposable write lock handle */ readonly writeLock: (signal?: AbortSignal) => Promise; /** * Waiter object for reader */ readonly readWaiter: Waiter; /** * Waiter object for writer */ readonly writeWaiter: Waiter; /** * Number of currently active readers */ readonly currentReaders: number; /** * Indicates if a writer currently holds the lock */ readonly hasWriter: boolean; /** * Number of pending read lock requests */ readonly pendingReadersCount: number; /** * Number of pending write lock requests */ readonly pendingWritersCount: number; } /** * Deferred interface for promise-based result handling */ export interface Deferred { /** * Promise that resolves to the result */ readonly promise: Promise; /** * Resolve the promise with a result * @param value The result to resolve the promise with */ readonly resolve: (value: T) => void; /** * Reject the promise with an error * @param error The error to reject the promise with */ readonly reject: (error: any) => void; } /** * Options for creating a deferred generator */ export interface DeferredGeneratorOptions { /** * Optional maximum number of items to reserve in the queue (Default: unlimited) */ maxItemReserved?: number; /** * Optional AbortSignal for cancelling the consumer (async iterator) wait */ signal?: AbortSignal; } /** * Deferred generator interface for async-generator-based streaming result handling */ export interface DeferredGenerator { /** * AsyncGenerator that yields values of type T */ readonly generator: AsyncGenerator; /** * Yield a value to the generator * @param value The value to yield * @param signal Optional AbortSignal for cancelling the yield */ readonly yield: (value: T, signal?: AbortSignal) => Promise; /** * Complete the generator (equivalent to return) * @param signal Optional AbortSignal for cancelling the return */ readonly return: (signal?: AbortSignal) => Promise; /** * Throw an error to the generator * @param error The error to throw * @param signal Optional AbortSignal for cancelling the throw */ readonly throw: (error: any, signal?: AbortSignal) => Promise; } /** * Value or promise-like value */ export type Awaitable = T | PromiseLike; /** * Source sequence handled by `AsyncOperator` */ export type AsyncOperatorSource = Iterable> | AsyncIterable>; /** * Chainable operators for asynchronously resolved collections * @remarks * The sequence is evaluated lazily and sequentially, and can be consumed either by terminal operators * such as `toArray()` or directly via `for await`. */ export interface AsyncOperator extends AsyncIterable { /** * Projects each resolved value into a new value * @param selector Selector function for each resolved value * @returns A new async operator whose values are the projected results */ readonly map: (selector: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Projects each resolved value into a sequence and flattens the result by one level * @param selector Selector function that returns a sequence for each resolved value * @returns A new async operator whose values are the flattened results */ readonly flatMap: (selector: (value: T, index: number) => Awaitable>) => AsyncOperator; /** * Filters resolved values by predicate * @param predicate Predicate function for each resolved value * @returns A new async operator whose values satisfy the predicate */ readonly filter: (predicate: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Concatenates the sequence with additional sources * @param sources Additional sources to append after the current sequence * @returns A new async operator whose values are emitted from each source in order */ readonly concat: (...sources: AsyncOperatorSource[]) => AsyncOperator; /** * Projects each resolved value into another value and omits nullish results * @param selector Selector function for each resolved value * @returns A new async operator whose values are the non-nullish projected results */ readonly choose: (selector: (value: T, index: number) => Awaitable) => AsyncOperator>; /** * Returns a portion of the sequence between two indexes * @param start Zero-based start index, or a negative offset from the end * @param end Zero-based exclusive end index, or a negative offset from the end * @returns A new async operator containing the sliced range * @remarks * This follows `Array.prototype.slice()` semantics. Negative indexes may require consuming the source first. */ readonly slice: (start: number, end?: number) => AsyncOperator; /** * Removes duplicate values * @returns A new async operator whose values are distinct */ readonly distinct: () => AsyncOperator; /** * Removes duplicate values by projected key * @param selector Selector function that produces the distinct key * @returns A new async operator whose values are distinct by key */ readonly distinctBy: (selector: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Skips the specified number of values * @param count Number of values to skip * @returns A new async operator that skips the specified number of values */ readonly skip: (count: number) => AsyncOperator; /** * Skips values while the predicate returns true * @param predicate Predicate function for each resolved value * @returns A new async operator that skips values while the predicate matches */ readonly skipWhile: (predicate: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Takes the specified number of values * @param count Number of values to take * @returns A new async operator that takes the specified number of values */ readonly take: (count: number) => AsyncOperator; /** * Takes values while the predicate returns true * @param predicate Predicate function for each resolved value * @returns A new async operator that takes values while the predicate matches */ readonly takeWhile: (predicate: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Produces adjacent pairs from the sequence * @returns A new async operator whose values are adjacent pairs */ readonly pairwise: () => AsyncOperator; /** * Combines the sequence with another sequence element by element * @param source Source sequence to zip with * @returns A new async operator whose values are pairs from both sequences */ readonly zip: (source: AsyncOperatorSource) => AsyncOperator; /** * Produces intermediate accumulator states, including the initial value * @param reducer Reducer function for each resolved value * @param initialValue Initial accumulator value * @returns A new async operator whose values are intermediate accumulator states */ readonly scan: (reducer: (previousValue: U, currentValue: T, index: number) => Awaitable, initialValue: U) => AsyncOperator; /** * Combines the sequence with another sequence and removes duplicate values * @param source Source sequence to union with * @returns A new async operator whose values are distinct across both sequences * @remarks * The result preserves the order of first occurrence across the left sequence and then the right sequence. */ readonly union: (source: AsyncOperatorSource) => AsyncOperator; /** * Combines the sequence with another sequence and removes duplicate values by projected key * @param source Source sequence to union with * @param selector Selector function that produces the distinct key * @returns A new async operator whose values are distinct by key across both sequences * @remarks * The result preserves the order of first occurrence across the left sequence and then the right sequence. */ readonly unionBy: (source: AsyncOperatorSource, selector: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Produces values that are present in both the current sequence and another sequence * @param source Source sequence to intersect with * @returns A new async operator whose values are distinct values shared by both sequences * @remarks * The result preserves the order of first occurrence from the left sequence. */ readonly intersect: (source: AsyncOperatorSource) => AsyncOperator; /** * Produces values that are present in both the current sequence and another sequence by projected key * @param source Source sequence to intersect with * @param selector Selector function that produces the comparison key * @returns A new async operator whose values are distinct by key and shared by both sequences * @remarks * The result preserves the order of first occurrence from the left sequence. */ readonly intersectBy: (source: AsyncOperatorSource, selector: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Produces values that are not present in another sequence * @param source Source sequence to exclude * @returns A new async operator whose values are distinct values unique to the current sequence * @remarks * The result preserves the order of first occurrence from the left sequence. */ readonly except: (source: AsyncOperatorSource) => AsyncOperator; /** * Produces values that are not present in another sequence by projected key * @param source Source sequence to exclude * @param selector Selector function that produces the comparison key * @returns A new async operator whose values are distinct by key and unique to the current sequence * @remarks * The result preserves the order of first occurrence from the left sequence. */ readonly exceptBy: (source: AsyncOperatorSource, selector: (value: T, index: number) => Awaitable) => AsyncOperator; /** * Groups resolved values into arrays of a fixed maximum size * @param size Maximum number of values in each chunk * @returns A new async operator whose values are chunk arrays * @remarks * `size` must be greater than 0. */ readonly chunkBySize: (size: number) => AsyncOperator; /** * Produces sliding windows of a fixed size * @param size Number of values in each window * @returns A new async operator whose values are window arrays * @remarks * `size` must be greater than 0. */ readonly windowed: (size: number) => AsyncOperator; /** * Flattens nested array values by the specified depth * @param depth Depth level to flatten * @returns A new async operator whose values are flattened array elements * @remarks * This follows `Array.prototype.flat()` semantics and consumes the source before producing results. */ readonly flat: { (): AsyncOperator>; (depth: Depth): AsyncOperator>; }; /** * Reverses the sequence order * @returns A new async operator whose values are in reverse order * @remarks * This consumes the source before producing results. The underlying source itself is not mutated. */ readonly reverse: () => AsyncOperator; /** * Returns a reversed copy of the sequence * @returns A new async operator whose values are in reverse order * @remarks * This consumes the source before producing results. The underlying source itself is not mutated. */ readonly toReversed: () => AsyncOperator; /** * Sorts the sequence values * @param compareFn Comparison function used to determine sort order * @returns A new async operator whose values are sorted * @remarks * This follows `Array.prototype.sort()` semantics and consumes the source before producing results. * The underlying source itself is not mutated. */ readonly sort: (compareFn?: (left: T, right: T) => number) => AsyncOperator; /** * Returns a sorted copy of the sequence * @param compareFn Comparison function used to determine sort order * @returns A new async operator whose values are sorted * @remarks * This follows `Array.prototype.toSorted()` semantics and consumes the source before producing results. * The underlying source itself is not mutated. */ readonly toSorted: (compareFn?: (left: T, right: T) => number) => AsyncOperator; /** * Executes an action for each resolved value * @param action Action function for each resolved value * @returns A promise that resolves when all values have been processed */ readonly forEach: (action: (value: T, index: number) => Awaitable) => Promise; /** * Reduces the sequence to a single value */ readonly reduce: { /** * Reduces the sequence without an explicit initial value * @param reducer Reducer function for each resolved value * @returns A promise that resolves to the reduced value */ (reducer: (previousValue: T, currentValue: T, index: number) => Awaitable): Promise; /** * Reduces the sequence with an explicit initial value * @param reducer Reducer function for each resolved value * @param initialValue Initial accumulator value * @returns A promise that resolves to the reduced value */ (reducer: (previousValue: U, currentValue: T, index: number) => Awaitable, initialValue: U): Promise; }; /** * Reduces the sequence from right to left */ readonly reduceRight: { /** * Reduces the sequence from right to left without an explicit initial value * @param reducer Reducer function for each resolved value * @returns A promise that resolves to the reduced value */ (reducer: (previousValue: T, currentValue: T, index: number) => Awaitable): Promise; /** * Reduces the sequence from right to left with an explicit initial value * @param reducer Reducer function for each resolved value * @param initialValue Initial accumulator value * @returns A promise that resolves to the reduced value */ (reducer: (previousValue: U, currentValue: T, index: number) => Awaitable, initialValue: U): Promise; }; /** * Determines whether any value satisfies the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to true when any value satisfies the predicate */ readonly some: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Determines whether all values satisfy the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to true when all values satisfy the predicate */ readonly every: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Finds the first value that satisfies the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to the found value or undefined */ readonly find: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Finds the index of the first value that satisfies the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to the found index or -1 */ readonly findIndex: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Returns the value at the specified index * @param index Zero-based index, or a negative offset from the end * @returns A promise that resolves to the value at the specified index or undefined * @remarks * This follows `Array.prototype.at()` semantics. Negative indexes may require consuming the source first. */ readonly at: (index: number) => Promise; /** * Determines whether the sequence contains the specified value * @param searchElement Value to locate in the sequence * @param fromIndex Zero-based index to start searching from * @returns A promise that resolves to true when the value is found * @remarks * This follows `Array.prototype.includes()` semantics. Negative `fromIndex` values may require consuming the source first. */ readonly includes: (searchElement: T, fromIndex?: number) => Promise; /** * Returns the first index of the specified value * @param searchElement Value to locate in the sequence * @param fromIndex Zero-based index to start searching from * @returns A promise that resolves to the found index or -1 * @remarks * This follows `Array.prototype.indexOf()` semantics. Negative `fromIndex` values may require consuming the source first. */ readonly indexOf: (searchElement: T, fromIndex?: number) => Promise; /** * Returns the last index of the specified value * @param searchElement Value to locate in the sequence * @param fromIndex Zero-based index to start searching backward from * @returns A promise that resolves to the found index or -1 * @remarks * This follows `Array.prototype.lastIndexOf()` semantics and may require consuming the source first. */ readonly lastIndexOf: (searchElement: T, fromIndex?: number) => Promise; /** * Finds the last value that satisfies the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to the found value or undefined */ readonly findLast: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Finds the index of the last value that satisfies the predicate * @param predicate Predicate function for each resolved value * @returns A promise that resolves to the found index or -1 */ readonly findLastIndex: (predicate: (value: T, index: number) => Awaitable) => Promise; /** * Finds the minimum value in the sequence * @returns A promise that resolves to the minimum value or undefined */ readonly min: () => Promise; /** * Finds the value with the minimum projected key * @param selector Selector function that produces the comparison key * @returns A promise that resolves to the value with the minimum key or undefined */ readonly minBy: (selector: (value: T, index: number) => Awaitable) => Promise; /** * Finds the maximum value in the sequence * @returns A promise that resolves to the maximum value or undefined */ readonly max: () => Promise; /** * Finds the value with the maximum projected key * @param selector Selector function that produces the comparison key * @returns A promise that resolves to the value with the maximum key or undefined */ readonly maxBy: (selector: (value: T, index: number) => Awaitable) => Promise; /** * Groups values by projected key * @param selector Selector function that produces the grouping key * @returns A promise that resolves to grouped values */ readonly groupBy: (selector: (value: T, index: number) => Awaitable) => Promise>; /** * Counts values by projected key * @param selector Selector function that produces the counting key * @returns A promise that resolves to counts grouped by key */ readonly countBy: (selector: (value: T, index: number) => Awaitable) => Promise>; /** * Concatenates the resolved values into a string * @param separator String used to separate adjacent values * @returns A promise that resolves to the concatenated string * @remarks * `null` and `undefined` values contribute empty strings, matching `Array.prototype.join`. */ readonly join: (separator?: string) => Promise; /** * Resolves the sequence into an array * @returns A promise that resolves to an array of values in input order */ readonly toArray: () => Promise; } /** @deprecated Use `Mutex` instead */ export type AsyncLock = Mutex; /** @deprecated Use `Conditional` instead */ export type Signal = Conditional; /** @deprecated Use `ManuallyConditional` instead */ export type ManuallySignal = ManuallyConditional; /** @deprecated Use `LockHandle` instead */ export type SemaphoreHandle = LockHandle; /** @deprecated Use `LockHandle` instead */ export type ReadLockHandle = LockHandle; /** @deprecated Use `LockHandle` instead */ export type WriteLockHandle = LockHandle; //# sourceMappingURL=types.d.ts.map