evnty 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +28 -28
  2. package/build/async.cjs +101 -0
  3. package/build/async.cjs.map +1 -0
  4. package/build/async.d.ts +37 -0
  5. package/build/async.js +83 -0
  6. package/build/async.js.map +1 -0
  7. package/build/broadcast.cjs +205 -0
  8. package/build/broadcast.cjs.map +1 -0
  9. package/build/broadcast.d.ts +164 -0
  10. package/build/broadcast.js +184 -0
  11. package/build/broadcast.js.map +1 -0
  12. package/build/dispatch-result.cjs +154 -0
  13. package/build/dispatch-result.cjs.map +1 -0
  14. package/build/dispatch-result.d.ts +49 -0
  15. package/build/dispatch-result.js +127 -0
  16. package/build/dispatch-result.js.map +1 -0
  17. package/build/event.cjs +92 -127
  18. package/build/event.cjs.map +1 -1
  19. package/build/event.d.ts +92 -167
  20. package/build/event.js +90 -122
  21. package/build/event.js.map +1 -1
  22. package/build/index.cjs +3 -1
  23. package/build/index.cjs.map +1 -1
  24. package/build/index.d.ts +3 -1
  25. package/build/index.js +3 -1
  26. package/build/index.js.map +1 -1
  27. package/build/iterator.cjs +578 -91
  28. package/build/iterator.cjs.map +1 -1
  29. package/build/iterator.d.ts +178 -7
  30. package/build/iterator.js +579 -92
  31. package/build/iterator.js.map +1 -1
  32. package/build/listener-registry.cjs +114 -0
  33. package/build/listener-registry.cjs.map +1 -0
  34. package/build/listener-registry.d.ts +54 -0
  35. package/build/listener-registry.js +104 -0
  36. package/build/listener-registry.js.map +1 -0
  37. package/build/ring-buffer.cjs +171 -0
  38. package/build/ring-buffer.cjs.map +1 -0
  39. package/build/ring-buffer.d.ts +80 -0
  40. package/build/ring-buffer.js +161 -0
  41. package/build/ring-buffer.js.map +1 -0
  42. package/build/sequence.cjs +34 -35
  43. package/build/sequence.cjs.map +1 -1
  44. package/build/sequence.d.ts +38 -24
  45. package/build/sequence.js +34 -35
  46. package/build/sequence.js.map +1 -1
  47. package/build/signal.cjs +26 -35
  48. package/build/signal.cjs.map +1 -1
  49. package/build/signal.d.ts +36 -39
  50. package/build/signal.js +26 -35
  51. package/build/signal.js.map +1 -1
  52. package/build/types.cjs +0 -11
  53. package/build/types.cjs.map +1 -1
  54. package/build/types.d.ts +86 -9
  55. package/build/types.js +1 -5
  56. package/build/types.js.map +1 -1
  57. package/build/utils.cjs +202 -22
  58. package/build/utils.cjs.map +1 -1
  59. package/build/utils.d.ts +85 -26
  60. package/build/utils.js +181 -22
  61. package/build/utils.js.map +1 -1
  62. package/package.json +27 -25
  63. package/src/__tests__/example.js +19 -24
  64. package/src/index.ts +3 -1
  65. package/build/callable.cjs +0 -72
  66. package/build/callable.cjs.map +0 -1
  67. package/build/callable.d.ts +0 -34
  68. package/build/callable.js +0 -51
  69. package/build/callable.js.map +0 -1
package/build/types.d.ts CHANGED
@@ -1,44 +1,121 @@
1
+ export interface Action {
2
+ (): void;
3
+ }
4
+ /**
5
+ * Generic function type with typed arguments and return value.
6
+ * @template A - Tuple of argument types
7
+ * @template R - Return type
8
+ */
1
9
  export interface Fn<A extends unknown[], R> {
2
10
  (...args: A): R;
3
11
  }
4
- export type MaybePromise<T> = Promise<T> | PromiseLike<T> | T;
12
+ /**
13
+ * A value that may be a PromiseLike or a plain value.
14
+ * @template T - The underlying value type
15
+ */
16
+ export type MaybePromise<T> = T | PromiseLike<T>;
17
+ /**
18
+ * Union of sync and async iterable types.
19
+ * @template T - The yielded value type
20
+ * @template TReturn - The return value type
21
+ * @template TNext - The type passed to next()
22
+ */
5
23
  export type AnyIterable<T, TReturn, TNext> = Iterable<T, TReturn, TNext> | AsyncIterable<T, TReturn, TNext>;
24
+ /**
25
+ * Union of sync and async iterator types.
26
+ * @template T - The yielded value type
27
+ * @template TReturn - The return value type
28
+ * @template TNext - The type passed to next()
29
+ */
30
+ export type AnyIterator<T, TReturn, TNext> = Iterator<T, TReturn, TNext> | AsyncIterator<T, TReturn, TNext>;
31
+ /**
32
+ * A zero-argument callback that may return a value or promise.
33
+ * @template R - The return type (defaults to void)
34
+ */
6
35
  export interface Callback<R = void> extends Fn<[], MaybePromise<R>> {
7
36
  }
8
- export interface Listener<T, R = unknown> extends Fn<[T], MaybePromise<R | void>> {
9
- }
10
- export declare enum HookType {
11
- Add = 0,
12
- Remove = 1
37
+ /**
38
+ * An event listener function that receives a value and optionally returns a result.
39
+ * @template T - The event payload type
40
+ * @template R - The return type (defaults to unknown)
41
+ */
42
+ export interface Listener<T, R = unknown> extends Fn<[T], R | void> {
13
43
  }
14
- export interface HookListener<T, R> {
15
- (listener: Listener<T, R> | undefined, type: HookType): void;
44
+ export interface Emitter<T, R> {
45
+ readonly sink: Fn<[T], R>;
46
+ emit(value: T): R;
16
47
  }
48
+ /**
49
+ * A filter function that receives both value and index.
50
+ * @template T - The value type to filter
51
+ */
17
52
  export interface FilterIndexFunction<T> {
18
53
  (value: T, index: number): boolean;
19
54
  }
55
+ /**
56
+ * A simple filter function that tests a value.
57
+ * @template T - The value type to filter
58
+ */
20
59
  export interface FilterFunction<T> {
21
60
  (value: T): boolean;
22
61
  }
62
+ /**
63
+ * An async filter function that may return a promise.
64
+ * @template T - The value type to filter
65
+ */
23
66
  export interface AsyncFilterFunction<T> {
24
67
  (value: T): MaybePromise<boolean>;
25
68
  }
69
+ /**
70
+ * A type guard predicate for narrowing types.
71
+ * @template T - The input type
72
+ * @template P - The narrowed output type
73
+ */
26
74
  export interface Predicate<T, P extends T> {
27
75
  (value: T): value is P;
28
76
  }
77
+ /**
78
+ * Union of filter function types including predicates.
79
+ * @template T - The value type to filter
80
+ * @template P - The narrowed type for predicates
81
+ */
29
82
  export type Filter<T, P extends T> = Predicate<T, P> | FilterFunction<T> | AsyncFilterFunction<T>;
83
+ /**
84
+ * A function that transforms a value to another type.
85
+ * @template T - The input type
86
+ * @template R - The output type
87
+ */
30
88
  export interface Mapper<T, R> {
31
89
  (value: T): MaybePromise<R>;
32
90
  }
91
+ /**
92
+ * A function that produces an async generator from a value.
93
+ * @template T - The input type
94
+ * @template R - The yielded value type
95
+ */
33
96
  export interface AsyncGenerable<T, R> {
34
97
  (value: T): AsyncGenerator<R, void, unknown>;
35
98
  }
99
+ /**
100
+ * A reducer function for accumulating values.
101
+ * @template T - The value type being reduced
102
+ * @template R - The accumulator type
103
+ */
36
104
  export interface Reducer<T, R> {
37
105
  (result: R, value: T): MaybePromise<R>;
38
106
  }
107
+ /**
108
+ * A function that expands a value into another form.
109
+ * @template T - The input type
110
+ * @template R - The output type
111
+ */
39
112
  export interface Expander<T, R> {
40
113
  (value: T): MaybePromise<R>;
41
114
  }
115
+ /**
116
+ * An object that provides a receive() method returning a promise.
117
+ * @template T - The resolved value type
118
+ */
42
119
  export interface Promiseable<T> {
43
- next(): Promise<T>;
120
+ receive(): Promise<T>;
44
121
  }
package/build/types.js CHANGED
@@ -1,7 +1,3 @@
1
- export var HookType = /*#__PURE__*/ function(HookType) {
2
- HookType[HookType["Add"] = 0] = "Add";
3
- HookType[HookType["Remove"] = 1] = "Remove";
4
- return HookType;
5
- }({});
1
+ export { };
6
2
 
7
3
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export interface Fn<A extends unknown[], R> {\n (...args: A): R;\n}\n\nexport type MaybePromise<T> = Promise<T> | PromiseLike<T> | T;\n\nexport type AnyIterable<T, TReturn, TNext> = Iterable<T, TReturn, TNext> | AsyncIterable<T, TReturn, TNext>;\n\nexport interface Callback<R = void> extends Fn<[], MaybePromise<R>> {}\n\nexport interface Listener<T, R = unknown> extends Fn<[T], MaybePromise<R | void>> {}\n\nexport enum HookType {\n Add,\n Remove,\n}\n\nexport interface HookListener<T, R> {\n (listener: Listener<T, R> | undefined, type: HookType): void;\n}\n\nexport interface FilterIndexFunction<T> {\n (value: T, index: number): boolean;\n}\n\nexport interface FilterFunction<T> {\n (value: T): boolean;\n}\n\nexport interface AsyncFilterFunction<T> {\n (value: T): MaybePromise<boolean>;\n}\n\nexport interface Predicate<T, P extends T> {\n (value: T): value is P;\n}\n\nexport type Filter<T, P extends T> = Predicate<T, P> | FilterFunction<T> | AsyncFilterFunction<T>;\n\nexport interface Mapper<T, R> {\n (value: T): MaybePromise<R>;\n}\n\nexport interface AsyncGenerable<T, R> {\n (value: T): AsyncGenerator<R, void, unknown>;\n}\n\nexport interface Reducer<T, R> {\n (result: R, value: T): MaybePromise<R>;\n}\n\nexport interface Expander<T, R> {\n (value: T): MaybePromise<R>;\n}\n\nexport interface Promiseable<T> {\n next(): Promise<T>;\n}\n"],"names":["HookType"],"mappings":"AAYA,OAAO,IAAA,AAAKA,kCAAAA;;;WAAAA;MAGX"}
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export interface Action {\n (): void;\n}\n\n/**\n * Generic function type with typed arguments and return value.\n * @template A - Tuple of argument types\n * @template R - Return type\n */\nexport interface Fn<A extends unknown[], R> {\n (...args: A): R;\n}\n\n/**\n * A value that may be a PromiseLike or a plain value.\n * @template T - The underlying value type\n */\nexport type MaybePromise<T> = T | PromiseLike<T>;\n\n/**\n * Union of sync and async iterable types.\n * @template T - The yielded value type\n * @template TReturn - The return value type\n * @template TNext - The type passed to next()\n */\nexport type AnyIterable<T, TReturn, TNext> = Iterable<T, TReturn, TNext> | AsyncIterable<T, TReturn, TNext>;\n\n/**\n * Union of sync and async iterator types.\n * @template T - The yielded value type\n * @template TReturn - The return value type\n * @template TNext - The type passed to next()\n */\nexport type AnyIterator<T, TReturn, TNext> = Iterator<T, TReturn, TNext> | AsyncIterator<T, TReturn, TNext>;\n\n/**\n * A zero-argument callback that may return a value or promise.\n * @template R - The return type (defaults to void)\n */\nexport interface Callback<R = void> extends Fn<[], MaybePromise<R>> {}\n\n/**\n * An event listener function that receives a value and optionally returns a result.\n * @template T - The event payload type\n * @template R - The return type (defaults to unknown)\n */\nexport interface Listener<T, R = unknown> extends Fn<[T], R | void> {}\n\nexport interface Emitter<T, R> {\n readonly sink: Fn<[T], R>;\n\n emit(value: T): R;\n}\n\n/**\n * A filter function that receives both value and index.\n * @template T - The value type to filter\n */\nexport interface FilterIndexFunction<T> {\n (value: T, index: number): boolean;\n}\n\n/**\n * A simple filter function that tests a value.\n * @template T - The value type to filter\n */\nexport interface FilterFunction<T> {\n (value: T): boolean;\n}\n\n/**\n * An async filter function that may return a promise.\n * @template T - The value type to filter\n */\nexport interface AsyncFilterFunction<T> {\n (value: T): MaybePromise<boolean>;\n}\n\n/**\n * A type guard predicate for narrowing types.\n * @template T - The input type\n * @template P - The narrowed output type\n */\nexport interface Predicate<T, P extends T> {\n (value: T): value is P;\n}\n\n/**\n * Union of filter function types including predicates.\n * @template T - The value type to filter\n * @template P - The narrowed type for predicates\n */\nexport type Filter<T, P extends T> = Predicate<T, P> | FilterFunction<T> | AsyncFilterFunction<T>;\n\n/**\n * A function that transforms a value to another type.\n * @template T - The input type\n * @template R - The output type\n */\nexport interface Mapper<T, R> {\n (value: T): MaybePromise<R>;\n}\n\n/**\n * A function that produces an async generator from a value.\n * @template T - The input type\n * @template R - The yielded value type\n */\nexport interface AsyncGenerable<T, R> {\n (value: T): AsyncGenerator<R, void, unknown>;\n}\n\n/**\n * A reducer function for accumulating values.\n * @template T - The value type being reduced\n * @template R - The accumulator type\n */\nexport interface Reducer<T, R> {\n (result: R, value: T): MaybePromise<R>;\n}\n\n/**\n * A function that expands a value into another form.\n * @template T - The input type\n * @template R - The output type\n */\nexport interface Expander<T, R> {\n (value: T): MaybePromise<R>;\n}\n\n/**\n * An object that provides a receive() method returning a promise.\n * @template T - The resolved value type\n */\nexport interface Promiseable<T> {\n receive(): Promise<T>;\n}\n"],"names":[],"mappings":"AAsIA,WAEC"}
package/build/utils.cjs CHANGED
@@ -9,9 +9,30 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
+ get MapIteratorType () {
13
+ return MapIteratorType;
14
+ },
15
+ get abortableIterable () {
16
+ return abortableIterable;
17
+ },
18
+ get isThenable () {
19
+ return isThenable;
20
+ },
12
21
  get iterate () {
13
22
  return iterate;
14
23
  },
24
+ get mapIterator () {
25
+ return mapIterator;
26
+ },
27
+ get mergeIterables () {
28
+ return mergeIterables;
29
+ },
30
+ get min () {
31
+ return min;
32
+ },
33
+ get noop () {
34
+ return noop;
35
+ },
15
36
  get pipe () {
16
37
  return pipe;
17
38
  },
@@ -22,25 +43,110 @@ _export(exports, {
22
43
  return toAsyncIterable;
23
44
  }
24
45
  });
25
- const iterate = (...args)=>{
26
- let start, count, step;
27
- if (args.length === 0) {
28
- start = 0;
29
- count = Infinity;
30
- step = 1;
31
- } else if (args.length === 1) {
32
- start = 0;
33
- count = args[0];
34
- step = 1;
35
- } else if (args.length === 2) {
36
- start = args[0];
37
- count = args[1];
38
- step = 1;
46
+ const _sequencecjs = require("./sequence.cjs");
47
+ function isThenable(value) {
48
+ return value !== null && typeof value === 'object' && typeof value.then === 'function';
49
+ }
50
+ const noop = ()=>{};
51
+ function min(values, fallback) {
52
+ let result = Infinity;
53
+ for (const value of values){
54
+ if (value < result) result = value;
55
+ }
56
+ return result === Infinity ? fallback : result;
57
+ }
58
+ var MapIteratorType = /*#__PURE__*/ function(MapIteratorType) {
59
+ MapIteratorType[MapIteratorType["NEXT"] = 0] = "NEXT";
60
+ MapIteratorType[MapIteratorType["RETURN"] = 1] = "RETURN";
61
+ MapIteratorType[MapIteratorType["THROW"] = 2] = "THROW";
62
+ return MapIteratorType;
63
+ }({});
64
+ const mapIterator = (iterator, map)=>{
65
+ const subIterator = {
66
+ next: async (...args)=>{
67
+ const result = await iterator.next(...args);
68
+ return map(result, 0);
69
+ }
70
+ };
71
+ if (iterator.return) {
72
+ subIterator.return = async (...args)=>{
73
+ const result = await iterator.return(...args);
74
+ return map(result, 1);
75
+ };
39
76
  } else {
40
- start = args[0];
41
- count = args[1];
42
- step = args[2];
77
+ subIterator.return = async (value)=>{
78
+ return map({
79
+ done: true,
80
+ value
81
+ }, 1);
82
+ };
43
83
  }
84
+ if (iterator.throw) {
85
+ subIterator.throw = async (...args)=>{
86
+ const result = await iterator.throw(...args);
87
+ return map(result, 2);
88
+ };
89
+ }
90
+ return subIterator;
91
+ };
92
+ function abortableIterable(iterable, signal) {
93
+ return {
94
+ [Symbol.asyncIterator] () {
95
+ const iterator = iterable[Symbol.asyncIterator]();
96
+ const { promise, resolve } = Promise.withResolvers();
97
+ const onAbort = ()=>resolve();
98
+ let closed = false;
99
+ const finish = (value)=>{
100
+ if (closed) {
101
+ return Promise.resolve({
102
+ done: true,
103
+ value: value
104
+ });
105
+ }
106
+ closed = true;
107
+ signal.removeEventListener('abort', onAbort);
108
+ return iterator.return?.(value) ?? Promise.resolve({
109
+ done: true,
110
+ value: value
111
+ });
112
+ };
113
+ if (signal.aborted) {
114
+ onAbort();
115
+ } else {
116
+ signal.addEventListener('abort', onAbort);
117
+ }
118
+ const race = [
119
+ promise,
120
+ undefined
121
+ ];
122
+ return {
123
+ async next (...args) {
124
+ race[1] = iterator.next(...args);
125
+ const result = await Promise.race(race);
126
+ if (result === undefined) {
127
+ void finish();
128
+ return {
129
+ done: true,
130
+ value: undefined
131
+ };
132
+ }
133
+ if (result.done) {
134
+ closed = true;
135
+ signal.removeEventListener('abort', onAbort);
136
+ }
137
+ return result;
138
+ },
139
+ async return (value) {
140
+ return finish(value);
141
+ }
142
+ };
143
+ }
144
+ };
145
+ }
146
+ const iterate = (startOrCount, countWhenTwoArgs, step = 1)=>{
147
+ const hasStartArg = countWhenTwoArgs !== undefined;
148
+ const start = hasStartArg ? startOrCount : 0;
149
+ const count = startOrCount === undefined ? Infinity : hasStartArg ? countWhenTwoArgs : startOrCount;
44
150
  return {
45
151
  [Symbol.iterator] () {
46
152
  let idx = 0;
@@ -114,16 +220,90 @@ const toAsyncIterable = (iterable)=>{
114
220
  }
115
221
  };
116
222
  };
223
+ const isAsyncIterable = (value)=>{
224
+ return typeof value[Symbol.asyncIterator] === 'function';
225
+ };
117
226
  async function* pipe(iterable, generatorFactory, signal) {
227
+ const source = signal ? abortableIterable(iterable, signal) : iterable;
118
228
  const generator = generatorFactory();
119
- for await (const value of iterable){
120
- if (signal?.aborted) return;
121
- for await (const subValue of generator(value)){
229
+ for await (const value of source){
230
+ const produced = generator(value);
231
+ const subIterable = isAsyncIterable(produced) ? produced : toAsyncIterable(produced);
232
+ const abortableSub = signal ? abortableIterable(subIterable, signal) : subIterable;
233
+ for await (const subValue of abortableSub){
122
234
  yield subValue;
123
- if (signal?.aborted) return;
124
235
  }
125
- if (signal?.aborted) return;
126
236
  }
127
237
  }
238
+ const mergeIterables = (...iterables)=>{
239
+ return {
240
+ [Symbol.asyncIterator] () {
241
+ if (iterables.length === 0) {
242
+ return {
243
+ next: async ()=>({
244
+ value: undefined,
245
+ done: true
246
+ })
247
+ };
248
+ }
249
+ const exit = Symbol('mergeIterables.exit');
250
+ const ctrl = new AbortController();
251
+ const sequence = new _sequencecjs.Sequence(ctrl.signal);
252
+ let remaining = iterables.length;
253
+ const pump = async (iterable)=>{
254
+ try {
255
+ for await (const value of abortableIterable(iterable, ctrl.signal)){
256
+ if (!sequence.emit(value)) {
257
+ break;
258
+ }
259
+ }
260
+ } catch (error) {
261
+ ctrl.abort(error);
262
+ } finally{
263
+ remaining -= 1;
264
+ if (remaining === 0 && !ctrl.signal.aborted) {
265
+ ctrl.abort(exit);
266
+ }
267
+ }
268
+ };
269
+ for (const iterable of iterables){
270
+ void pump(iterable);
271
+ }
272
+ return {
273
+ next: async ()=>{
274
+ try {
275
+ const value = await sequence.receive();
276
+ return {
277
+ value,
278
+ done: false
279
+ };
280
+ } catch {
281
+ if (ctrl.signal.aborted && ctrl.signal.reason === exit) {
282
+ return {
283
+ value: undefined,
284
+ done: true
285
+ };
286
+ }
287
+ throw ctrl.signal.reason;
288
+ }
289
+ },
290
+ return: async ()=>{
291
+ ctrl.abort(exit);
292
+ return {
293
+ value: undefined,
294
+ done: true
295
+ };
296
+ },
297
+ throw: async (error)=>{
298
+ ctrl.abort(error);
299
+ return {
300
+ value: undefined,
301
+ done: true
302
+ };
303
+ }
304
+ };
305
+ }
306
+ };
307
+ };
128
308
 
129
309
  //# sourceMappingURL=utils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils.ts"],"sourcesContent":["/**\n * Interface for creating iterable number sequences with various parameter combinations.\n * Supports infinite sequences, counted sequences, and sequences with custom start and step values.\n */\nexport interface Iterate {\n (): Iterable<number, void, unknown>;\n (count: number): Iterable<number, void, unknown>;\n (start: number, count: number): Iterable<number, void, unknown>;\n (start: number, count: number, step: number): Iterable<number, void, unknown>;\n}\n\n/**\n * Creates an iterable sequence of numbers with flexible parameters.\n * Can generate infinite sequences, finite sequences, or sequences with custom start and step values.\n *\n * @param args Variable arguments to configure the sequence:\n * - No args: Infinite sequence starting at 0 with step 1\n * - 1 arg (count): Sequence from 0 to count-1\n * - 2 args (start, count): Sequence starting at 'start' for 'count' iterations\n * - 3 args (start, count, step): Custom start, count, and step value\n * @returns An iterable that generates numbers according to the parameters\n *\n * @example\n * ```typescript\n * // Infinite sequence: 0, 1, 2, 3, ...\n * for (const n of iterate()) { }\n *\n * // Count only: 0, 1, 2, 3, 4\n * for (const n of iterate(5)) { }\n *\n * // Start and count: 10, 11, 12, 13, 14\n * for (const n of iterate(10, 5)) { }\n *\n * // Start, count, and step: 0, 2, 4, 6, 8\n * for (const n of iterate(0, 5, 2)) { }\n * ```\n */\nexport const iterate: Iterate = (...args: number[]): Iterable<number, void, unknown> => {\n let start: number, count: number, step: number;\n\n if (args.length === 0) {\n start = 0;\n count = Infinity;\n step = 1;\n } else if (args.length === 1) {\n start = 0;\n count = args[0]!;\n step = 1;\n } else if (args.length === 2) {\n start = args[0]!;\n count = args[1]!;\n step = 1;\n } else {\n start = args[0]!;\n count = args[1]!;\n step = args[2]!;\n }\n\n return {\n [Symbol.iterator]() {\n let idx = 0;\n let current = start;\n return {\n next() {\n if (idx < count) {\n const value = current;\n current += step;\n idx++;\n return { value, done: false };\n }\n return { value: undefined, done: true };\n },\n return(value) {\n idx = count;\n return { value, done: true };\n },\n throw(error?: unknown) {\n idx = count;\n throw error;\n },\n } satisfies Iterator<number, void, unknown>;\n },\n };\n};\n\n/**\n * @internal\n * Creates a promise that resolves after a specified timeout. If an `AbortSignal` is provided and triggered,\n * the timeout is cleared, and the promise resolves to `false`.\n *\n * @param {number} timeout - The time in milliseconds to wait before resolving the promise.\n * @param {AbortSignal} [signal] - An optional `AbortSignal` that can abort the timeout.\n * @returns {Promise<boolean>} A promise that resolves to `true` if the timeout completed, or `false` if it was aborted.\n *\n * @example\n * ```typescript\n * const controller = new AbortController();\n * setTimeout(() => controller.abort(), 500);\n * const result = await setTimeoutAsync(1000, controller.signal);\n * console.log(result); // false\n * ```\n */\nexport const setTimeoutAsync = async (timeout: number, signal?: AbortSignal): Promise<boolean> => {\n if (signal?.aborted) {\n return false;\n }\n const { promise, resolve } = Promise.withResolvers<boolean>();\n const timerId = setTimeout(resolve, timeout, true);\n const onAbort = () => {\n clearTimeout(timerId);\n resolve(false);\n };\n signal?.addEventListener('abort', onAbort);\n\n return promise.finally(() => signal?.removeEventListener('abort', onAbort));\n};\n\n/**\n * Converts a synchronous iterable to an asynchronous iterable.\n * Wraps the sync iterator methods to return promises, enabling uniform async handling.\n *\n * @template T The type of values yielded by the iterator\n * @template TReturn The return type of the iterator\n * @template TNext The type of value that can be passed to next()\n * @param iterable A synchronous iterable to convert\n * @returns An async iterable that yields the same values as the input\n *\n * @example\n * ```typescript\n * const syncArray = [1, 2, 3, 4, 5];\n * const asyncIterable = toAsyncIterable(syncArray);\n *\n * for await (const value of asyncIterable) {\n * console.log(value); // 1, 2, 3, 4, 5\n * }\n * ```\n */\nexport const toAsyncIterable = <T, TReturn, TNext>(iterable: Iterable<T, TReturn, TNext>): AsyncIterable<T, TReturn, TNext> => {\n return {\n [Symbol.asyncIterator]() {\n const iterator = iterable[Symbol.iterator]();\n return {\n async next(...args: [TNext] | []) {\n return iterator.next(...args);\n },\n async return(maybeValue) {\n const value = await maybeValue;\n return iterator.return?.(value) ?? ({ value, done: true } as IteratorResult<T, TReturn>);\n },\n async throw(error) {\n if (iterator.throw) {\n return iterator.throw(error);\n }\n throw error;\n },\n } satisfies AsyncIterator<T, TReturn, TNext>;\n },\n };\n};\n\n/**\n * Pipes values from an async iterable through a generator transformation.\n * Applies a generator function to each value, yielding all resulting values.\n * Supports cancellation via AbortSignal for early termination.\n *\n * @template T The input value type\n * @template U The output value type\n * @param iterable The source async iterable\n * @param generatorFactory A factory that returns a generator function for transforming values\n * @param signal Optional AbortSignal to cancel the operation\n * @returns An async generator yielding transformed values\n *\n * @example\n * ```typescript\n * async function* source() {\n * yield 1; yield 2; yield 3;\n * }\n *\n * const doubled = pipe(source(), () => async function*(n) {\n * yield n * 2;\n * });\n *\n * for await (const value of doubled) {\n * console.log(value); // 2, 4, 6\n * }\n * ```\n */\nexport async function* pipe<T, U>(\n iterable: AsyncIterable<T>,\n generatorFactory: () => (value: T) => AsyncIterable<U>,\n signal?: AbortSignal,\n): AsyncGenerator<Awaited<U>, void, unknown> {\n const generator = generatorFactory();\n for await (const value of iterable) {\n if (signal?.aborted) return;\n for await (const subValue of generator(value)) {\n yield subValue;\n if (signal?.aborted) return;\n }\n if (signal?.aborted) return;\n }\n}\n"],"names":["iterate","pipe","setTimeoutAsync","toAsyncIterable","args","start","count","step","length","Infinity","Symbol","iterator","idx","current","next","value","done","undefined","return","throw","error","timeout","signal","aborted","promise","resolve","Promise","withResolvers","timerId","setTimeout","onAbort","clearTimeout","addEventListener","finally","removeEventListener","iterable","asyncIterator","maybeValue","generatorFactory","generator","subValue"],"mappings":";;;;;;;;;;;QAqCaA;eAAAA;;QAsJUC;eAAAA;;QArFVC;eAAAA;;QAmCAC;eAAAA;;;AApGN,MAAMH,UAAmB,CAAC,GAAGI;IAClC,IAAIC,OAAeC,OAAeC;IAElC,IAAIH,KAAKI,MAAM,KAAK,GAAG;QACrBH,QAAQ;QACRC,QAAQG;QACRF,OAAO;IACT,OAAO,IAAI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AO;gBACL,MAAMG,MAAK,GAAGV,IAAkB;oBAC9B,OAAOO,SAASG,IAAI,IAAIV;gBAC1B;gBACA,MAAMc,QAAOmB,UAAU;oBACrB,MAAMtB,QAAQ,MAAMsB;oBACpB,OAAO1B,SAASO,MAAM,GAAGH,UAAW;wBAAEA;wBAAOC,MAAM;oBAAK;gBAC1D;gBACA,MAAMG,OAAMC,KAAK;oBACf,IAAIT,SAASQ,KAAK,EAAE;wBAClB,OAAOR,SAASQ,KAAK,CAACC;oBACxB;oBACA,MAAMA;gBACR;YACF;QACF;IACF;AACF;AA6BO,gBAAgBnB,KACrBkC,QAA0B,EAC1BG,gBAAsD,EACtDhB,MAAoB;IAEpB,MAAMiB,YAAYD;IAClB,WAAW,MAAMvB,SAASoB,SAAU;QAClC,IAAIb,QAAQC,SAAS;QACrB,WAAW,MAAMiB,YAAYD,UAAUxB,OAAQ;YAC7C,MAAMyB;YACN,IAAIlB,QAAQC,SAAS;QACvB;QACA,IAAID,QAAQC,SAAS;IACvB;AACF"}
1
+ {"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { Sequence } from './sequence.js';\nimport { AnyIterator, AnyIterable, MaybePromise } from './types.js';\n\n/**\n * @internal\n */\nexport function isThenable(value: unknown): value is PromiseLike<unknown> {\n return value !== null && typeof value === 'object' && typeof (value as PromiseLike<unknown>).then === 'function';\n}\n\n/**\n * @internal\n * A no-operation function. Useful as a default callback or placeholder.\n */\nexport const noop = () => {};\n\n/**\n * @internal\n * Returns the minimum value from an iterable, or a fallback if empty.\n */\nexport function min(values: Iterable<number>, fallback: number): number {\n let result = Infinity;\n for (const value of values) {\n if (value < result) result = value;\n }\n return result === Infinity ? fallback : result;\n}\n\n/**\n * @internal\n * Indicates which iterator method triggered a mapping operation.\n */\nexport enum MapIteratorType {\n /** The next() method was called */\n NEXT,\n /** The return() method was called */\n RETURN,\n /** The throw() method was called */\n THROW,\n}\n\n/**\n * @internal\n * A mapping function for transforming iterator results.\n * @template T - The input value type\n * @template U - The output value type\n * @template TReturn - The iterator return type\n */\nexport interface MapNext<T, U, TReturn> {\n (result: MaybePromise<IteratorResult<T, TReturn>>, type: MapIteratorType): MaybePromise<IteratorResult<U, TReturn>>;\n}\n\n/**\n * @internal\n * Wraps an iterator with a mapping function applied to each result.\n * @template U - The output value type\n * @template T - The input value type\n * @template TReturn - The iterator return type\n * @template TNext - The type passed to next()\n * @param iterator - The source iterator to wrap\n * @param map - The mapping function to apply to each result\n * @returns An async iterator with mapped results\n */\nexport const mapIterator = <U, T, TReturn, TNext>(iterator: AnyIterator<T, TReturn, TNext>, map: MapNext<T, U, TReturn>): AsyncIterator<U, TReturn, TNext> => {\n const subIterator: AsyncIterator<U, TReturn, TNext> = {\n next: async (...args: [] | [TNext]) => {\n const result = await iterator.next(...args);\n return map(result, MapIteratorType.NEXT);\n },\n };\n if (iterator.return) {\n subIterator.return = async (...args: [] | [TReturn]) => {\n const result = await iterator.return!(...args);\n return map(result, MapIteratorType.RETURN);\n };\n } else {\n subIterator.return = async (value: TReturn) => {\n return map({ done: true, value }, MapIteratorType.RETURN);\n };\n }\n if (iterator.throw) {\n subIterator.throw = async (...args: [] | [unknown]) => {\n const result = await iterator.throw!(...args);\n return map(result, MapIteratorType.THROW);\n };\n }\n\n return subIterator;\n};\n\n/**\n * Wraps an async iterable with abort signal support.\n * Each iteration creates a fresh iterator with scoped abort handling.\n * Listener is added at iteration start and removed on completion/abort/return.\n *\n * @template T - The yielded value type\n * @template TReturn - The return value type\n * @template TNext - The type passed to next()\n * @param iterable - The source async iterable to wrap\n * @param signal - AbortSignal to cancel iteration\n * @returns An async iterable with abort support\n *\n * @example\n * ```typescript\n * const controller = new AbortController();\n * const source = async function*() { yield 1; yield 2; yield 3; };\n *\n * for await (const value of abortableIterable(source(), controller.signal)) {\n * console.log(value);\n * if (value === 2) controller.abort();\n * }\n * ```\n */\nexport function abortableIterable<T, TReturn, TNext>(iterable: AsyncIterable<T, TReturn, TNext>, signal: AbortSignal): AsyncIterable<T, TReturn, TNext> {\n return {\n [Symbol.asyncIterator](): AsyncIterator<T, TReturn, TNext> {\n const iterator = iterable[Symbol.asyncIterator]();\n const { promise, resolve } = Promise.withResolvers<void>();\n const onAbort = () => resolve();\n let closed = false;\n\n const finish = (value?: TReturn): Promise<IteratorResult<T, TReturn>> => {\n if (closed) {\n return Promise.resolve({ done: true, value: value as TReturn });\n }\n closed = true;\n signal.removeEventListener('abort', onAbort);\n return iterator.return?.(value) ?? Promise.resolve({ done: true, value: value as TReturn });\n };\n\n if (signal.aborted) {\n onAbort();\n } else {\n signal.addEventListener('abort', onAbort);\n }\n\n const race = [promise, undefined] as unknown as [Promise<void>, Promise<IteratorResult<T, TReturn>>];\n\n return {\n async next(...args: [] | [TNext]): Promise<IteratorResult<T, TReturn>> {\n race[1] = iterator.next(...args);\n const result = await Promise.race(race);\n if (result === undefined) {\n void finish();\n return { done: true, value: undefined as TReturn };\n }\n if (result.done) {\n closed = true;\n signal.removeEventListener('abort', onAbort);\n }\n return result;\n },\n async return(value?: TReturn): Promise<IteratorResult<T, TReturn>> {\n return finish(value);\n },\n };\n },\n };\n}\n\n/**\n * Interface for creating iterable number sequences with various parameter combinations.\n * Supports infinite sequences, counted sequences, and sequences with custom start and step values.\n */\nexport interface Iterate {\n (): Iterable<number, void, unknown>;\n (count: number): Iterable<number, void, unknown>;\n (start: number, count: number): Iterable<number, void, unknown>;\n (start: number, count: number, step: number): Iterable<number, void, unknown>;\n}\n\n/**\n * Creates an iterable sequence of numbers with flexible parameters.\n * Can generate infinite sequences, finite sequences, or sequences with custom start and step values.\n *\n * @param args Variable arguments to configure the sequence:\n * - No args: Infinite sequence starting at 0 with step 1\n * - 1 arg (count): Sequence from 0 to count-1\n * - 2 args (start, count): Sequence starting at 'start' for 'count' iterations\n * - 3 args (start, count, step): Custom start, count, and step value\n * @returns An iterable that generates numbers according to the parameters\n *\n * @example\n * ```typescript\n * // Infinite sequence: 0, 1, 2, 3, ...\n * for (const n of iterate()) { }\n *\n * // Count only: 0, 1, 2, 3, 4\n * for (const n of iterate(5)) { }\n *\n * // Start and count: 10, 11, 12, 13, 14\n * for (const n of iterate(10, 5)) { }\n *\n * // Start, count, and step: 0, 2, 4, 6, 8\n * for (const n of iterate(0, 5, 2)) { }\n * ```\n */\nexport const iterate: Iterate = (startOrCount?: number, countWhenTwoArgs?: number, step: number = 1): Iterable<number, void, unknown> => {\n const hasStartArg = countWhenTwoArgs !== undefined;\n const start = hasStartArg ? startOrCount! : 0;\n const count = startOrCount === undefined ? Infinity : hasStartArg ? countWhenTwoArgs : startOrCount;\n\n return {\n [Symbol.iterator]() {\n let idx = 0;\n let current = start;\n return {\n next() {\n if (idx < count) {\n const value = current;\n current += step;\n idx++;\n return { value, done: false };\n }\n return { value: undefined, done: true };\n },\n return(value) {\n idx = count;\n return { value, done: true };\n },\n throw(error?: unknown) {\n idx = count;\n throw error;\n },\n } satisfies Iterator<number, void, unknown>;\n },\n };\n};\n\n/**\n * @internal\n * Creates a promise that resolves after a specified timeout. If an `AbortSignal` is provided and triggered,\n * the timeout is cleared, and the promise resolves to `false`.\n *\n * @param {number} timeout - The time in milliseconds to wait before resolving the promise.\n * @param {AbortSignal} [signal] - An optional `AbortSignal` that can abort the timeout.\n * @returns {Promise<boolean>} A promise that resolves to `true` if the timeout completed, or `false` if it was aborted.\n *\n * @example\n * ```typescript\n * const controller = new AbortController();\n * setTimeout(() => controller.abort(), 500);\n * const result = await setTimeoutAsync(1000, controller.signal);\n * console.log(result); // false\n * ```\n */\nexport const setTimeoutAsync = async (timeout: number, signal?: AbortSignal): Promise<boolean> => {\n if (signal?.aborted) {\n return false;\n }\n const { promise, resolve } = Promise.withResolvers<boolean>();\n const timerId = setTimeout(resolve, timeout, true);\n const onAbort = () => {\n clearTimeout(timerId);\n resolve(false);\n };\n signal?.addEventListener('abort', onAbort);\n\n return promise.finally(() => signal?.removeEventListener('abort', onAbort));\n};\n\n/**\n * Converts a synchronous iterable to an asynchronous iterable.\n * Wraps the sync iterator methods to return promises, enabling uniform async handling.\n *\n * @template T The type of values yielded by the iterator\n * @template TReturn The return type of the iterator\n * @template TNext The type of value that can be passed to next()\n * @param iterable A synchronous iterable to convert\n * @returns An async iterable that yields the same values as the input\n *\n * @example\n * ```typescript\n * const syncArray = [1, 2, 3, 4, 5];\n * const asyncIterable = toAsyncIterable(syncArray);\n *\n * for await (const value of asyncIterable) {\n * console.log(value); // 1, 2, 3, 4, 5\n * }\n * ```\n */\nexport const toAsyncIterable = <T, TReturn, TNext>(iterable: Iterable<T, TReturn, TNext>): AsyncIterable<T, TReturn, TNext> => {\n return {\n [Symbol.asyncIterator]() {\n const iterator = iterable[Symbol.iterator]();\n return {\n async next(...args: [TNext] | []) {\n return iterator.next(...args);\n },\n async return(maybeValue) {\n const value = await maybeValue;\n return iterator.return?.(value) ?? ({ value, done: true } as IteratorResult<T, TReturn>);\n },\n async throw(error) {\n if (iterator.throw) {\n return iterator.throw(error);\n }\n throw error;\n },\n } satisfies AsyncIterator<T, TReturn, TNext>;\n },\n };\n};\n\n/**\n * @internal\n * Pipes values from an async iterable through a generator transformation.\n * Applies a generator function to each value, yielding all resulting values.\n * Supports cancellation via AbortSignal for early termination.\n *\n * @template T The input value type\n * @template U The output value type\n * @param iterable The source async iterable\n * @param generatorFactory A factory that returns a generator function for transforming values\n * @param signal Optional AbortSignal to cancel the operation\n * @returns An async generator yielding transformed values\n *\n * @example\n * ```typescript\n * async function* source() {\n * yield 1; yield 2; yield 3;\n * }\n *\n * const doubled = pipe(source(), () => async function*(n) {\n * yield n * 2;\n * });\n *\n * for await (const value of doubled) {\n * console.log(value); // 2, 4, 6\n * }\n * ```\n */\nconst isAsyncIterable = <T, TReturn, TNext>(value: AnyIterable<T, TReturn, TNext>): value is AsyncIterable<T, TReturn, TNext> => {\n return typeof (value as AsyncIterable<T, TReturn, TNext>)[Symbol.asyncIterator] === 'function';\n};\n\n/**\n * @internal\n */\nexport async function* pipe<T, U>(\n iterable: AsyncIterable<T>,\n generatorFactory: () => (value: T) => AnyIterable<U, void, unknown>,\n signal?: AbortSignal,\n): AsyncGenerator<Awaited<U>, void, unknown> {\n const source = signal ? abortableIterable(iterable, signal) : iterable;\n const generator = generatorFactory();\n\n for await (const value of source) {\n const produced = generator(value);\n const subIterable = isAsyncIterable(produced) ? produced : toAsyncIterable(produced);\n const abortableSub = signal ? abortableIterable(subIterable, signal) : subIterable;\n\n for await (const subValue of abortableSub) {\n yield subValue;\n }\n }\n}\n\n/**\n * @internal\n * Merges multiple async iterables into a single stream.\n * Values are yielded as they become available from any source.\n * Completes when all sources complete; aborts all on error.\n * @template T - The value type yielded by all iterables\n * @param iterables - The async iterables to merge\n * @returns A merged async iterable\n */\nexport const mergeIterables = <T>(...iterables: AsyncIterable<T, void, unknown>[]): AsyncIterable<T, void, unknown> => {\n return {\n [Symbol.asyncIterator]() {\n if (iterables.length === 0) {\n return {\n next: async () => ({ value: undefined, done: true }),\n };\n }\n\n const exit = Symbol('mergeIterables.exit');\n const ctrl = new AbortController();\n const sequence = new Sequence<T>(ctrl.signal);\n let remaining = iterables.length;\n\n const pump = async (iterable: AsyncIterable<T, void, unknown>) => {\n try {\n for await (const value of abortableIterable(iterable, ctrl.signal)) {\n if (!sequence.emit(value)) {\n break;\n }\n }\n } catch (error) {\n ctrl.abort(error);\n } finally {\n remaining -= 1;\n if (remaining === 0 && !ctrl.signal.aborted) {\n ctrl.abort(exit);\n }\n }\n };\n\n for (const iterable of iterables) {\n void pump(iterable);\n }\n\n return {\n next: async () => {\n try {\n const value = await sequence.receive();\n return { value, done: false };\n } catch {\n if (ctrl.signal.aborted && ctrl.signal.reason === exit) {\n return { value: undefined, done: true };\n }\n throw ctrl.signal.reason;\n }\n },\n return: async () => {\n ctrl.abort(exit);\n return { value: undefined, done: true };\n },\n throw: async (error?: unknown) => {\n ctrl.abort(error);\n return { value: undefined, done: true };\n },\n };\n },\n };\n};\n"],"names":["MapIteratorType","abortableIterable","isThenable","iterate","mapIterator","mergeIterables","min","noop","pipe","setTimeoutAsync","toAsyncIterable","value","then","values","fallback","result","Infinity","iterator","map","subIterator","next","args","return","done","throw","iterable","signal","Symbol","asyncIterator","promise","resolve","Promise","withResolvers","onAbort","closed","finish","removeEventListener","aborted","addEventListener","race","undefined","startOrCount","countWhenTwoArgs","step","hasStartArg","start","count","idx","current","error","timeout","timerId","setTimeout","clearTimeout","finally","maybeValue","isAsyncIterable","generatorFactory","source","generator","produced","subIterable","abortableSub","subValue","iterables","length","exit","ctrl","AbortController","sequence","Sequence","remaining","pump","emit","abort","receive","reason"],"mappings":";;;;;;;;;;;QAgCYA;eAAAA;;QAiFIC;eAAAA;;QA3GAC;eAAAA;;QA+LHC;eAAAA;;QAtIAC;eAAAA;;QAgTAC;eAAAA;;QA3VGC;eAAAA;;QANHC;eAAAA;;QAqUUC;eAAAA;;QA7FVC;eAAAA;;QAmCAC;eAAAA;;;6BAzRY;AAMlB,SAASR,WAAWS,KAAc;IACvC,OAAOA,UAAU,QAAQ,OAAOA,UAAU,YAAY,OAAO,AAACA,MAA+BC,IAAI,KAAK;AACxG;AAMO,MAAML,OAAO,KAAO;AAMpB,SAASD,IAAIO,MAAwB,EAAEC,QAAgB;IAC5D,IAAIC,SAASC;IACb,KAAK,MAAML,SAASE,OAAQ;QAC1B,IAAIF,QAAQI,QAAQA,SAASJ;IAC/B;IACA,OAAOI,WAAWC,WAAWF,WAAWC;AAC1C;AAMO,IAAA,AAAKf,yCAAAA;;;;WAAAA;;AA+BL,MAAMI,cAAc,CAAuBa,UAA0CC;IAC1F,MAAMC,cAAgD;QACpDC,MAAM,OAAO,GAAGC;YACd,MAAMN,SAAS,MAAME,SAASG,IAAI,IAAIC;YACtC,OAAOH,IAAIH;QACb;IACF;IACA,IAAIE,SAASK,MAAM,EAAE;QACnBH,YAAYG,MAAM,GAAG,OAAO,GAAGD;YAC7B,MAAMN,SAAS,MAAME,SAASK,MAAM,IAAKD;YACzC,OAAOH,IAAIH;QACb;IACF,OAAO;QACLI,YAAYG,MAAM,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AM;YAEhC,MAAMO,OAAO,OAAO/C;gBAClB,IAAI;oBACF,WAAW,MAAMd,SAASV,kBAAkBwB,UAAU0C,KAAKzC,MAAM,EAAG;wBAClE,IAAI,CAAC2C,SAASI,IAAI,CAAC9D,QAAQ;4BACzB;wBACF;oBACF;gBACF,EAAE,OAAOsC,OAAO;oBACdkB,KAAKO,KAAK,CAACzB;gBACb,SAAU;oBACRsB,aAAa;oBACb,IAAIA,cAAc,KAAK,CAACJ,KAAKzC,MAAM,CAACW,OAAO,EAAE;wBAC3C8B,KAAKO,KAAK,CAACR;oBACb;gBACF;YACF;YAEA,KAAK,MAAMzC,YAAYuC,UAAW;gBAChC,KAAKQ,KAAK/C;YACZ;YAEA,OAAO;gBACLL,MAAM;oBACJ,IAAI;wBACF,MAAMT,QAAQ,MAAM0D,SAASM,OAAO;wBACpC,OAAO;4BAAEhE;4BAAOY,MAAM;wBAAM;oBAC9B,EAAE,OAAM;wBACN,IAAI4C,KAAKzC,MAAM,CAACW,OAAO,IAAI8B,KAAKzC,MAAM,CAACkD,MAAM,KAAKV,MAAM;4BACtD,OAAO;gCAAEvD,OAAO6B;gCAAWjB,MAAM;4BAAK;wBACxC;wBACA,MAAM4C,KAAKzC,MAAM,CAACkD,MAAM;oBAC1B;gBACF;gBACAtD,QAAQ;oBACN6C,KAAKO,KAAK,CAACR;oBACX,OAAO;wBAAEvD,OAAO6B;wBAAWjB,MAAM;oBAAK;gBACxC;gBACAC,OAAO,OAAOyB;oBACZkB,KAAKO,KAAK,CAACzB;oBACX,OAAO;wBAAEtC,OAAO6B;wBAAWjB,MAAM;oBAAK;gBACxC;YACF;QACF;IACF;AACF"}
package/build/utils.d.ts CHANGED
@@ -1,3 +1,76 @@
1
+ import { AnyIterator, AnyIterable, MaybePromise } from './types.js';
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare function isThenable(value: unknown): value is PromiseLike<unknown>;
6
+ /**
7
+ * @internal
8
+ * A no-operation function. Useful as a default callback or placeholder.
9
+ */
10
+ export declare const noop: () => void;
11
+ /**
12
+ * @internal
13
+ * Returns the minimum value from an iterable, or a fallback if empty.
14
+ */
15
+ export declare function min(values: Iterable<number>, fallback: number): number;
16
+ /**
17
+ * @internal
18
+ * Indicates which iterator method triggered a mapping operation.
19
+ */
20
+ export declare enum MapIteratorType {
21
+ /** The next() method was called */
22
+ NEXT = 0,
23
+ /** The return() method was called */
24
+ RETURN = 1,
25
+ /** The throw() method was called */
26
+ THROW = 2
27
+ }
28
+ /**
29
+ * @internal
30
+ * A mapping function for transforming iterator results.
31
+ * @template T - The input value type
32
+ * @template U - The output value type
33
+ * @template TReturn - The iterator return type
34
+ */
35
+ export interface MapNext<T, U, TReturn> {
36
+ (result: MaybePromise<IteratorResult<T, TReturn>>, type: MapIteratorType): MaybePromise<IteratorResult<U, TReturn>>;
37
+ }
38
+ /**
39
+ * @internal
40
+ * Wraps an iterator with a mapping function applied to each result.
41
+ * @template U - The output value type
42
+ * @template T - The input value type
43
+ * @template TReturn - The iterator return type
44
+ * @template TNext - The type passed to next()
45
+ * @param iterator - The source iterator to wrap
46
+ * @param map - The mapping function to apply to each result
47
+ * @returns An async iterator with mapped results
48
+ */
49
+ export declare const mapIterator: <U, T, TReturn, TNext>(iterator: AnyIterator<T, TReturn, TNext>, map: MapNext<T, U, TReturn>) => AsyncIterator<U, TReturn, TNext>;
50
+ /**
51
+ * Wraps an async iterable with abort signal support.
52
+ * Each iteration creates a fresh iterator with scoped abort handling.
53
+ * Listener is added at iteration start and removed on completion/abort/return.
54
+ *
55
+ * @template T - The yielded value type
56
+ * @template TReturn - The return value type
57
+ * @template TNext - The type passed to next()
58
+ * @param iterable - The source async iterable to wrap
59
+ * @param signal - AbortSignal to cancel iteration
60
+ * @returns An async iterable with abort support
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const controller = new AbortController();
65
+ * const source = async function*() { yield 1; yield 2; yield 3; };
66
+ *
67
+ * for await (const value of abortableIterable(source(), controller.signal)) {
68
+ * console.log(value);
69
+ * if (value === 2) controller.abort();
70
+ * }
71
+ * ```
72
+ */
73
+ export declare function abortableIterable<T, TReturn, TNext>(iterable: AsyncIterable<T, TReturn, TNext>, signal: AbortSignal): AsyncIterable<T, TReturn, TNext>;
1
74
  /**
2
75
  * Interface for creating iterable number sequences with various parameter combinations.
3
76
  * Supports infinite sequences, counted sequences, and sequences with custom start and step values.
@@ -75,30 +148,16 @@ export declare const setTimeoutAsync: (timeout: number, signal?: AbortSignal) =>
75
148
  */
76
149
  export declare const toAsyncIterable: <T, TReturn, TNext>(iterable: Iterable<T, TReturn, TNext>) => AsyncIterable<T, TReturn, TNext>;
77
150
  /**
78
- * Pipes values from an async iterable through a generator transformation.
79
- * Applies a generator function to each value, yielding all resulting values.
80
- * Supports cancellation via AbortSignal for early termination.
81
- *
82
- * @template T The input value type
83
- * @template U The output value type
84
- * @param iterable The source async iterable
85
- * @param generatorFactory A factory that returns a generator function for transforming values
86
- * @param signal Optional AbortSignal to cancel the operation
87
- * @returns An async generator yielding transformed values
88
- *
89
- * @example
90
- * ```typescript
91
- * async function* source() {
92
- * yield 1; yield 2; yield 3;
93
- * }
94
- *
95
- * const doubled = pipe(source(), () => async function*(n) {
96
- * yield n * 2;
97
- * });
98
- *
99
- * for await (const value of doubled) {
100
- * console.log(value); // 2, 4, 6
101
- * }
102
- * ```
151
+ * @internal
152
+ */
153
+ export declare function pipe<T, U>(iterable: AsyncIterable<T>, generatorFactory: () => (value: T) => AnyIterable<U, void, unknown>, signal?: AbortSignal): AsyncGenerator<Awaited<U>, void, unknown>;
154
+ /**
155
+ * @internal
156
+ * Merges multiple async iterables into a single stream.
157
+ * Values are yielded as they become available from any source.
158
+ * Completes when all sources complete; aborts all on error.
159
+ * @template T - The value type yielded by all iterables
160
+ * @param iterables - The async iterables to merge
161
+ * @returns A merged async iterable
103
162
  */
104
- export declare function pipe<T, U>(iterable: AsyncIterable<T>, generatorFactory: () => (value: T) => AsyncIterable<U>, signal?: AbortSignal): AsyncGenerator<Awaited<U>, void, unknown>;
163
+ export declare const mergeIterables: <T>(...iterables: AsyncIterable<T, void, unknown>[]) => AsyncIterable<T, void, unknown>;