Permalink
Browse files

Check for empty events

  • Loading branch information...
Koen Bok
Koen Bok committed May 3, 2013
1 parent 3e2c579 commit 1ec6a885e272d13300688cd0efe953aef3adf1ae
Showing with 16 additions and 1 deletion.
  1. +16 −1 src/eventemitter.coffee
@@ -1,10 +1,17 @@
check = require "check-types"
eventCheck = (event) ->
check.verifyUnemptyString event, "Missing event type"
class exports.EventEmitter
constructor: ->
@events = {}
emit: (event, args...) ->
eventCheck event
if not @events?[event]
return
@@ -15,6 +22,8 @@ class exports.EventEmitter
# @emit "addListener", event, listener
eventCheck event
@events ?= {}
@events[event] ?= []
@events[event].push listener
@@ -23,21 +32,27 @@ class exports.EventEmitter
# @emit "removeListener", event, listener
check.verifyUnemptyString event
return unless @events
return unless @events[event]
@events[event] = (l for l in @events[event] when l isnt listener)
once: (event, listener) ->
eventCheck event
fn = =>
@removeListener event, fn
listener arguments...
@on event, fn
removeAllListeners: (event) ->
eventCheck event
return unless @events
return unless @events[event]

0 comments on commit 1ec6a88

Please sign in to comment.