-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredispatch.js
45 lines (35 loc) · 1 KB
/
redispatch.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
define(function (require) {
var _ = require('underscore')
, dispatch = require('d3-dispatch').dispatch
return function redispatch() {
var dispatchers = []
function redispatch() {
var dispatcher = dispatch.apply(null, dispatchers.reduce(types, []))
dispatchers.forEach(proxyEvents)
return dispatcher
function proxyEvents(d) {
d.types.forEach(proxyEvent)
function proxyEvent(type) {
d.dispatcher.on(type + '.redispatch', dispatcher[type])
}
}
}
redispatch.from = function(dispatcher, eventTypes) {
dispatchers.push({
dispatcher: dispatcher
, types: _.isArray(eventTypes) ? eventTypes : _.rest(arguments)
})
return redispatch
}
redispatch.eventTypes = function() {
return dispatchers.reduce(toTypes, [])
}
return redispatch
function types(acc, next) {
return acc.concat(next.types)
}
function toTypes(acc, v, k) {
return acc.concat(v.types)
}
}
})