emittr icon indicating copy to clipboard operation
emittr copied to clipboard

Implement namespaces

Open talyssonoc opened this issue 9 years ago • 3 comments

It would be cool to support namespaces. Something like:

class User
  attr_reader :name

  def initialize(name:, emitter:)
    @name = name
    @emitter = emitter
  end

  def sign_in!
    # do something
    @emitter.emit('user::sign_in', self)
  end
end

emitter = Emittr::Emitter.new
user = User.new(name: 'Name for a bad example', emitter: emitter)

emitter.on 'user::*', do |event_name, u|
  puts "#{event_name} emitted for user #{u.name}"
end

talyssonoc avatar Aug 23 '16 02:08 talyssonoc

@rscardinho what do you think about it?

talyssonoc avatar Aug 23 '16 02:08 talyssonoc

It could also be implemented using a regex for the first parameter of on/once methods instead of a string. Maybe that would be even more flexible:

emitter = Emittr::Emitter.new

emitter.on /^user::\w+/ do |event_name|
   puts event_name
end

emitter.emit 'user::create'

#=> user::create

I would make it a little more verbose, perhaps.

talyssonoc avatar Aug 23 '16 02:08 talyssonoc

yeah, good idea. I've thought on something similar, but I forgot to write down anywhere. HEHE For me, the regex example is way more flexible than the string implementation, but what about both implementations? 🚀

rscardinho avatar Aug 23 '16 18:08 rscardinho