| 4056 | * @since 3.3.0 |
| 4057 | */ |
| 4058 | export const zipLatestAll = <T extends ReadonlyArray<Stream<any, any, any>>>( |
| 4059 | ...streams: T |
| 4060 | ): Stream< |
| 4061 | [T[number]] extends [never] ? never |
| 4062 | : { [K in keyof T]: T[K] extends Stream<infer A, infer _E, infer _R> ? A : never }, |
| 4063 | [T[number]] extends [never] ? never : T[number] extends Stream<infer _A, infer _E, infer _R> ? _E : never, |
| 4064 | [T[number]] extends [never] ? never : T[number] extends Stream<infer _A, infer _E, infer _R> ? _R : never |
| 4065 | > => |
| 4066 | fromChannel(Channel.suspend(() => { |
| 4067 | const latest: Array<any> = [] |
| 4068 | const emitted = new Set<number>() |
| 4069 | const readyLatch = Latch.makeUnsafe() |
| 4070 | const emptyLatch = Latch.makeUnsafe() |
| 4071 | return Channel.mergeAll( |
| 4072 | Channel.fromArray( |
| 4073 | streams.map((s, i) => |
| 4074 | s.channel.pipe( |
| 4075 | Channel.flattenArray, |
| 4076 | Channel.mapEffect((a) => { |
| 4077 | latest[i] = a |
| 4078 | if (!emitted.has(i)) { |
| 4079 | emitted.add(i) |
| 4080 | if (emitted.size < streams.length) { |
| 4081 | return readyLatch.await as Effect.Effect<undefined> |
| 4082 | } |
| 4083 | return Effect.as(readyLatch.open, Arr.of(latest.slice())) |
| 4084 | } |
| 4085 | return Effect.succeed(Arr.of(latest.slice())) |
| 4086 | }), |
| 4087 | Channel.filter(isNotUndefined), |
| 4088 | // An input ending without a value makes a complete tuple impossible. |
| 4089 | Channel.mapDoneEffect(() => emitted.has(i) ? Effect.void : emptyLatch.open) |
| 4090 | ) |
| 4091 | ) |
| 4092 | ), |
| 4093 | { |
| 4094 | concurrency: "unbounded", |
| 4095 | bufferSize: 0 |
| 4096 | } |
| 4097 | ).pipe(Channel.interruptWhen(emptyLatch.await)) |
| 4098 | })) as any |
| 4099 | |
| 4100 | /** |
| 4101 | * Combines two streams by emitting each new element with the latest value from the other stream. |