UNPKG

@idealic/poker-engine

Version:

Professional poker game engine and hand evaluator with built-in iterator utilities

98 lines 3.02 kB
import { pubsub } from './lib/pubsub'; /** * Sequentially accumulates all items from an iterable into an array */ async function _accumulate(iterable) { const result = []; for await (const item of iterable) { result.push(item); } return result; } /** * Concurrently accumulates all items from an iterable into an array * @param iterable Source iterable to accumulate * @param concurrency Maximum number of concurrent operations */ async function _accumulateConcurrently(iterable, concurrency) { const result = []; const { publish, consume, producing, wait, output: loop, onReadComplete: end, } = pubsub(concurrency); let isDone = false; // Process the input stream without blocking the main loop (async () => { try { for await (const item of iterable) { if (producing.size >= concurrency) { await wait(); } publish(item); } } finally { console.log('accumulate input done', isDone); end(); } })(); for await (const item of loop()) { result.push(item); } return result; } /** * Processes an iterable with concurrency and yields each item as it completes * @param iterable Source iterable to process * @param concurrency Maximum number of concurrent operations */ async function* _drainConcurrently(iterable, concurrency) { const { publish, consume, producing, wait } = pubsub(concurrency); let isDone = false; // Process the input stream without blocking the main loop (async () => { try { for await (const item of iterable) { if (producing.size >= concurrency) { await wait(); } publish(item); } } finally { isDone = true; } })(); // Yield items as they become available while (!isDone || producing.size) { yield await consume(); } } /** * Sequentially processes an iterable and yields each item */ async function* _drain(iterable) { for await (const item of iterable) { yield item; } } export function accumulate(iterableOrConcurrency, concurrency) { if (typeof iterableOrConcurrency === 'number') { return (iterable) => accumulate(iterable, iterableOrConcurrency); } else if (concurrency && concurrency > 1) { return _accumulateConcurrently(iterableOrConcurrency, concurrency); } else { return _accumulate(iterableOrConcurrency); } } export function drain(iterableOrConcurrency, concurrency) { if (typeof iterableOrConcurrency === 'number') { return (iterable) => drain(iterable, iterableOrConcurrency); } else if (concurrency && concurrency > 1) { return _drainConcurrently(iterableOrConcurrency, concurrency); } else { return _drain(iterableOrConcurrency); } } //# sourceMappingURL=accumulate.js.map