@tanstack/db-ivm
Version:
Incremental View Maintenance for TanStack DB based on Differential Dataflow
1 lines • 2.24 kB
Source Map (JSON)
{"version":3,"file":"consolidate.cjs","sources":["../../../src/operators/consolidate.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\n\n/**\n * Operator that consolidates collections\n */\nexport class ConsolidateOperator<T> extends UnaryOperator<T> {\n run(): void {\n const messages = this.inputMessages()\n if (messages.length === 0) {\n return\n }\n\n // Combine all messages into a single MultiSet\n const combined = new MultiSet<T>()\n for (const message of messages) {\n combined.extend(message)\n }\n\n // Consolidate the combined MultiSet\n const consolidated = combined.consolidate()\n\n // Only send if there are results\n if (consolidated.getInner().length > 0) {\n this.output.sendData(consolidated)\n }\n }\n}\n\n/**\n * Consolidates the elements in the stream\n */\nexport function consolidate<T>(): PipedOperator<T, T> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const output = new StreamBuilder<T>(\n stream.graph,\n new DifferenceStreamWriter<T>()\n )\n const operator = new ConsolidateOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["UnaryOperator","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;AAQO,MAAM,4BAA+BA,MAAAA,cAAiB;AAAA,EAC3D,MAAY;AACV,UAAM,WAAW,KAAK,cAAA;AACtB,QAAI,SAAS,WAAW,GAAG;AACzB;AAAA,IACF;AAGA,UAAM,WAAW,IAAIC,kBAAA;AACrB,eAAW,WAAW,UAAU;AAC9B,eAAS,OAAO,OAAO;AAAA,IACzB;AAGA,UAAM,eAAe,SAAS,YAAA;AAG9B,QAAI,aAAa,WAAW,SAAS,GAAG;AACtC,WAAK,OAAO,SAAS,YAAY;AAAA,IACnC;AAAA,EACF;AACF;AAKO,SAAS,cAAsC;AACpD,SAAO,CAAC,WAAiD;AACvD,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA0B;AAEhC,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,OAAO;AAAA,IAAA;AAET,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}