timers 4.1.2 → 4.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/timers.rb +23 -3
- data/lib/timers/events.rb +138 -106
- data/lib/timers/group.rb +139 -119
- data/lib/timers/interval.rb +58 -0
- data/lib/timers/timer.rb +138 -117
- data/lib/timers/version.rb +21 -1
- data/lib/timers/wait.rb +62 -42
- metadata +29 -45
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -17
- data/.rspec +0 -6
- data/.rubocop.yml +0 -28
- data/.ruby-version +0 -1
- data/.travis.yml +0 -21
- data/AUTHORS.md +0 -15
- data/CHANGES.md +0 -62
- data/Gemfile +0 -19
- data/LICENSE +0 -23
- data/README.md +0 -113
- data/Rakefile +0 -10
- data/spec/spec_helper.rb +0 -21
- data/spec/timers/cancel_spec.rb +0 -45
- data/spec/timers/events_spec.rb +0 -56
- data/spec/timers/every_spec.rb +0 -33
- data/spec/timers/group_spec.rb +0 -255
- data/spec/timers/performance_spec.rb +0 -96
- data/spec/timers/strict_spec.rb +0 -36
- data/spec/timers/wait_spec.rb +0 -30
- data/timers.gemspec +0 -26
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
module Timers
|
24
|
+
# A collection of timers which may fire at different times
|
25
|
+
class Interval
|
26
|
+
# Get the current elapsed monotonic time.
|
27
|
+
def initialize
|
28
|
+
@total = 0.0
|
29
|
+
@current = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def start
|
33
|
+
return if @current
|
34
|
+
|
35
|
+
@current = now
|
36
|
+
end
|
37
|
+
|
38
|
+
def stop
|
39
|
+
return unless @current
|
40
|
+
|
41
|
+
@total += duration
|
42
|
+
|
43
|
+
@current = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_f
|
47
|
+
@total + duration
|
48
|
+
end
|
49
|
+
|
50
|
+
protected def duration
|
51
|
+
now - @current
|
52
|
+
end
|
53
|
+
|
54
|
+
protected def now
|
55
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/timers/timer.rb
CHANGED
@@ -1,129 +1,150 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
12
22
|
|
13
|
-
|
14
|
-
|
23
|
+
module Timers
|
24
|
+
# An individual timer set to fire a given proc at a given time. A timer is
|
25
|
+
# always connected to a Timer::Group but it would ONLY be in @group.timers
|
26
|
+
# if it also has a @handle specified. Otherwise it is either PAUSED or has
|
27
|
+
# been FIRED and is not recurring. You can manually enter this state by
|
28
|
+
# calling #cancel and resume normal operation by calling #reset.
|
29
|
+
class Timer
|
30
|
+
include Comparable
|
31
|
+
attr_reader :interval, :offset, :recurring
|
15
32
|
|
16
|
-
|
17
|
-
|
18
|
-
@block = block
|
19
|
-
@offset = offset
|
33
|
+
def initialize(group, interval, recurring = false, offset = nil, &block)
|
34
|
+
@group = group
|
20
35
|
|
21
|
-
|
36
|
+
@interval = interval
|
37
|
+
@recurring = recurring
|
38
|
+
@block = block
|
39
|
+
@offset = offset
|
40
|
+
|
41
|
+
@handle = nil
|
22
42
|
|
23
|
-
|
24
|
-
|
25
|
-
|
43
|
+
# If a start offset was supplied, use that, otherwise use the current timers offset.
|
44
|
+
reset(@offset || @group.current_offset)
|
45
|
+
end
|
26
46
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
47
|
+
def paused?
|
48
|
+
@group.paused_timers.include? self
|
49
|
+
end
|
50
|
+
|
51
|
+
def pause
|
52
|
+
return if paused?
|
53
|
+
|
54
|
+
@group.timers.delete self
|
55
|
+
@group.paused_timers.add self
|
56
|
+
|
57
|
+
@handle.cancel! if @handle
|
58
|
+
@handle = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def resume
|
62
|
+
return unless paused?
|
63
|
+
|
64
|
+
@group.paused_timers.delete self
|
65
|
+
|
66
|
+
# This will add us back to the group:
|
67
|
+
reset
|
68
|
+
end
|
69
|
+
|
70
|
+
alias continue resume
|
71
|
+
|
72
|
+
# Extend this timer
|
73
|
+
def delay(seconds)
|
74
|
+
@handle.cancel! if @handle
|
75
|
+
|
76
|
+
@offset += seconds
|
77
|
+
|
78
|
+
@handle = @group.events.schedule(@offset, self)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Cancel this timer. Do not call while paused.
|
82
|
+
def cancel
|
83
|
+
return unless @handle
|
84
|
+
|
85
|
+
@handle.cancel! if @handle
|
86
|
+
@handle = nil
|
87
|
+
|
88
|
+
# This timer is no longer valid:
|
89
|
+
@group.timers.delete self if @group
|
90
|
+
end
|
91
|
+
|
92
|
+
# Reset this timer. Do not call while paused.
|
93
|
+
# @param offset [Numeric] the duration to add to the timer.
|
94
|
+
def reset(offset = @group.current_offset)
|
95
|
+
# This logic allows us to minimise the interaction with @group.timers.
|
96
|
+
# A timer with a handle is always registered with the group.
|
97
|
+
if @handle
|
98
|
+
@handle.cancel!
|
99
|
+
else
|
100
|
+
@group.timers << self
|
101
|
+
end
|
102
|
+
|
103
|
+
@offset = Float(offset) + @interval
|
104
|
+
|
105
|
+
@handle = @group.events.schedule(@offset, self)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Fire the block.
|
109
|
+
def fire(offset = @group.current_offset)
|
110
|
+
if recurring == :strict
|
111
|
+
# ... make the next interval strictly the last offset + the interval:
|
112
|
+
reset(@offset)
|
113
|
+
elsif recurring
|
114
|
+
reset(offset)
|
115
|
+
else
|
116
|
+
@offset = offset
|
117
|
+
end
|
118
|
+
|
119
|
+
@block.call(offset, self)
|
120
|
+
|
121
|
+
cancel unless recurring
|
122
|
+
end
|
123
|
+
|
124
|
+
alias call fire
|
125
|
+
|
126
|
+
# Number of seconds until next fire / since last fire
|
127
|
+
def fires_in
|
128
|
+
@offset - @group.current_offset if @offset
|
129
|
+
end
|
109
130
|
|
110
|
-
|
111
|
-
|
112
|
-
|
131
|
+
# Inspect a timer
|
132
|
+
def inspect
|
133
|
+
str = "#{to_s[0..-2]} ".dup
|
113
134
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
135
|
+
if @offset
|
136
|
+
str << if fires_in >= 0
|
137
|
+
"fires in #{fires_in} seconds"
|
138
|
+
else
|
139
|
+
"fired #{fires_in.abs} seconds ago"
|
140
|
+
end
|
120
141
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
142
|
+
str << ", recurs every #{interval}" if recurring
|
143
|
+
else
|
144
|
+
str << "dead"
|
145
|
+
end
|
125
146
|
|
126
|
-
|
127
|
-
|
128
|
-
|
147
|
+
str << ">"
|
148
|
+
end
|
149
|
+
end
|
129
150
|
end
|
data/lib/timers/version.rb
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
3
23
|
module Timers
|
4
|
-
|
24
|
+
VERSION = "4.3.2"
|
5
25
|
end
|
data/lib/timers/wait.rb
CHANGED
@@ -1,47 +1,67 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require_relative "interval"
|
4
24
|
|
5
25
|
module Timers
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
26
|
+
# An exclusive, monotonic timeout class.
|
27
|
+
class Wait
|
28
|
+
def self.for(duration, &block)
|
29
|
+
if duration
|
30
|
+
timeout = new(duration)
|
31
|
+
|
32
|
+
timeout.while_time_remaining(&block)
|
33
|
+
else
|
34
|
+
loop do
|
35
|
+
yield(nil)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(duration)
|
41
|
+
@duration = duration
|
42
|
+
@remaining = true
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :duration
|
46
|
+
attr_reader :remaining
|
47
|
+
|
48
|
+
# Yields while time remains for work to be done:
|
49
|
+
def while_time_remaining
|
50
|
+
@interval = Interval.new
|
51
|
+
@interval.start
|
52
|
+
|
53
|
+
yield @remaining while time_remaining?
|
54
|
+
ensure
|
55
|
+
@interval.stop
|
56
|
+
@interval = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def time_remaining?
|
62
|
+
@remaining = (@duration - @interval.to_f)
|
63
|
+
|
64
|
+
@remaining > 0
|
65
|
+
end
|
66
|
+
end
|
47
67
|
end
|