Class: Reru::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/reru/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(*sources) ⇒ Dispatcher

Returns a new instance of Dispatcher.



2
3
4
# File 'lib/reru/dispatcher.rb', line 2

def initialize(*sources)
  @sources = sources
end

Instance Method Details

#each(&sink) ⇒ Object



6
7
8
9
# File 'lib/reru/dispatcher.rb', line 6

def each(&sink)
  @sink = sink
  subscribe(*@sources)
end

#shutdown(source) ⇒ Object



23
24
25
# File 'lib/reru/dispatcher.rb', line 23

def shutdown(source)
  unsubscribe(source)
end

#subscribe(*sources) ⇒ Object



11
12
13
14
15
16
# File 'lib/reru/dispatcher.rb', line 11

def subscribe(*sources)
  sources.each do |source|
    @sources << source
    source.add_observer(self)
  end
end

#unsubscribe(source) ⇒ Object



27
28
29
30
# File 'lib/reru/dispatcher.rb', line 27

def unsubscribe(source)
  source.delete_observer(self)
  @sources.delete(source)
end

#update(source, event) ⇒ Object



18
19
20
21
# File 'lib/reru/dispatcher.rb', line 18

def update(source, event)
  @sink.call(event)
  shutdown(source) if event.eos?
end