AaronAcerboni/PubSub.as — comment on commit d2c4dc71dd835e944eca4bc19d3b8e5986a6336f
File: PubSub.as line 90
By aron:

@mroderick I think the documentation is fine, it's a nice library too. The issue is that unsubscribe() modifies the messages array directly using splice(). And unsubscribe() can be called by an event handler (for example in the case of one time callback mentioned above). Iterating over the messages loop is _syncronous_ so it's possible to unintentionally modify the array being iterated.

For [example](http://jsbin.com/avusog/2/):

```js
// This callback should only be called once then unbound, a common pattern.
var token = PubSub.subscribe('my-event', function () {
console.log('Callback One');
PubSub.unsubscribe(token);
});

// This is another callback.
PubSub.subscribe('my-event', function () {
console.log('Callback Two');
});

// Now we publish the event twice in quick succession.
PubSub.publish('my-event', 'hello world!');
PubSub.publish('my-event', 'hello world!');

// You would expect this to output.
//
// Callback One
// Callback Two
// Callback Two
//
// But instead you get.
//
// Callback One
// Callback Two
// Exception Thrown.
```

Now it's great that the exception was caught and did not affect the second publish call. But my point is the exception was caused by PubSubJS not handling the unsubscribe() call in the way I expected (unless this is the intended behavior and the developer should not call unsubscribe() in handlers).

I hope that all makes sense… (@AaronAcerboni sorry for hogging your commit)