Class: Takagi::Observer::Watcher
- Inherits:
-
Object
- Object
- Takagi::Observer::Watcher
- Defined in:
- lib/takagi/observer/watcher.rb
Overview
Periodically notifies observers by re-running registered handlers.
Instance Method Summary collapse
-
#initialize(interval: 1) ⇒ Watcher
constructor
A new instance of Watcher.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(interval: 1) ⇒ Watcher
Returns a new instance of Watcher.
7 8 9 10 11 |
# File 'lib/takagi/observer/watcher.rb', line 7 def initialize(interval: 1) @interval = interval @running = false @thread = nil end |
Instance Method Details
#start ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/takagi/observer/watcher.rb', line 13 def start return @thread if @running @running = true @thread = Thread.new do while @running Takagi::ObserveRegistry.subscription_paths.each do |path| observable_route = Takagi::Base.router.find_observable(path) next unless observable_route handler = observable_route.block current_value = handler.call(nil) Takagi::ObserveRegistry.notify(path, current_value) end sleep @interval end @thread rescue StandardError => e Takagi.logger.error "Observer Watcher Error: #{e.}" end end |
#stop ⇒ Object
36 37 38 39 40 |
# File 'lib/takagi/observer/watcher.rb', line 36 def stop @running = false @thread&.wakeup if @thread&.alive? @thread&.join(2) # Wait max 2 seconds end |