Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

misc

Tj Holowaychuk54abdca9b06b
diff --git a/lib/browser/events.js b/lib/browser/events.js
index 7c9c4a89..50c47ba0 100644
--- a/lib/browser/events.js
+++ b/lib/browser/events.js
@@ -3,7 +3,7 @@
* Module exports.
*/
-module.exports = EventEmitter
+module.exports = EventEmitter;
/**
* Check if `obj` is an array.
diff --git a/mocha.js b/mocha.js
index 374d0265..5c4f7296 100644
--- a/mocha.js
+++ b/mocha.js
@@ -58,58 +58,182 @@ module.exports = function(type){
require.register("browser/events.js", function(module, exports, require){
/**
- * Expose `EventEmitter`.
+ * Module exports.
*/
-exports.EventEmitter = EventEmitter;
+module.exports = EventEmitter
/**
- * Slice reference.
+ * Check if `obj` is an array.
*/
-var slice = [].slice;
+function isArray(obj) {
+ return '[object Array]' == {}.toString.call(obj);
+}
/**
- * EventEmitter.
+ * Event emitter constructor.
+ *
+ * @api public.
*/
-function EventEmitter() {
- this.callbacks = {};
+function EventEmitter(){};
+
+/**
+ * Adds a listener.
+ *
+ * @api public
+ */
+
+EventEmitter.prototype.on = function (name, fn) {
+ if (!this.$events) {
+ this.$events = {};
+ }
+
+ if (!this.$events[name]) {
+ this.$events[name] = fn;
+ } else if (isArray(this.$events[name])) {
+ this.$events[name].push(fn);
+ } else {
+ this.$events[name] = [this.$events[name], fn];
+ }
+
+ return this;
};
+EventEmitter.prototype.addListener = EventEmitter.prototype.on;
+
/**
- * Listen on the given `event` with `fn`.
+ * Adds a volatile listener.
*
- * @param {String} event
- * @param {Function} fn
+ * @api public
*/
-EventEmitter.prototype.on = function(event, fn){
- (this.callbacks[event] = this.callbacks[event] || [])
- .push(fn);
+EventEmitter.prototype.once = function (name, fn) {
+ var self = this;
+
+ function on () {
+ self.removeListener(name, on);
+ fn.apply(this, arguments);
+ };
+
+ on.listener = fn;
+ this.on(name, on);
+
return this;
};
/**
- * Emit `event` with the given args.
+ * Removes a listener.
*
- * @param {String} event
- * @param {Mixed} ...
+ * @api public
*/
-EventEmitter.prototype.emit = function(event){
- var args = slice.call(arguments, 1)
- , callbacks = this.callbacks[event];
+EventEmitter.prototype.removeListener = function (name, fn) {
+ if (this.$events && this.$events[name]) {
+ var list = this.$events[name];
+
+ if (isArray(list)) {
+ var pos = -1;
+
+ for (var i = 0, l = list.length; i < l; i++) {
+ if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
+ pos = i;
+ break;
+ }
+ }
+
+ if (pos < 0) {
+ return this;
+ }
+
+ list.splice(pos, 1);
- if (callbacks) {
- for (var i = 0, len = callbacks.length; i < len; ++i) {
- callbacks[i].apply(this, args)
+ if (!list.length) {
+ delete this.$events[name];
+ }
+ } else if (list === fn || (list.listener && list.listener === fn)) {
+ delete this.$events[name];
}
}
return this;
};
+/**
+ * Removes all listeners for an event.
+ *
+ * @api public
+ */
+
+EventEmitter.prototype.removeAllListeners = function (name) {
+ if (name === undefined) {
+ this.$events = {};
+ return this;
+ }
+
+ if (this.$events && this.$events[name]) {
+ this.$events[name] = null;
+ }
+
+ return this;
+};
+
+/**
+ * Gets all listeners for a certain event.
+ *
+ * @api publci
+ */
+
+EventEmitter.prototype.listeners = function (name) {
+ if (!this.$events) {
+ this.$events = {};
+ }
+
+ if (!this.$events[name]) {
+ this.$events[name] = [];
+ }
+
+ if (!isArray(this.$events[name])) {
+ this.$events[name] = [this.$events[name]];
+ }
+
+ return this.$events[name];
+};
+
+/**
+ * Emits an event.
+ *
+ * @api public
+ */
+
+EventEmitter.prototype.emit = function (name) {
+ if (!this.$events) {
+ return false;
+ }
+
+ var handler = this.$events[name];
+
+ if (!handler) {
+ return false;
+ }
+
+ var args = [].slice.call(arguments);
+
+ if ('function' == typeof handler) {
+ handler.apply(this, args);
+ } else if (isArray(handler)) {
+ var listeners = handler.slice();
+
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ listeners[i].apply(this, args);
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+};
}); // module: browser/events.js
require.register("browser/fs.js", function(module, exports, require){