@electric-sql/d2ts
Version:
D2TS is a TypeScript implementation of Differential Dataflow.
31 lines • 1.06 kB
JavaScript
import { DifferenceStreamWriter } from '../graph.js';
import { StreamBuilder } from '../d2.js';
import { ReduceOperator } from './reduce.js';
/**
* Operator that counts elements by key
*/
export class CountOperator extends ReduceOperator {
constructor(id, inputA, output, initialFrontier) {
const countInner = (vals) => {
let count = 0;
for (const [_, diff] of vals) {
count += diff;
}
return [[count, 1]];
};
super(id, inputA, output, countInner, initialFrontier);
}
}
/**
* Counts the number of elements by key
*/
export function count() {
return (stream) => {
const output = new StreamBuilder(stream.graph, new DifferenceStreamWriter());
const operator = new CountOperator(stream.graph.getNextOperatorId(), stream.connectReader(), output.writer, stream.graph.frontier());
stream.graph.addOperator(operator);
stream.graph.addStream(output.connectReader());
return output;
};
}
//# sourceMappingURL=count.js.map