mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-15 03:04:37 +03:00
Add EventEmitter.subscriptionCount
This commit is contained in:
parent
e1283e1788
commit
5e5a5bdfa5
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user