@electric-sql/d2ts
Version:
D2TS is a TypeScript implementation of Differential Dataflow.
62 lines • 2.54 kB
JavaScript
import { DifferenceStreamWriter } from '../graph.js';
import { StreamBuilder } from '../d2.js';
import { BinaryOperator } from '../graph.js';
import { MessageType } from '../types.js';
/**
* Operator that concatenates two input streams
*/
export class ConcatOperator extends BinaryOperator {
run() {
for (const message of this.inputAMessages()) {
if (message.type === MessageType.DATA) {
const { version, collection } = message.data;
this.output.sendData(version, collection);
}
else if (message.type === MessageType.FRONTIER) {
const frontier = message.data;
if (!this.inputAFrontier().lessEqual(frontier)) {
throw new Error('Invalid frontier update');
}
this.setInputAFrontier(frontier);
}
}
for (const message of this.inputBMessages()) {
if (message.type === MessageType.DATA) {
const { version, collection } = message.data;
this.output.sendData(version, collection);
}
else if (message.type === MessageType.FRONTIER) {
const frontier = message.data;
if (!this.inputBFrontier().lessEqual(frontier)) {
throw new Error('Invalid frontier update');
}
this.setInputBFrontier(frontier);
}
}
const inputFrontier = this.inputAFrontier().meet(this.inputBFrontier());
if (!this.outputFrontier.lessEqual(inputFrontier)) {
throw new Error('Invalid frontier state');
}
if (this.outputFrontier.lessThan(inputFrontier)) {
this.outputFrontier = inputFrontier;
this.output.sendFrontier(this.outputFrontier);
}
}
}
/**
* Concatenates two input streams
* @param other - The other stream to concatenate
*/
export function concat(other) {
return (stream) => {
if (stream.graph !== other.graph) {
throw new Error('Cannot concat streams from different graphs');
}
const output = new StreamBuilder(stream.graph, new DifferenceStreamWriter());
const operator = new ConcatOperator(stream.graph.getNextOperatorId(), stream.connectReader(), other.connectReader(), output.writer, stream.graph.frontier());
stream.graph.addOperator(operator);
stream.graph.addStream(output.connectReader());
return output;
};
}
//# sourceMappingURL=concat.js.map