Open History to see the last commit
1import { MultiSet } from './multiset.js'
2import type { MultiSetArray } from './multiset.js'
3import type {
4 IDifferenceStreamReader,
5 IDifferenceStreamWriter,
6 IOperator,
7} from './types.js'
8
9/**
10 * A read handle to a dataflow edge that receives data from a writer.
11 */
12export class DifferenceStreamReader<T> implements IDifferenceStreamReader<T> {
13 #queue: Array<MultiSet<T>>
14
15 constructor(queue: Array<MultiSet<T>>) {
16 this.#queue = queue
17 }
18
19 drain(): Array<MultiSet<T>> {
20 const out = [...this.#queue].reverse()
21 this.#queue.length = 0
22 return out
23 }
24
25 isEmpty(): boolean {
26 return this.#queue.length === 0
27 }
28}
29
30/**
31 * A write handle to a dataflow edge that is allowed to publish data.
32 */
33export class DifferenceStreamWriter<T> implements IDifferenceStreamWriter<T> {
34 #queues: Array<Array<MultiSet<T>>> = []
35
36 sendData(collection: MultiSet<T> | MultiSetArray<T>): void {
37 if (!(collection instanceof MultiSet)) {
38 collection = new MultiSet(collection)
39 }
40
41 for (const q of this.#queues) {
42 q.unshift(collection)
43 }
44 }
45
46 newReader(): DifferenceStreamReader<T> {
47 const q: Array<MultiSet<T>> = []
48 this.#queues.push(q)
49 return new DifferenceStreamReader(q)
50 }
51}
52
53/**
54 * A generic implementation of a dataflow operator (node) that has multiple incoming edges (read handles) and
55 * one outgoing edge (write handle).
56 */
57export abstract class Operator<T> implements IOperator<T> {
58 protected inputs: Array<DifferenceStreamReader<T>>
59 protected output: DifferenceStreamWriter<T>
60
61 constructor(
62 public id: number,
63 inputs: Array<DifferenceStreamReader<T>>,
64 output: DifferenceStreamWriter<T>,
65 ) {
66 this.inputs = inputs
67 this.output = output
68 }
69
70 abstract run(): void
71
72 hasPendingWork(): boolean {
73 return this.inputs.some((input) => !input.isEmpty())
74 }
75}
76
77/**
78 * A convenience implementation of a dataflow operator that has a handle to one
79 * incoming stream of data, and one handle to an outgoing stream of data.
80 */
81export abstract class UnaryOperator<Tin, Tout = Tin> extends Operator<
82 Tin | Tout
83> {
84 constructor(
85 public id: number,
86 inputA: DifferenceStreamReader<Tin>,
87 output: DifferenceStreamWriter<Tout>,
88 ) {
89 super(id, [inputA], output)
90 }
91
92 inputMessages(): Array<MultiSet<Tin>> {
93 return this.inputs[0]!.drain() as Array<MultiSet<Tin>>
94 }
95}
96
97/**
98 * A convenience implementation of a dataflow operator that has a handle to two
99 * incoming streams of data, and one handle to an outgoing stream of data.
100 */
101export abstract class BinaryOperator<T> extends Operator<T> {
102 constructor(
103 public id: number,
104 inputA: DifferenceStreamReader<T>,
105 inputB: DifferenceStreamReader<T>,
106 output: DifferenceStreamWriter<T>,
107 ) {
108 super(id, [inputA, inputB], output)
109 }
110
111 inputAMessages(): Array<MultiSet<T>> {
112 return this.inputs[0]!.drain()
113 }
114
115 inputBMessages(): Array<MultiSet<T>> {
116 return this.inputs[1]!.drain()
117 }
118}
119
120/**
121 * Base class for operators that process a single input stream
122 */
123export abstract class LinearUnaryOperator<T, U> extends UnaryOperator<T | U> {
124 abstract inner(collection: MultiSet<T | U>): MultiSet<U>
125
126 run(): void {
127 for (const message of this.inputMessages()) {
128 this.output.sendData(this.inner(message))
129 }
130 }
131}