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) {
return this;
};
EventEmitter.prototype.prependListener = function prependListener(type, listener) {
if ('function' !== typeof listener) {
throw new CasperError('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, listener);
if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
} else if (isArray(this._events[type])) {
// If we've already got an array, just append.
this._events[type].unshift(listener);
// Check for listener leak
if (!this._events[type].warned) {
var m;
if (this._maxListeners !== undefined) {
m = this._maxListeners;
} else {
m = defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
} else {
// Adding the second element, need to change to array.
this._events[type] = [listener, this._events[type]];
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function once(type, listener) {
......
......@@ -1532,7 +1532,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
};
/**
* Runs al suites contained in the paths passed as arguments.
* Runs all suites contained in the paths passed as arguments.
*
*/
Tester.prototype.runSuites = function runSuites() {
......
......@@ -27,3 +27,19 @@ casper.test.begin('filters', 3, function(test) {
delete casper.foo;
test.done();
});
casper.test.begin('events order', 2, function(test) {
casper.mowed = "Moo";
casper.on("mow", function() {
this.mowed = casper.mowed + " Moo";
});
casper.emit("mow");
test.assertEquals(casper.mowed, "Moo Moo", "mowed has the correct value");
casper.prependListener("mow", function() {
this.mowed = this.mowed + " Boo";
});
casper.emit("mow");
test.assertEquals(casper.mowed, "Moo Moo Boo Moo", "mowed has the correct value");
test.done();
});
......