UNPKG

1.25 kBJavaScriptView Raw
1import SortedList from './util/SortedList';
2import {Transform, stableCompare, tupleid} from 'vega-dataflow';
3import {inherits} from 'vega-util';
4
5/**
6 * Collects all data tuples that pass through this operator.
7 * @constructor
8 * @param {object} params - The parameters for this operator.
9 * @param {function(*,*): number} [params.sort] - An optional
10 * comparator function for additionally sorting the collected tuples.
11 */
12export default function Collect(params) {
13 Transform.call(this, [], params);
14}
15
16Collect.Definition = {
17 'type': 'Collect',
18 'metadata': {'source': true},
19 'params': [
20 { 'name': 'sort', 'type': 'compare' }
21 ]
22};
23
24inherits(Collect, Transform, {
25 transform(_, pulse) {
26 const out = pulse.fork(pulse.ALL),
27 list = SortedList(tupleid, this.value, out.materialize(out.ADD).add),
28 sort = _.sort,
29 mod = pulse.changed() || (sort &&
30 (_.modified('sort') || pulse.modified(sort.fields)));
31
32 out.visit(out.REM, list.remove);
33
34 this.modified(mod);
35 this.value = out.source = list.data(stableCompare(sort), mod);
36
37 // propagate tree root if defined
38 if (pulse.source && pulse.source.root) {
39 this.value.root = pulse.source.root;
40 }
41
42 return out;
43 }
44});