-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathstreaming.mjs
More file actions
82 lines (67 loc) · 2.12 KB
/
streaming.mjs
File metadata and controls
82 lines (67 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
import logger from './logger/index.mjs';
const streamingLogger = logger.child('streaming');
/**
* Checks if a value is an async generator/iterable.
*
* @param {unknown} obj - Value to check
* @returns {obj is AsyncGenerator} True if the value is an async iterable
*/
export const isAsyncGenerator = obj =>
obj !== null &&
typeof obj === 'object' &&
typeof obj[Symbol.asyncIterator] === 'function';
/**
* Collects all values from an async generator into a flat array.
* Each yielded chunk is spread into the results array.
*
* @template T
* @param {AsyncGenerator<T[]>} generator - Async generator yielding arrays
* @returns {Promise<T[]>} Flattened array of all yielded items
*/
export const collectAsyncGenerator = async generator => {
const results = [];
let chunkCount = 0;
for await (const chunk of generator) {
chunkCount++;
results.push(...chunk);
streamingLogger.debug(`Collected chunk ${chunkCount}`, {
itemsInChunk: chunk.length,
});
}
streamingLogger.debug(`Collection complete`, {
totalItems: results.length,
chunks: chunkCount,
});
return results;
};
/**
* Creates a cache for async generator collection results.
* Ensures that when multiple consumers request the same async generator,
* only one collection happens and all consumers share the result.
*/
export const createStreamingCache = () => {
/** @type {Map<string, Promise<unknown[]>>} */
const cache = new Map();
return {
/**
* Gets the collected result for a generator, starting collection if needed.
*
* @param {string} key - Cache key (usually generator name)
* @param {AsyncGenerator<unknown[]>} generator - The async generator to collect
* @returns {Promise<unknown[]>} Promise resolving to collected results
*/
getOrCollect(key, generator) {
const hasKey = cache.has(key);
if (!hasKey) {
cache.set(key, collectAsyncGenerator(generator));
}
streamingLogger.debug(
hasKey
? `Using cached result for "${key}"`
: `Starting collection for "${key}"`
);
return cache.get(key);
},
};
};