Repository navigation
Expand file tree
/
Copy pathevt.js
More file actions
161 lines (148 loc) · 4.56 KB
/
Copy pathevt.js
File metadata and controls
161 lines (148 loc) · 4.56 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*global define, setTimeout */
/*
* Custom events lib. Notable features:
*
* - the module itself is an event emitter. Useful for "global" pub/sub.
* - evt.mix can be used to mix in an event emitter into existing object.
* - notification of listeners is done in a try/catch, so all listeners
* are notified even if one fails. Errors are thrown async via setTimeout
* so that all the listeners can be notified without escaping from the
* code via a throw within the listener group notification.
* - new evt.Emitter() can be used to create a new instance of an
* event emitter.
* - Uses "this" insternally, so always call object with the emitter args
*
*/
define(function() {
var evt,
slice = Array.prototype.slice,
props = ['_events', '_pendingEvents', 'on', 'once', 'latest',
'latestOnce', 'removeListener', 'emitWhenListener', 'emit'];
function Emitter() {
this._events = {};
this._pendingEvents = {};
}
Emitter.prototype = {
on: function(id, fn) {
var listeners = this._events[id],
pending = this._pendingEvents[id];
if (!listeners) {
listeners = this._events[id] = [];
}
listeners.push(fn);
if (pending) {
pending.forEach(function(args) {
fn.apply(null, args);
});
delete this._pendingEvents[id];
}
return this;
},
once: function(id, fn) {
var self = this,
fired = false;
function one() {
if (fired)
return;
fired = true;
fn.apply(null, arguments);
// Remove at a further turn so that the event
// forEach in emit does not get modified during
// this turn.
setTimeout(function() {
self.removeListener(id, one);
});
}
return this.on(id, one);
},
/**
* Waits for a property on the object that has the event interface
* to be available. That property MUST EVALUATE TO A TRUTHY VALUE.
* hasOwnProperty is not used because many objects are created with
* null placeholders to give a proper JS engine shape to them, and
* this method should not trigger the listener for those cases.
* If the property is already available, call the listener right
* away. If not available right away, listens for an event name that
* matches the property name.
* @param {String} id property name.
* @param {Function} fn listener.
*/
latest: function(id, fn) {
if (this[id] && !this._pendingEvents[id]) {
fn(this[id]);
}
this.on(id, fn);
},
/**
* Same as latest, but only calls the listener once.
* @param {String} id property name.
* @param {Function} fn listener.
*/
latestOnce: function(id, fn) {
if (this[id] && !this._pendingEvents[id])
fn(this[id]);
else
this.once(id, fn);
},
removeListener: function(id, fn) {
var i,
listeners = this._events[id];
if (listeners) {
i = listeners.indexOf(fn);
if (i !== -1) {
listeners.splice(i, 1);
}
if (listeners.length === 0)
delete this._events[id];
}
},
/**
* Like emit, but if no listeners yet, holds on
* to the value until there is one. Any other
* args after first one are passed to listeners.
* @param {String} id event ID.
*/
emitWhenListener: function(id) {
var listeners = this._events[id];
if (listeners) {
this.emit.apply(this, arguments);
} else {
if (!this._pendingEvents[id])
this._pendingEvents[id] = [];
this._pendingEvents[id].push(slice.call(arguments, 1));
}
},
emit: function(id) {
var args = slice.call(arguments, 1),
listeners = this._events[id];
if (listeners) {
listeners.forEach(function(fn) {
try {
fn.apply(null, args);
} catch (e) {
// Throw at later turn so that other listeners
// can complete. While this messes with the
// stack for the error, continued operation is
// valued more in this tradeoff.
setTimeout(function() {
throw e;
});
}
});
}
}
};
evt = new Emitter();
evt.Emitter = Emitter;
evt.mix = function(obj) {
var e = new Emitter();
props.forEach(function(prop) {
if (obj.hasOwnProperty(prop)) {
throw new Error('Object already has a property "' + prop + '"');
}
obj[prop] = e[prop];
});
return obj;
};
return evt;
});