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

unary-operator.ts

32 lines926 Bytes

1import { StreamBuilder } from './d2.js'
2import { DifferenceStreamWriter } from './graph.js'
3import type { DifferenceStreamReader, UnaryOperator } from './graph.js'
4import type { PipedOperator } from './types.js'
5​
6// Share graph wiring, while each public operator keeps its own constructor,
7// inheritance and run/inner dispatch.
8export function unaryOperator<T, U, Args extends Array<unknown>>(
9 Operator: new (
10 id: number,
11 input: DifferenceStreamReader<T>,
12 output: DifferenceStreamWriter<U>,
13 ...args: Args
14 ) => UnaryOperator<T | U>,
15 ...args: Args
16): PipedOperator<T, U> {
17 return (stream) => {
18 const output = new StreamBuilder<U>(
19 stream.graph,
20 new DifferenceStreamWriter<U>(),
21 )
22 stream.graph.addOperator(
23 new Operator(
24 stream.graph.getNextOperatorId(),
25 stream.connectReader(),
26 output.writer,
27 ...args,
28 ),
29 )
30 return output
31 }