Commit 1d1fb3c4 1d1fb3c49fc1d6972d350b9660b5c56cd45a1e9e by Mickaël Andrieu

Merge pull request #766 from n1k0/feat-issue687

refs #687 - resolved issue by changing event listeners order
2 parents 797aa2da 4ae419ad
...@@ -157,6 +157,51 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { ...@@ -157,6 +157,51 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
157 return this; 157 return this;
158 }; 158 };
159 159
160 EventEmitter.prototype.prependListener = function prependListener(type, listener) {
161 if ('function' !== typeof listener) {
162 throw new CasperError('addListener only takes instances of Function');
163 }
164
165 if (!this._events) this._events = {};
166
167 // To avoid recursion in the case that type == "newListeners"! Before
168 // adding it to the listeners, first emit "newListeners".
169 this.emit('newListener', type, listener);
170
171 if (!this._events[type]) {
172 // Optimize the case of one listener. Don't need the extra array object.
173 this._events[type] = listener;
174 } else if (isArray(this._events[type])) {
175
176 // If we've already got an array, just append.
177 this._events[type].unshift(listener);
178
179 // Check for listener leak
180 if (!this._events[type].warned) {
181 var m;
182 if (this._maxListeners !== undefined) {
183 m = this._maxListeners;
184 } else {
185 m = defaultMaxListeners;
186 }
187
188 if (m && m > 0 && this._events[type].length > m) {
189 this._events[type].warned = true;
190 console.error('(node) warning: possible EventEmitter memory ' +
191 'leak detected. %d listeners added. ' +
192 'Use emitter.setMaxListeners() to increase limit.',
193 this._events[type].length);
194 console.trace();
195 }
196 }
197 } else {
198 // Adding the second element, need to change to array.
199 this._events[type] = [listener, this._events[type]];
200 }
201
202 return this;
203 };
204
160 EventEmitter.prototype.on = EventEmitter.prototype.addListener; 205 EventEmitter.prototype.on = EventEmitter.prototype.addListener;
161 206
162 EventEmitter.prototype.once = function once(type, listener) { 207 EventEmitter.prototype.once = function once(type, listener) {
......
...@@ -1532,7 +1532,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { ...@@ -1532,7 +1532,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
1532 }; 1532 };
1533 1533
1534 /** 1534 /**
1535 * Runs al suites contained in the paths passed as arguments. 1535 * Runs all suites contained in the paths passed as arguments.
1536 * 1536 *
1537 */ 1537 */
1538 Tester.prototype.runSuites = function runSuites() { 1538 Tester.prototype.runSuites = function runSuites() {
......
...@@ -27,3 +27,19 @@ casper.test.begin('filters', 3, function(test) { ...@@ -27,3 +27,19 @@ casper.test.begin('filters', 3, function(test) {
27 delete casper.foo; 27 delete casper.foo;
28 test.done(); 28 test.done();
29 }); 29 });
30
31 casper.test.begin('events order', 2, function(test) {
32 casper.mowed = "Moo";
33 casper.on("mow", function() {
34 this.mowed = casper.mowed + " Moo";
35 });
36 casper.emit("mow");
37 test.assertEquals(casper.mowed, "Moo Moo", "mowed has the correct value");
38
39 casper.prependListener("mow", function() {
40 this.mowed = this.mowed + " Boo";
41 });
42 casper.emit("mow");
43 test.assertEquals(casper.mowed, "Moo Moo Boo Moo", "mowed has the correct value");
44 test.done();
45 });
......