choojs/nanobus

using `once` then `on` for the same eventName throws an error

greggraf opened this issue · 1 comments

If I use once() then on() to subscribe to the same eventName I get TypeError: listener is not a function.

I am pretty sure this happens because the listeners array is altered when the once() callback is triggered, but the loop logic does not take this into account.

This illustrates the use case:

var tape = require('tape')
var nanobus = require('./')

tape('nanobus', function (t) {
  t.test('should allow for subscribing to the same event using once() then on() ', function (t) {
    t.plan(2)
    var bus = nanobus()
    var i = 0

    bus.once('foo:bar', function () {
      console.log('I want to know about foo:bar')
      i++
    })
    bus.on('foo:bar', function () {
      console.log('I also want to know about foo:bar')
      i++
    })

    bus.emit('foo:bar')
    t.equal(i, 2, 'count 2')
    bus.emit('foo:bar')
    t.equal(i, 2, 'count 2')
  })
})

Closed in v2.2.2