Sha256: 0cb73bdffef52d75cdd7c8ae3f01776f95bf175a9ef6c2ca45ee21a871b0a685

Contents?: true

Size: 907 Bytes

Versions: 1

Compression:

Stored size: 907 Bytes

Contents

# A simple event registering/triggering module to mix into classes.
# Events are stored in the `@events` ivar.
module Eventable

  # Register a handler for the given event name.
  #
  #   obj.on(:foo) { puts "foo was called" }
  #
  # @param [String, Symbol] name event name
  # @return handler
  def on(name, &handler)
    @eventable ||= Hash.new { |hash, key| hash[key] = [] }
    @eventable[name] << handler
    handler
  end

  def off(name, handler)
    if @eventable and evts = @eventable[name]
      evts.delete handler
    end
  end

  # Trigger the given event name and passes all args to each handler
  # for this event.
  #
  #   obj.trigger(:foo)
  #   obj.trigger(:foo, 1, 2, 3)
  #
  # @param [String, Symbol] name event name to trigger
  def trigger(name, *args)
    @eventable ||= Hash.new { |hash, key| hash[key] = [] }
    @eventable[name].each { |handler| handler.call(*args) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-eventable-0.0.1 opal/opal-eventable.rb