-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
signals.js
132 lines (114 loc) · 3.34 KB
/
signals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var dl = require('datalib'),
expr = require('./expr'),
SIGNALS = require('vega-dataflow').Dependencies.SIGNALS;
var RESERVED = ['datum', 'event', 'signals', 'width', 'height', 'padding']
.concat(dl.keys(expr.codegen.functions));
function parseSignals(model, spec) {
// process each signal definition
(spec || []).forEach(function(s) {
if (RESERVED.indexOf(s.name) !== -1) {
throw Error('Signal name "'+s.name+'" is a '+
'reserved keyword ('+RESERVED.join(', ')+').');
}
var signal = model.signal(s.name, s.init)
.verbose(s.verbose);
if (s.init && s.init.expr) {
s.init.expr = model.expr(s.init.expr);
signal.value(exprVal(model, s.init));
}
if (s.expr) {
s.expr = model.expr(s.expr);
signal.evaluate = function(input) {
var val = exprVal(model, s),
sg = input.signals;
if (val !== signal.value() || signal.verbose()) {
signal.value(val);
sg[s.name] = 1;
}
return sg[s.name] ? input : model.doNotPropagate;
};
signal.dependency(SIGNALS, s.expr.globals);
s.expr.globals.forEach(function(dep) {
model.signal(dep).addListener(signal);
});
}
});
return spec;
}
function exprVal(model, spec) {
var e = spec.expr, v = e.fn();
return spec.scale ? parseSignals.scale(model, spec, v) : v;
}
parseSignals.scale = function scale(model, spec, value, datum, evt) {
var def = spec.scale,
name = def.name || def.signal || def,
scope = def.scope, e;
if (scope) {
if (scope.signal) {
scope = model.signalRef(scope.signal);
} else if (dl.isString(scope)) { // Scope is an expression
e = def._expr = (def._expr || model.expr(scope));
scope = e.fn(datum, evt);
}
}
if (!scope || !scope.scale) {
scope = (scope && scope.mark) ? scope.mark.group : model.scene().items[0];
}
// Verify scope is valid
if (model.group(scope._id) !== scope) {
throw new Error('Scope for scale "'+name+'" is not a valid group item.');
}
var s = scope.scale(name);
return !s ? value : (def.invert ? s.invert(value) : s(value));
};
module.exports = parseSignals;
parseSignals.schema = {
"refs": {
"signal": {
"title": "SignalRef",
"type": "object",
"properties": {"signal": {"type": "string"}},
"required": ["signal"]
},
"scopedScale": {
"oneOf": [
{"type": "string"},
{
"type": "object",
"properties": {
"name": {
"oneOf": [{"$ref": "#/refs/signal"}, {"type": "string"}]
},
"scope": {
"oneOf": [
{"$ref": "#/refs/signal"},
{"type": "string"}
]
},
"invert": {"type": "boolean", "default": false}
},
"additionalProperties": false,
"required": ["name"]
}
]
}
},
"defs": {
"signal": {
"type": "object",
"properties": {
"name": {
"type": "string",
"not": {"enum": RESERVED}
},
"init": {},
"verbose": {"type": "boolean", "default": false},
"expr": {"type": "string"},
"scale": {"$ref": "#/refs/scopedScale"},
"streams": {"$ref": "#/defs/streams"}
},
"additionalProperties": false,
"required": ["name"]
}
}
};