UNPKG

1.12 kBJavaScriptView Raw
1import {Transform} from 'vega-dataflow';
2import {fastmap, inherits} from 'vega-util';
3
4/**
5 * An index that maps from unique, string-coerced, field values to tuples.
6 * Assumes that the field serves as a unique key with no duplicate values.
7 * @constructor
8 * @param {object} params - The parameters for this operator.
9 * @param {function(object): *} params.field - The field accessor to index.
10 */
11export default function TupleIndex(params) {
12 Transform.call(this, fastmap(), params);
13}
14
15var prototype = inherits(TupleIndex, Transform);
16
17prototype.transform = function(_, pulse) {
18 var df = pulse.dataflow,
19 field = _.field,
20 index = this.value,
21 mod = true;
22
23 function set(t) { index.set(field(t), t); }
24
25 if (_.modified('field') || pulse.modified(field.fields)) {
26 index.clear();
27 pulse.visit(pulse.SOURCE, set);
28 } else if (pulse.changed()) {
29 pulse.visit(pulse.REM, function(t) { index.delete(field(t)); });
30 pulse.visit(pulse.ADD, set);
31 } else {
32 mod = false;
33 }
34
35 this.modified(mod);
36 if (index.empty > df.cleanThreshold) df.runAfter(index.clean);
37 return pulse.fork();
38};