Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
111 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,129 @@ | ||
function WildEmitter() { | ||
this.isWildEmitter = true; | ||
this.callbacks = {}; | ||
} | ||
|
||
// Listen on the given `event` with `fn`. Store a group name if present. | ||
WildEmitter.prototype.on = function (event, groupName, fn) { | ||
var hasGroup = (arguments.length === 3), | ||
group = hasGroup ? arguments[1] : undefined, | ||
func = hasGroup ? arguments[2] : arguments[1]; | ||
func._groupName = group; | ||
(this.callbacks[event] = this.callbacks[event] || []).push(func); | ||
return this; | ||
}; | ||
function WildEmitter() { } | ||
|
||
// Adds an `event` listener that will be invoked a single | ||
// time then automatically removed. | ||
WildEmitter.prototype.once = function (event, groupName, fn) { | ||
var self = this, | ||
hasGroup = (arguments.length === 3), | ||
group = hasGroup ? arguments[1] : undefined, | ||
func = hasGroup ? arguments[2] : arguments[1]; | ||
function on() { | ||
self.off(event, on); | ||
func.apply(this, arguments); | ||
} | ||
this.on(event, group, on); | ||
return this; | ||
}; | ||
WildEmitter.mixin = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
|
||
// Listen on the given `event` with `fn`. Store a group name if present. | ||
prototype.on = function (event, groupName, fn) { | ||
this.callbacks = this.callbacks || {}; | ||
var hasGroup = (arguments.length === 3), | ||
group = hasGroup ? arguments[1] : undefined, | ||
func = hasGroup ? arguments[2] : arguments[1]; | ||
func._groupName = group; | ||
(this.callbacks[event] = this.callbacks[event] || []).push(func); | ||
return this; | ||
}; | ||
|
||
// Adds an `event` listener that will be invoked a single | ||
// time then automatically removed. | ||
prototype.once = function (event, groupName, fn) { | ||
var self = this, | ||
hasGroup = (arguments.length === 3), | ||
group = hasGroup ? arguments[1] : undefined, | ||
func = hasGroup ? arguments[2] : arguments[1]; | ||
function on() { | ||
self.off(event, on); | ||
func.apply(this, arguments); | ||
} | ||
this.on(event, group, on); | ||
return this; | ||
}; | ||
|
||
// Unbinds an entire group | ||
WildEmitter.prototype.releaseGroup = function (groupName) { | ||
var item, i, len, handlers; | ||
for (item in this.callbacks) { | ||
handlers = this.callbacks[item]; | ||
for (i = 0, len = handlers.length; i < len; i++) { | ||
if (handlers[i]._groupName === groupName) { | ||
//console.log('removing'); | ||
// remove it and shorten the array we're looping through | ||
handlers.splice(i, 1); | ||
i--; | ||
len--; | ||
// Unbinds an entire group | ||
prototype.releaseGroup = function (groupName) { | ||
this.callbacks = this.callbacks || {}; | ||
var item, i, len, handlers; | ||
for (item in this.callbacks) { | ||
handlers = this.callbacks[item]; | ||
for (i = 0, len = handlers.length; i < len; i++) { | ||
if (handlers[i]._groupName === groupName) { | ||
//console.log('removing'); | ||
// remove it and shorten the array we're looping through | ||
handlers.splice(i, 1); | ||
i--; | ||
len--; | ||
} | ||
} | ||
} | ||
} | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
|
||
// Remove the given callback for `event` or all | ||
// registered callbacks. | ||
prototype.off = function (event, fn) { | ||
this.callbacks = this.callbacks || {}; | ||
var callbacks = this.callbacks[event], | ||
i; | ||
|
||
// Remove the given callback for `event` or all | ||
// registered callbacks. | ||
WildEmitter.prototype.off = function (event, fn) { | ||
var callbacks = this.callbacks[event], | ||
i; | ||
if (!callbacks) return this; | ||
|
||
if (!callbacks) return this; | ||
// remove all handlers | ||
if (arguments.length === 1) { | ||
delete this.callbacks[event]; | ||
return this; | ||
} | ||
|
||
// remove all handlers | ||
if (arguments.length === 1) { | ||
delete this.callbacks[event]; | ||
// remove specific handler | ||
i = callbacks.indexOf(fn); | ||
callbacks.splice(i, 1); | ||
if (callbacks.length === 0) { | ||
delete this.callbacks[event]; | ||
} | ||
return this; | ||
} | ||
|
||
// remove specific handler | ||
i = callbacks.indexOf(fn); | ||
callbacks.splice(i, 1); | ||
if (callbacks.length === 0) { | ||
delete this.callbacks[event]; | ||
} | ||
return this; | ||
}; | ||
}; | ||
|
||
/// Emit `event` with the given args. | ||
// also calls any `*` handlers | ||
WildEmitter.prototype.emit = function (event) { | ||
var args = [].slice.call(arguments, 1), | ||
callbacks = this.callbacks[event], | ||
specialCallbacks = this.getWildcardCallbacks(event), | ||
i, | ||
len, | ||
item, | ||
listeners; | ||
|
||
if (callbacks) { | ||
listeners = callbacks.slice(); | ||
for (i = 0, len = listeners.length; i < len; ++i) { | ||
if (!listeners[i]) { | ||
break; | ||
/// Emit `event` with the given args. | ||
// also calls any `*` handlers | ||
prototype.emit = function (event) { | ||
this.callbacks = this.callbacks || {}; | ||
var args = [].slice.call(arguments, 1), | ||
callbacks = this.callbacks[event], | ||
specialCallbacks = this.getWildcardCallbacks(event), | ||
i, | ||
len, | ||
item, | ||
listeners; | ||
|
||
if (callbacks) { | ||
listeners = callbacks.slice(); | ||
for (i = 0, len = listeners.length; i < len; ++i) { | ||
if (!listeners[i]) { | ||
break; | ||
} | ||
listeners[i].apply(this, args); | ||
} | ||
listeners[i].apply(this, args); | ||
} | ||
} | ||
|
||
if (specialCallbacks) { | ||
len = specialCallbacks.length; | ||
listeners = specialCallbacks.slice(); | ||
for (i = 0, len = listeners.length; i < len; ++i) { | ||
if (!listeners[i]) { | ||
break; | ||
|
||
if (specialCallbacks) { | ||
len = specialCallbacks.length; | ||
listeners = specialCallbacks.slice(); | ||
for (i = 0, len = listeners.length; i < len; ++i) { | ||
if (!listeners[i]) { | ||
break; | ||
} | ||
listeners[i].apply(this, [event].concat(args)); | ||
} | ||
listeners[i].apply(this, [event].concat(args)); | ||
} | ||
} | ||
|
||
return this; | ||
}; | ||
return this; | ||
}; | ||
|
||
// Helper for for finding special wildcard event handlers that match the event | ||
WildEmitter.prototype.getWildcardCallbacks = function (eventName) { | ||
var item, | ||
split, | ||
result = []; | ||
// Helper for for finding special wildcard event handlers that match the event | ||
prototype.getWildcardCallbacks = function (eventName) { | ||
this.callbacks = this.callbacks || {}; | ||
var item, | ||
split, | ||
result = []; | ||
|
||
for (item in this.callbacks) { | ||
split = item.split('*'); | ||
if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { | ||
result = result.concat(this.callbacks[item]); | ||
for (item in this.callbacks) { | ||
split = item.split('*'); | ||
if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { | ||
result = result.concat(this.callbacks[item]); | ||
} | ||
} | ||
} | ||
return result; | ||
return result; | ||
}; | ||
|
||
}; | ||
|
||
WildEmitter.mixin(WildEmitter); |