lib/finite_machine/observer.rb in finite_machine-0.9.1 vs lib/finite_machine/observer.rb in finite_machine-0.9.2
- old
+ new
@@ -39,22 +39,24 @@
# @api public
# TODO: throw error if event type isn't handled
def on(event_type = HookEvent, *args, &callback)
sync_exclusive do
name, async, _ = args
- name = ANY_EVENT if name.nil?
+ if name.nil?
+ name = event_type < HookEvent::Anyaction ? ANY_EVENT : ANY_STATE
+ end
async = false if async.nil?
ensure_valid_callback_name!(event_type, name)
callback.extend(Async) if async == :async
hooks.register event_type, name, callback
end
end
# Unregister callback for a given event
#
# @api public
- def off(event_type = ANY_EVENT, name = ANY_STATE, &callback)
+ def off(event_type, name = ANY_STATE, &callback)
sync_exclusive do
hooks.unregister event_type, name, callback
end
end
@@ -105,12 +107,12 @@
# Trigger all listeners
#
# @api public
def trigger(event, *args, &block)
sync_exclusive do
- [event.type, ANY_EVENT].each do |event_type|
- [event.name, ANY_STATE].each do |event_name|
+ [event.type].each do |event_type|
+ [event.name, ANY_STATE, ANY_EVENT].each do |event_name|
hooks.call(event_type, event_name) do |hook|
handle_callback(hook, event)
off(event_type, event_name, &hook) if hook.is_a?(Once)
end
end
@@ -161,10 +163,10 @@
# @return [Array[Symbol]]
# valid callback names
#
# @api private
def callback_names
- machine.states + machine.event_names + [ANY_EVENT]
+ machine.states + machine.event_names + [ANY_EVENT, ANY_STATE]
end
# Forward the message to observer
#
# @param [String] method_name