timers 4.1.2 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rspec +1 -4
- data/.travis.yml +15 -17
- data/Gemfile +7 -11
- data/README.md +60 -24
- data/Rakefile +2 -6
- data/lib/timers.rb +5 -0
- data/lib/timers/events.rb +108 -106
- data/lib/timers/group.rb +124 -119
- data/lib/timers/interval.rb +43 -0
- data/lib/timers/timer.rb +121 -116
- data/lib/timers/version.rb +6 -1
- data/lib/timers/wait.rb +47 -42
- data/spec/spec_helper.rb +30 -13
- data/spec/timers/cancel_spec.rb +33 -28
- data/spec/timers/events_spec.rb +40 -35
- data/spec/timers/every_spec.rb +27 -22
- data/spec/timers/group_spec.rb +247 -242
- data/spec/timers/performance_spec.rb +83 -54
- data/spec/timers/strict_spec.rb +37 -32
- data/spec/timers/wait_spec.rb +22 -17
- data/timers.gemspec +27 -20
- metadata +29 -18
- data/.coveralls.yml +0 -1
- data/.rubocop.yml +0 -28
- data/.ruby-version +0 -1
- data/AUTHORS.md +0 -15
- data/CHANGES.md +0 -62
- data/LICENSE +0 -23
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
4
|
+
#
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
6
|
+
#
|
7
|
+
|
8
|
+
module Timers
|
9
|
+
# A collection of timers which may fire at different times
|
10
|
+
class Interval
|
11
|
+
# Get the current elapsed monotonic time.
|
12
|
+
def initialize
|
13
|
+
@total = 0.0
|
14
|
+
@current = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def start
|
18
|
+
return if @current
|
19
|
+
|
20
|
+
@current = now
|
21
|
+
end
|
22
|
+
|
23
|
+
def stop
|
24
|
+
return unless @current
|
25
|
+
|
26
|
+
@total += duration
|
27
|
+
|
28
|
+
@current = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_f
|
32
|
+
@total + duration
|
33
|
+
end
|
34
|
+
|
35
|
+
protected def duration
|
36
|
+
now - @current
|
37
|
+
end
|
38
|
+
|
39
|
+
protected def now
|
40
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/timers/timer.rb
CHANGED
@@ -1,129 +1,134 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
4
|
+
#
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
6
|
+
#
|
2
7
|
|
3
8
|
module Timers
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
# An individual timer set to fire a given proc at a given time. A timer is
|
10
|
+
# always connected to a Timer::Group but it would ONLY be in @group.timers
|
11
|
+
# if it also has a @handle specified. Otherwise it is either PAUSED or has
|
12
|
+
# been FIRED and is not recurring. You can manually enter this state by
|
13
|
+
# calling #cancel and resume normal operation by calling #reset.
|
14
|
+
class Timer
|
15
|
+
include Comparable
|
16
|
+
attr_reader :interval, :offset, :recurring
|
12
17
|
|
13
|
-
|
14
|
-
|
18
|
+
def initialize(group, interval, recurring = false, offset = nil, &block)
|
19
|
+
@group = group
|
15
20
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
21
|
+
@interval = interval
|
22
|
+
@recurring = recurring
|
23
|
+
@block = block
|
24
|
+
@offset = offset
|
20
25
|
|
21
|
-
|
26
|
+
@handle = nil
|
22
27
|
|
23
|
-
|
24
|
-
|
25
|
-
|
28
|
+
# If a start offset was supplied, use that, otherwise use the current timers offset.
|
29
|
+
reset(@offset || @group.current_offset)
|
30
|
+
end
|
26
31
|
|
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
|
-
|
32
|
+
def paused?
|
33
|
+
@group.paused_timers.include? self
|
34
|
+
end
|
35
|
+
|
36
|
+
def pause
|
37
|
+
return if paused?
|
38
|
+
|
39
|
+
@group.timers.delete self
|
40
|
+
@group.paused_timers.add self
|
41
|
+
|
42
|
+
@handle.cancel! if @handle
|
43
|
+
@handle = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def resume
|
47
|
+
return unless paused?
|
48
|
+
|
49
|
+
@group.paused_timers.delete self
|
50
|
+
|
51
|
+
# This will add us back to the group:
|
52
|
+
reset
|
53
|
+
end
|
54
|
+
|
55
|
+
alias continue resume
|
56
|
+
|
57
|
+
# Extend this timer
|
58
|
+
def delay(seconds)
|
59
|
+
@handle.cancel! if @handle
|
60
|
+
|
61
|
+
@offset += seconds
|
62
|
+
|
63
|
+
@handle = @group.events.schedule(@offset, self)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Cancel this timer. Do not call while paused.
|
67
|
+
def cancel
|
68
|
+
return unless @handle
|
69
|
+
|
70
|
+
@handle.cancel! if @handle
|
71
|
+
@handle = nil
|
72
|
+
|
73
|
+
# This timer is no longer valid:
|
74
|
+
@group.timers.delete self if @group
|
75
|
+
end
|
76
|
+
|
77
|
+
# Reset this timer. Do not call while paused.
|
78
|
+
def reset(offset = @group.current_offset)
|
79
|
+
# This logic allows us to minimise the interaction with @group.timers.
|
80
|
+
# A timer with a handle is always registered with the group.
|
81
|
+
if @handle
|
82
|
+
@handle.cancel!
|
83
|
+
else
|
84
|
+
@group.timers << self
|
85
|
+
end
|
86
|
+
|
87
|
+
@offset = Float(offset) + @interval
|
88
|
+
|
89
|
+
@handle = @group.events.schedule(@offset, self)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Fire the block.
|
93
|
+
def fire(offset = @group.current_offset)
|
94
|
+
if recurring == :strict
|
95
|
+
# ... make the next interval strictly the last offset + the interval:
|
96
|
+
reset(@offset)
|
97
|
+
elsif recurring
|
98
|
+
reset(offset)
|
99
|
+
else
|
100
|
+
@offset = offset
|
101
|
+
end
|
102
|
+
|
103
|
+
@block.call(offset)
|
104
|
+
|
105
|
+
cancel unless recurring
|
106
|
+
end
|
107
|
+
|
108
|
+
alias call fire
|
109
|
+
|
110
|
+
# Number of seconds until next fire / since last fire
|
111
|
+
def fires_in
|
112
|
+
@offset - @group.current_offset if @offset
|
113
|
+
end
|
109
114
|
|
110
|
-
|
111
|
-
|
112
|
-
|
115
|
+
# Inspect a timer
|
116
|
+
def inspect
|
117
|
+
str = "#{to_s[0..-2]} ".dup
|
113
118
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
119
|
+
if @offset
|
120
|
+
str << if fires_in >= 0
|
121
|
+
"fires in #{fires_in} seconds"
|
122
|
+
else
|
123
|
+
"fired #{fires_in.abs} seconds ago"
|
124
|
+
end
|
120
125
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
126
|
+
str << ", recurs every #{interval}" if recurring
|
127
|
+
else
|
128
|
+
str << "dead"
|
129
|
+
end
|
125
130
|
|
126
|
-
|
127
|
-
|
128
|
-
|
131
|
+
str << ">"
|
132
|
+
end
|
133
|
+
end
|
129
134
|
end
|
data/lib/timers/version.rb
CHANGED
data/lib/timers/wait.rb
CHANGED
@@ -1,47 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
4
|
+
#
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
6
|
+
#
|
2
7
|
|
3
|
-
|
8
|
+
require_relative "interval"
|
4
9
|
|
5
10
|
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
|
-
|
11
|
+
# An exclusive, monotonic timeout class.
|
12
|
+
class Wait
|
13
|
+
def self.for(duration, &block)
|
14
|
+
if duration
|
15
|
+
timeout = new(duration)
|
16
|
+
|
17
|
+
timeout.while_time_remaining(&block)
|
18
|
+
else
|
19
|
+
loop do
|
20
|
+
yield(nil)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(duration)
|
26
|
+
@duration = duration
|
27
|
+
@remaining = true
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :duration
|
31
|
+
attr_reader :remaining
|
32
|
+
|
33
|
+
# Yields while time remains for work to be done:
|
34
|
+
def while_time_remaining
|
35
|
+
@interval = Interval.new
|
36
|
+
@interval.start
|
37
|
+
|
38
|
+
yield @remaining while time_remaining?
|
39
|
+
ensure
|
40
|
+
@interval.stop
|
41
|
+
@interval = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def time_remaining?
|
47
|
+
@remaining = (@duration - @interval.to_f)
|
48
|
+
|
49
|
+
@remaining > 0
|
50
|
+
end
|
51
|
+
end
|
47
52
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,21 +1,38 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
4
|
+
#
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
6
|
+
#
|
2
7
|
|
3
|
-
|
4
|
-
|
8
|
+
# Level of accuracy enforced by tests (50ms)
|
9
|
+
TIMER_QUANTUM = 0.05
|
10
|
+
|
11
|
+
if ENV['COVERAGE'] || ENV['TRAVIS']
|
12
|
+
begin
|
13
|
+
require 'simplecov'
|
14
|
+
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter "/spec/"
|
17
|
+
end
|
18
|
+
|
19
|
+
if ENV['TRAVIS']
|
20
|
+
require 'coveralls'
|
21
|
+
Coveralls.wear!
|
22
|
+
end
|
23
|
+
rescue LoadError
|
24
|
+
warn "Could not load simplecov: #{$!}"
|
25
|
+
end
|
26
|
+
end
|
5
27
|
|
6
28
|
require "bundler/setup"
|
7
29
|
require "timers"
|
8
30
|
|
9
|
-
# Level of accuracy enforced by tests (50ms)
|
10
|
-
TIMER_QUANTUM = 0.05
|
11
|
-
|
12
31
|
RSpec.configure do |config|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# https://relishapp.com/rspec/rspec-core/v/3-0/docs/configuration/global-namespace-dsl
|
20
|
-
config.expose_dsl_globally = false
|
32
|
+
# Enable flags like --only-failures and --next-failure
|
33
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
34
|
+
|
35
|
+
config.expect_with :rspec do |c|
|
36
|
+
c.syntax = :expect
|
37
|
+
end
|
21
38
|
end
|