UNPKG

@electric-sql/d2mini

Version:

D2Mini is a minimal implementation of Differential Dataflow for performing in-memory incremental view maintenance.

96 lines 2.49 kB
import { MultiSet } from './multiset.js'; /** * A read handle to a dataflow edge that receives data from a writer. */ export class DifferenceStreamReader { #queue; constructor(queue) { this.#queue = queue; } drain() { const out = [...this.#queue].reverse(); this.#queue.length = 0; return out; } isEmpty() { return this.#queue.length === 0; } } /** * A write handle to a dataflow edge that is allowed to publish data. */ export class DifferenceStreamWriter { #queues = []; sendData(collection) { if (!(collection instanceof MultiSet)) { collection = new MultiSet(collection); } for (const q of this.#queues) { q.unshift(collection); } } newReader() { const q = []; this.#queues.push(q); return new DifferenceStreamReader(q); } } /** * A generic implementation of a dataflow operator (node) that has multiple incoming edges (read handles) and * one outgoing edge (write handle). */ export class Operator { id; inputs; output; constructor(id, inputs, output) { this.id = id; this.inputs = inputs; this.output = output; } hasPendingWork() { return this.inputs.some((input) => !input.isEmpty()); } } /** * A convenience implementation of a dataflow operator that has a handle to one * incoming stream of data, and one handle to an outgoing stream of data. */ export class UnaryOperator extends Operator { id; constructor(id, inputA, output) { super(id, [inputA], output); this.id = id; } inputMessages() { return this.inputs[0].drain(); } } /** * A convenience implementation of a dataflow operator that has a handle to two * incoming streams of data, and one handle to an outgoing stream of data. */ export class BinaryOperator extends Operator { id; constructor(id, inputA, inputB, output) { super(id, [inputA, inputB], output); this.id = id; } inputAMessages() { return this.inputs[0].drain(); } inputBMessages() { return this.inputs[1].drain(); } } /** * Base class for operators that process a single input stream */ export class LinearUnaryOperator extends UnaryOperator { run() { for (const message of this.inputMessages()) { this.output.sendData(this.inner(message)); } } } //# sourceMappingURL=graph.js.map