Class: Chamomile::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/chamomile/components/timer.rb

Overview

Countdown timer with timeout notification and tick-based updates.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout:, interval: 1.0) ⇒ Timer

Returns a new instance of Timer.



27
28
29
30
31
32
33
34
# File 'lib/chamomile/components/timer.rb', line 27

def initialize(timeout:, interval: 1.0)
  @id = self.class.next_id
  @timeout = timeout.to_f
  @interval = interval
  @remaining = @timeout
  @tag = 0
  @running = false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/chamomile/components/timer.rb', line 25

def id
  @id
end

#intervalObject (readonly)

Returns the value of attribute interval.



25
26
27
# File 'lib/chamomile/components/timer.rb', line 25

def interval
  @interval
end

#remainingObject (readonly)

Returns the value of attribute remaining.



25
26
27
# File 'lib/chamomile/components/timer.rb', line 25

def remaining
  @remaining
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



25
26
27
# File 'lib/chamomile/components/timer.rb', line 25

def timeout
  @timeout
end

Class Method Details

.next_idObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chamomile/components/timer.rb', line 13

def self.next_id
  @id_mutex.synchronize do
    if Process.pid != @id_pid
      @id_pid = Process.pid
      @next_id = 0
      @id_mutex = Mutex.new
    end
    @next_id += 1
    "#{@id_pid}-tm-#{@next_id}"
  end
end

Instance Method Details

#handle(msg) ⇒ Object Also known as: update



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chamomile/components/timer.rb', line 73

def handle(msg)
  case msg
  when TimerTickMsg
    return unless msg.id == @id && msg.tag == @tag

    @remaining = [(@remaining - @interval), 0.0].max
    @tag += 1

    if @remaining <= 0
      @running = false
      timeout_cmd
    else
      tick_cmd
    end
  end
end

#resetObject



58
59
60
61
62
63
# File 'lib/chamomile/components/timer.rb', line 58

def reset
  @remaining = @timeout
  @running = false
  @tag += 1
  self
end

#running?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/chamomile/components/timer.rb', line 65

def running?
  @running
end

#start_cmdObject



36
37
38
39
40
41
# File 'lib/chamomile/components/timer.rb', line 36

def start_cmd
  return nil if @running || timed_out?

  @running = true
  tick_cmd
end

#stopObject



43
44
45
46
47
# File 'lib/chamomile/components/timer.rb', line 43

def stop
  @running = false
  @tag += 1
  self
end

#timed_out?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/chamomile/components/timer.rb', line 69

def timed_out?
  @remaining <= 0
end

#toggleObject



49
50
51
52
53
54
55
56
# File 'lib/chamomile/components/timer.rb', line 49

def toggle
  if @running
    stop
    nil
  else
    start_cmd
  end
end

#viewObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chamomile/components/timer.rb', line 92

def view
  total = @remaining.ceil.to_i
  total = 0 if total.negative?
  hours = total / 3600
  minutes = (total % 3600) / 60
  seconds = total % 60

  if hours.positive?
    format("%d:%02d:%02d", hours, minutes, seconds)
  else
    format("%02d:%02d", minutes, seconds)
  end
end