Class: Wakame::EventDispatcher

Inherits:
Object
  • Object
show all
Includes:
ThreadImmutable
Defined in:
lib/wakame/event_dispatcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreadImmutable

#bind_thread, included, #target_thread, #target_thread?, #thread_check

Constructor Details

#initializeEventDispatcher

Returns a new instance of EventDispatcher.



34
35
36
37
38
39
# File 'lib/wakame/event_dispatcher.rb', line 34

def initialize
  @event_handlers = {}
  @tickets = {}

  @unsubscribe_queue = []
end

Class Method Details

.fire_event(event_obj) ⇒ Object



22
23
24
# File 'lib/wakame/event_dispatcher.rb', line 22

def fire_event(event_obj)
  self.instance.fire_event(event_obj)
end

.instanceObject



6
7
8
9
10
11
# File 'lib/wakame/event_dispatcher.rb', line 6

def instance
  if @instance.nil?
    @instance = self.new
  end
  @instance
end

.resetObject



26
27
28
# File 'lib/wakame/event_dispatcher.rb', line 26

def reset
  @instance = nil
end

.subscribe(event_class, *args, &blk) ⇒ Object



13
14
15
# File 'lib/wakame/event_dispatcher.rb', line 13

def subscribe(event_class, *args, &blk)
  self.instance.subscribe(event_class, *args, &blk)
end

.unsubscribe(event_class) ⇒ Object



17
18
19
# File 'lib/wakame/event_dispatcher.rb', line 17

def unsubscribe(event_class)
  self.instance.unsubscribe(event_class)
end

Instance Method Details

#fire_event(event_obj) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wakame/event_dispatcher.rb', line 89

def fire_event(event_obj)
  raise ArgumentError unless event_obj.is_a?(Event::Base)
  log_msg = ""
  log_msg = " #{event_obj.log_message}" unless event_obj.log_message.nil?

  Wakame.log.debug("Event #{event_obj.class} has been fired:" + log_msg )
  tlist = @event_handlers[event_obj.class]
  return if tlist.nil?

  run_callbacks = proc {
    @unsubscribe_queue.each { |t|
      @tickets.delete(t)
      tlist.delete(t)
    }

    tlist.each { |t|
      ary = @tickets[t]
      c = ary[1]
      if c.nil?
        next
      end
      
      begin 
        c.call(event_obj)
      rescue => e
        Wakame.log.error(e)
        #raise e
      end
    }
    
    @unsubscribe_queue.each { |t|
      @tickets.delete(t)
      tlist.delete(t)
    }
  }

  #@handler_run_queue.push(run_handlers)
  
  ::EventMachine.barrier {
    begin 
      run_callbacks.call
    rescue => e
      Wakame.log.error(e)
    end
  }
end

#reset(event_class = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/wakame/event_dispatcher.rb', line 136

def reset(event_class=nil)
  if event_class.nil?
    @event_handlers.clear
    @tickets.clear
  else
    unless @event_handlers[event_class.to_s].nil?
      @event_handlers[event_class.to_s].each_key { |k|
        @tickets.delete(k)
      }
      @event_handlers[event_class.to_s].clear
    end
  end
end

#subscribe(event_class, *args, &blk) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wakame/event_dispatcher.rb', line 41

def subscribe(event_class, *args, &blk)
  event_class = case event_class
                when Class
                  event_class
                when String
                  Util.str2const(event_class)
                else
                  raise ArgumentError, "event_class has to be a form of String or Class type"
                end

  EM.barrier {
  tlist = @event_handlers[event_class]
  if tlist.nil?
    tlist = @event_handlers[event_class] = []
  end
  
  tickets = []
  args.each { |o|
    tickets << Util.gen_id
    @tickets.store(tickets.last, [event_class, o])
    tlist << tickets.last
  }
  
  if blk
    tickets << Util.gen_id
    @tickets.store(tickets.last, [event_class, blk])
    tlist << tickets.last
  end

    # Return in array if num of ticket to be returned is more than or equal 2.
    tickets.size > 1 ? tickets : tickets.first
  }
end

#unsubscribe(ticket) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wakame/event_dispatcher.rb', line 75

def unsubscribe(ticket)
  unless @tickets.has_key?(ticket)
    #Wakame.log.warn("EventHander.unsubscribe(#{ticket}) has been tried but the ticket was not registered.")
    return nil
  end
  
  EM.barrier {
    Wakame.log.debug("#{self.class}.unsubscribe(#{ticket})")

    @unsubscribe_queue << ticket
    ticket
  }
end