choojs/nanobus

Allow symbols for event keys

Closed this issue · 1 comments

My idea is to pass available events between modules. Right now I'm doing

store module

const addMessage = 'addMessage'
export const events = { addMessage }
export const store = (state, emitter) {
  emitter.on(addMessage, () => { ... })
}

view module

import { events } from './module.mjs'
export const view = (state, emit) => html`
   <button onclick=${() => emit(events.addMessage)}>click me </button>`

But I really want to make use of symbols (really not sure why) so that I can write instead in my store module

const addMessage = Symbol('addMessage')

This would only require a change to a couple of assertions, so that

assert.equal(typeof eventName, 'string', 'nanobus.removeListener: eventName should be type string')

reads

const allowedKeyTypes = ['string', 'symbol']
assert.ok(allowedKeyTypes.includes(typeof eventName), 'nanobus.removeListener: eventName should be type string')

I'd be happy to create a pull request if that is something you'd accept.

Cheers!

typeof name === 'string' || typeof name === 'symbol' would be good I think. .includes isn't supported everywhere