Class: TokenOfFire::EventBus

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/token_of_fire/event_bus.rb

Instance Method Summary collapse

Constructor Details

#initializeEventBus

Returns a new instance of EventBus.



10
11
12
13
# File 'lib/token_of_fire/event_bus.rb', line 10

def initialize
  @subscriptions = TokenOfFire::Subscriptions.new
  self
end

Instance Method Details

#fire(event_name, scope, payload) ⇒ Object



55
56
57
# File 'lib/token_of_fire/event_bus.rb', line 55

def fire(event_name, scope, payload)
  perform(event_name, scope, payload, :async)
end

#fire_sync(event_name, scope, payload) ⇒ Object



59
60
61
# File 'lib/token_of_fire/event_bus.rb', line 59

def fire_sync(event_name, scope, payload)
  perform(event_name, scope, payload, :sync)
end

#make_event(event_name, scope, payload) ⇒ Object



63
64
65
# File 'lib/token_of_fire/event_bus.rb', line 63

def make_event(event_name, scope, payload)
  TokenOfFire::Event.new(event_name, scope, payload)
end

#perform(event_name, scope, payload, sync_type) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/token_of_fire/event_bus.rb', line 45

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

#scope(filter = {}) ⇒ Object



15
16
17
# File 'lib/token_of_fire/event_bus.rb', line 15

def scope(filter={})
  TokenOfFire::Scope.new(Celluloid::Actor.current, filter, true)
end

#subscribe(event_name, scope, handler, handler_method) ⇒ Object



19
20
21
22
23
# File 'lib/token_of_fire/event_bus.rb', line 19

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

#subscriptionsObject



30
31
32
# File 'lib/token_of_fire/event_bus.rb', line 30

def subscriptions
  @subscriptions.get_subscriptions
end

#trigger(event) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/token_of_fire/event_bus.rb', line 34

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) ⇒ Object



25
26
27
28
# File 'lib/token_of_fire/event_bus.rb', line 25

def unsubscribe(uuid)
  # $stdout.puts "-- Unsubscribe uuid:#{uuid}"
  @subscriptions.unsubscribe(uuid)
end