Skip to content

Commit

Permalink
fix .once to work with overriden .on
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodo Digital committed Jan 18, 2017
1 parent cb64db6 commit cfd46e0
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/longjohn.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,25 @@ EventEmitter.prototype.addListener = (event, callback) ->

EventEmitter.prototype.on = (event, callback) ->
args = Array::slice.call(arguments)
args[1] = wrap_callback(callback, 'EventEmitter.on')
_on.apply(this, args)

EventEmitter.prototype.once = (event, callback) ->
args = Array::slice.call(arguments)
if typeof callback != 'function'
throw TypeError('callback must be a function');
fired = false
wrap = wrap_callback(callback, 'EventEmitter.once');
g = () ->
this.removeListener(event, g)
if !fired
fired = true
wrap.apply(this, arguments)
g.listener = callback
# Coming from EventEmitter.prototype.once
if callback.listener
wrap = wrap_callback(callback.listener, 'EventEmitter.once');

g = () ->
this.removeListener(event, g)

if !fired
fired = true
wrap.apply(this, arguments)

g.listener = callback.listener;
else
g = wrap_callback(callback, 'EventEmitter.on')

args[1] = g;

_on.apply(this, args)
return this;

EventEmitter.prototype.listeners = (event) ->
listeners = _listeners.call(this, event)
Expand Down

0 comments on commit cfd46e0

Please sign in to comment.