UNPKG

1.11 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
15inherits(TupleIndex, Transform, {
16 transform(_, pulse) {
17 const df = pulse.dataflow,
18 field = _.field,
19 index = this.value,
20 set = t => index.set(field(t), t);
21
22 let mod = true;
23
24 if (_.modified('field') || pulse.modified(field.fields)) {
25 index.clear();
26 pulse.visit(pulse.SOURCE, set);
27 } else if (pulse.changed()) {
28 pulse.visit(pulse.REM, t => index.delete(field(t)));
29 pulse.visit(pulse.ADD, set);
30 } else {
31 mod = false;
32 }
33
34 this.modified(mod);
35 if (index.empty > df.cleanThreshold) df.runAfter(index.clean);
36 return pulse.fork();
37 }
38});