UNPKG

1.24 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
24var prototype = inherits(Collect, Transform);
25
26prototype.transform = function(_, pulse) {
27 var out = pulse.fork(pulse.ALL),
28 list = SortedList(tupleid, this.value, out.materialize(out.ADD).add),
29 sort = _.sort,
30 mod = pulse.changed() || (sort &&
31 (_.modified('sort') || pulse.modified(sort.fields)));
32
33 out.visit(out.REM, list.remove);
34
35 this.modified(mod);
36 this.value = out.source = list.data(stableCompare(sort), mod);
37
38 // propagate tree root if defined
39 if (pulse.source && pulse.source.root) {
40 this.value.root = pulse.source.root;
41 }
42
43 return out;
44};