@electric-sql/d2ts
Version:
D2TS is a TypeScript implementation of Differential Dataflow.
56 lines • 2.46 kB
JavaScript
import { MessageType, } from '../types.js';
import { DifferenceStreamWriter, UnaryOperator, } from '../graph.js';
import { StreamBuilder } from '../d2.js';
/**
* Operator that logs debug information about the stream
*/
export class DebugOperator extends UnaryOperator {
#name;
#indent;
constructor(id, inputA, output, name, initialFrontier, indent = false) {
super(id, inputA, output, initialFrontier);
this.#name = name;
this.#indent = indent;
}
run() {
for (const message of this.inputMessages()) {
if (message.type === MessageType.DATA) {
const { version, collection } = message.data;
// eslint-disable-next-line no-console
console.log(`debug ${this.#name} data: version: ${version.toString()} collection: ${collection.toString(this.#indent)}`);
this.output.sendData(version, collection);
}
else if (message.type === MessageType.FRONTIER) {
const frontier = message.data;
if (!this.inputFrontier().lessEqual(frontier)) {
throw new Error('Invalid frontier update');
}
this.setInputFrontier(frontier);
// eslint-disable-next-line no-console
console.log(`debug ${this.#name} notification: frontier ${frontier.toString()}`);
if (!this.outputFrontier.lessEqual(this.inputFrontier())) {
throw new Error('Invalid frontier state');
}
if (this.outputFrontier.lessThan(this.inputFrontier())) {
this.outputFrontier = this.inputFrontier();
this.output.sendFrontier(this.outputFrontier);
}
}
}
}
}
/**
* Logs debug information about the stream using console.log
* @param name - The name to prefix debug messages with
* @param indent - Whether to indent the debug output
*/
export function debug(name, indent = false) {
return (stream) => {
const output = new StreamBuilder(stream.graph, new DifferenceStreamWriter());
const operator = new DebugOperator(stream.graph.getNextOperatorId(), stream.connectReader(), output.writer, name, stream.graph.frontier(), indent);
stream.graph.addOperator(operator);
stream.graph.addStream(output.connectReader());
return output;
};
}
//# sourceMappingURL=debug.js.map