Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Slice the events array before looping through it #26

Merged
merged 1 commit into from

2 participants

@olivernn

This fixes a bug where event handlers that mutate the events array cause index out of bounds errors.

@olivernn olivernn slice the events array before looping through it and calling event ha…
…ndlers

prevents errors from handlers that mutate the events array, e.g. by unbinding other events.
d02dc67
@benpickles benpickles merged commit 944c2da into benpickles:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 21, 2012
  1. @olivernn

    slice the events array before looping through it and calling event ha…

    olivernn authored
    …ndlers
    
    prevents errors from handlers that mutate the events array, e.g. by unbinding other events.
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 1 deletion.
  1. +1 −1  src/event_emitter.js
  2. +18 −0 test/tests/event_emitter_test.js
View
2  src/event_emitter.js
@@ -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
View
18 test/tests/event_emitter_test.js
@@ -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)
+
+})
Something went wrong with that request. Please try again.