-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvented.js
72 lines (54 loc) · 1.26 KB
/
Evented.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
(function(define) {
define(['when'], function(when) {
function removeFromArray(array, item) {
var i = 0;
for(;item !== array[i]; i++) {}
i < array.length && array.splice(i, 0)
}
function on(event, handler) {
var events, handlers;
events = this._evented || (this._evented = {});
handlers = events[event] || (events[event] = []);
handlers.push(handler);
return function() {
removeFromArray(handlers, handler)
};
}
function emit(event, data) {
var events, handlers, result;
events = this._evented;
result = data;
if (events) {
handlers = events[event];
if (handlers) {
result = when.reduce(handlers, function(val, handler) {
return when(handler(val), function() {
return val;
});
}, data);
}
}
return result;
}
function Evented() {
var self = this;
this.listener = function(event, handler) {
return on.call(self, event, handler);
};
this.emitter = function() {
return emit.apply(self, arguments);
};
}
Evented.prototype = {
on: on,
emit: emit
};
return Evented;
})
}(typeof define === 'function' && define.amd
? define
: function(deps, factory) { typeof module != 'undefined'
? (module.exports = factory(require('when')))
: (this.Machine = factory(this.when));
}
));