Allow off to be called before on without error

Previously calling `something.off 'event-name', handler` would throw
an error unless `on` was called first.
This commit is contained in:
probablycorey 2013-04-22 11:48:28 -07:00 committed by Corey Johnson & Nathan Sobo
parent 7e9c39fd55
commit 9cc4c2e5de
2 changed files with 5 additions and 2 deletions

View File

@ -109,6 +109,9 @@ describe "EventEmitter mixin", ->
expect(fooHandler1).not.toHaveBeenCalled()
expect(fooHandler2).toHaveBeenCalled()
it "does not throw an exception if there was not matching `on` call", ->
expect(-> object.off 'marco', -> "nothing").not.toThrow()
describe "when there are namespaced event handlers", ->
[barHandler2, bazHandler1, bazHandler2, bazHandler3] = []

View File

@ -78,8 +78,8 @@ module.exports =
@off eventName, handler
else
subscriptionCountBefore = @subscriptionCount()
if handler
_.remove(@eventHandlersByEventName[eventName], handler)
if eventHandlers = @eventHandlersByEventName[eventName]
_.remove(eventHandlers, handler)
else
delete @eventHandlersByEventName?[eventName]
@afterUnsubscribe?() if @subscriptionCount() < subscriptionCountBefore