Add EventEmitter.subscriptionCount

This commit is contained in:
Nathan Sobo 2012-03-13 19:05:24 -06:00
parent e1283e1788
commit 5e5a5bdfa5
2 changed files with 20 additions and 3 deletions

View File

@ -138,4 +138,14 @@ describe "EventEmitter mixin", ->
object.off '.garbage'
object.off 'junk.garbage'
describe ".subscriptionCount()", ->
it "returns the total number of subscriptions on the object", ->
expect(object.subscriptionCount()).toBe 3
object.on 'baz', ->
expect(object.subscriptionCount()).toBe 4
object.off 'foo'
expect(object.subscriptionCount()).toBe 2

View File

@ -28,12 +28,12 @@ module.exports =
if namespace
if eventName
handlers = @eventHandlersByNamespace[namespace]?[eventName] ? []
handlers = @eventHandlersByNamespace?[namespace]?[eventName] ? []
for handler in new Array(handlers...)
_.remove(handlers, handler)
@off eventName, handler
else
for eventName, handlers of @eventHandlersByNamespace[namespace] ? {}
for eventName, handlers of @eventHandlersByNamespace?[namespace] ? {}
for handler in new Array(handlers...)
_.remove(handlers, handler)
@off eventName, handler
@ -41,4 +41,11 @@ module.exports =
if handler
_.remove(@eventHandlersByEventName[eventName], handler)
else
delete @eventHandlersByEventName[eventName]
delete @eventHandlersByEventName?[eventName]
subscriptionCount: ->
count = 0
for name, handlers of @eventHandlersByEventName
count += handlers.length
count