Sha256: 1d1cda381ef788a4d3e69fa29cc8512548185bd1dcd30c2b0ae341ee26a0755f

Contents?: true

Size: 1020 Bytes

Versions: 1

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

require 'observe_event'

# A simple "timer" class
class Timer
  extend ObserveEvent

  event :tick             # Define publisher/observable `tick`
  event_accessor :running # Define publisher/observable `running_changed`, which notifies it's subscribers/observers

  def state
    @running ? :running : :stopped
  end
end

timer = Timer.new

# Define subscriber to `running_changed` on the Timer instance
timer.running_changed do |from, to|
  puts "Timer#running changed from `#{from.inspect}` to `#{to.inspect}`"
  puts "Timer #{timer.state}"
end

# Define subscribers to `tick` on the Timer instance
timer.tick do
  puts 'Tick' if timer.running
end

timer.tick do
  puts 'Tock' if timer.running
end

timer.tick
timer.tick
timer.running = true
timer.tick
timer.tick
timer.running = false
timer.tick
timer.tick

# => Timer#running changed from `nil` to `true`
# => Timer running
# => Tick
# => Tock
# => Tick
# => Tock
# => Timer#running changed from `true` to `false`
# => Timer stopped

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
observe_event-0.1.0 examples/simple.rb