Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Slice the events array before looping through it #26

Merged
merged 1 commit into from Sep 21, 2012
Jump to file or symbol
Failed to load files and symbols.
+19 −1
Split
View
@@ -33,7 +33,7 @@
EventEmitter.prototype.emit = function(name) {
var args = Array.prototype.slice.call(arguments, 1)
- var events = prepareEvent.call(this, name)
+ var events = prepareEvent.call(this, name).slice()
for (var i = 0, length = events.length; i < length; i++) {
var callback = events[i].callback
@@ -50,3 +50,21 @@ test("unbinding by callback and scope", 1, function() {
ee.emit("a")
})
+
+test("emitting events with callbacks that unbind events", function () {
+ var ee = new Model.EventEmitter,
+ eventName = 'test',
+ callbackCalled = false
+
+ var handler = function () {
+ ee.off(eventName, arguments.callee)
+ }
+
+ ee.on('test', handler)
+ ee.on('test', function () { callbackCalled = true })
+
+ ee.emit('test')
+
+ ok(callbackCalled)
+
+})