class TokenOfFire::EventBus

Public Class Methods

new() click to toggle source
# File lib/token_of_fire/event_bus.rb, line 15
def initialize
  @subscriptions = TokenOfFire::Subscriptions.new
  @global_scope = TokenOfFire::Scope.new(Celluloid::Actor.current)
  @global_token = @global_scope.token
  self
end

Public Instance Methods

fire(event_name, payload, scope=nil) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 58
def fire(event_name, payload, scope=nil)
  scope ||= @global_scope.filter
  perform(event_name, scope, payload, :async)
end
fire_sync(event_name, payload, scope=nil) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 63
def fire_sync(event_name, payload, scope=nil)
  scope ||= @global_scope.filter
  perform(event_name, scope, payload, :sync)
end
make_event(event_name, scope, payload) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 68
def make_event(event_name, scope, payload)
  TokenOfFire::Event.new(event_name, scope, payload)
end
perform(event_name, scope, payload, sync_type) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 48
def perform(event_name, scope, payload, sync_type)
  event = make_event(event_name, scope, payload)
  case sync_type
    when :async
      async.trigger(event)
    when :sync
      trigger(event)
  end
end
subscribe(event_name, scope, handler, handler_method) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 22
def subscribe(event_name, scope, handler, handler_method)
  uuid = @subscriptions.subscribe(event_name, scope, handler, handler_method)
  # $stdout.puts "-- Subscribe event_name: #{event_name} #{uuid}"
  uuid
end
subscriptions() click to toggle source
# File lib/token_of_fire/event_bus.rb, line 33
def subscriptions
  @subscriptions.get_subscriptions
end
trigger(event) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 37
def trigger(event)
  subscriptions = @subscriptions.get_subscriptions(event.name, event.scope)
  if subscriptions and subscriptions.size > 0
    subscriptions.each_value do |subscription|
      subscription[:handler].send(subscription[:method_name], event.payload)
    end
  else
    # $stdout.puts "No subscriptions found for: scope: #{event.scope}, event_name: #{event.name}"
  end
end
unsubscribe(uuid) click to toggle source
# File lib/token_of_fire/event_bus.rb, line 28
def unsubscribe(uuid)
  # $stdout.puts "-- Unsubscribe uuid:#{uuid}"
  @subscriptions.unsubscribe(uuid)
end