Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

concat.ts

44 lines1.2 KB

1import { BinaryOperator, DifferenceStreamWriter } from "../graph.js"
2import { StreamBuilder } from "../d2.js"
3import type { IStreamBuilder, PipedOperator } from "../types.js"
4​
5/**
6 * Operator that concatenates two input streams
7 */
8export class ConcatOperator<T, T2> extends BinaryOperator<T | T2> {
9 run(): void {
10 for (const message of this.inputAMessages()) {
11 this.output.sendData(message)
12 }
13​
14 for (const message of this.inputBMessages()) {
15 this.output.sendData(message)
16 }
17 }
19​
20/**
21 * Concatenates two input streams
22 * @param other - The other stream to concatenate
23 */
24export function concat<T, T2>(
25 other: IStreamBuilder<T2>
26): PipedOperator<T, T | T2> {
27 return (stream: IStreamBuilder<T | T2>): IStreamBuilder<T | T2> => {
28 if (stream.graph !== other.graph) {
29 throw new Error(`Cannot concat streams from different graphs`)
30 }
31 const output = new StreamBuilder<T | T2>(
32 stream.graph,
33 new DifferenceStreamWriter<T | T2>()
34 )
35 const operator = new ConcatOperator<T, T2>(
36 stream.graph.getNextOperatorId(),
37 stream.connectReader(),
38 other.connectReader(),
39 output.writer
40 )
41 stream.graph.addOperator(operator)
42 return output
43 }