Open History to see the last commit
1import { UnaryOperator } from '../graph.js'
2import { unaryOperator } from '../unary-operator.js'
3import type { PipedOperator } from '../types.js'
4import type {
5 DifferenceStreamReader,
6 DifferenceStreamWriter,
7} from '../graph.js'
8import type { MultiSet } from '../multiset.js'
9
10/**
11 * Operator that outputs the messages in the stream
12 */
13export class OutputOperator<T> extends UnaryOperator<T> {
14 #fn: (data: MultiSet<T>) => void
15
16 constructor(
17 id: number,
18 inputA: DifferenceStreamReader<T>,
19 outputWriter: DifferenceStreamWriter<T>,
20 fn: (data: MultiSet<T>) => void,
21 ) {
22 super(id, inputA, outputWriter)
23 this.#fn = fn
24 }
25
26 run(): void {
27 for (const message of this.inputMessages()) {
28 this.#fn(message)
29 this.output.sendData(message)
30 }
31 }
32}
33
34/**
35 * Outputs the messages in the stream
36 * @param fn - The function to call with each message
37 */
38export function output<T>(
39 fn: (data: MultiSet<T>) => void,
40): PipedOperator<T, T> {
41 return unaryOperator(OutputOperator<T>, fn)
42}