timers 4.2.0 → 4.3.3
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 +4 -4
- data/lib/timers.rb +23 -8
- data/lib/timers/events.rb +58 -56
- data/lib/timers/group.rb +42 -27
- data/lib/timers/interval.rb +20 -5
- data/lib/timers/priority_heap.rb +174 -0
- data/lib/timers/timer.rb +63 -45
- data/lib/timers/version.rb +21 -6
- data/lib/timers/wait.rb +28 -13
- metadata +25 -51
- data/.gitignore +0 -17
- data/.rspec +0 -3
- data/.travis.yml +0 -19
- data/Gemfile +0 -15
- data/README.md +0 -149
- data/Rakefile +0 -6
- data/spec/spec_helper.rb +0 -38
- data/spec/timers/cancel_spec.rb +0 -50
- data/spec/timers/events_spec.rb +0 -61
- data/spec/timers/every_spec.rb +0 -38
- data/spec/timers/group_spec.rb +0 -260
- data/spec/timers/performance_spec.rb +0 -125
- data/spec/timers/strict_spec.rb +0 -41
- data/spec/timers/wait_spec.rb +0 -35
- data/timers.gemspec +0 -33
data/lib/timers/timer.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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.
|
7
22
|
|
8
23
|
module Timers
|
9
24
|
# An individual timer set to fire a given proc at a given time. A timer is
|
@@ -14,67 +29,68 @@ module Timers
|
|
14
29
|
class Timer
|
15
30
|
include Comparable
|
16
31
|
attr_reader :interval, :offset, :recurring
|
17
|
-
|
32
|
+
|
18
33
|
def initialize(group, interval, recurring = false, offset = nil, &block)
|
19
34
|
@group = group
|
20
|
-
|
35
|
+
|
21
36
|
@interval = interval
|
22
37
|
@recurring = recurring
|
23
38
|
@block = block
|
24
39
|
@offset = offset
|
25
|
-
|
40
|
+
|
26
41
|
@handle = nil
|
27
|
-
|
42
|
+
|
28
43
|
# If a start offset was supplied, use that, otherwise use the current timers offset.
|
29
44
|
reset(@offset || @group.current_offset)
|
30
45
|
end
|
31
|
-
|
46
|
+
|
32
47
|
def paused?
|
33
48
|
@group.paused_timers.include? self
|
34
49
|
end
|
35
|
-
|
50
|
+
|
36
51
|
def pause
|
37
52
|
return if paused?
|
38
|
-
|
53
|
+
|
39
54
|
@group.timers.delete self
|
40
55
|
@group.paused_timers.add self
|
41
|
-
|
56
|
+
|
42
57
|
@handle.cancel! if @handle
|
43
58
|
@handle = nil
|
44
59
|
end
|
45
|
-
|
60
|
+
|
46
61
|
def resume
|
47
62
|
return unless paused?
|
48
|
-
|
63
|
+
|
49
64
|
@group.paused_timers.delete self
|
50
|
-
|
65
|
+
|
51
66
|
# This will add us back to the group:
|
52
67
|
reset
|
53
68
|
end
|
54
|
-
|
69
|
+
|
55
70
|
alias continue resume
|
56
|
-
|
71
|
+
|
57
72
|
# Extend this timer
|
58
73
|
def delay(seconds)
|
59
74
|
@handle.cancel! if @handle
|
60
|
-
|
75
|
+
|
61
76
|
@offset += seconds
|
62
|
-
|
77
|
+
|
63
78
|
@handle = @group.events.schedule(@offset, self)
|
64
79
|
end
|
65
|
-
|
80
|
+
|
66
81
|
# Cancel this timer. Do not call while paused.
|
67
82
|
def cancel
|
68
83
|
return unless @handle
|
69
|
-
|
84
|
+
|
70
85
|
@handle.cancel! if @handle
|
71
86
|
@handle = nil
|
72
|
-
|
87
|
+
|
73
88
|
# This timer is no longer valid:
|
74
89
|
@group.timers.delete self if @group
|
75
90
|
end
|
76
|
-
|
91
|
+
|
77
92
|
# Reset this timer. Do not call while paused.
|
93
|
+
# @param offset [Numeric] the duration to add to the timer.
|
78
94
|
def reset(offset = @group.current_offset)
|
79
95
|
# This logic allows us to minimise the interaction with @group.timers.
|
80
96
|
# A timer with a handle is always registered with the group.
|
@@ -83,12 +99,12 @@ module Timers
|
|
83
99
|
else
|
84
100
|
@group.timers << self
|
85
101
|
end
|
86
|
-
|
102
|
+
|
87
103
|
@offset = Float(offset) + @interval
|
88
|
-
|
104
|
+
|
89
105
|
@handle = @group.events.schedule(@offset, self)
|
90
106
|
end
|
91
|
-
|
107
|
+
|
92
108
|
# Fire the block.
|
93
109
|
def fire(offset = @group.current_offset)
|
94
110
|
if recurring == :strict
|
@@ -99,36 +115,38 @@ module Timers
|
|
99
115
|
else
|
100
116
|
@offset = offset
|
101
117
|
end
|
102
|
-
|
103
|
-
@block.call(offset)
|
104
|
-
|
118
|
+
|
119
|
+
@block.call(offset, self)
|
120
|
+
|
105
121
|
cancel unless recurring
|
106
122
|
end
|
107
|
-
|
123
|
+
|
108
124
|
alias call fire
|
109
|
-
|
125
|
+
|
110
126
|
# Number of seconds until next fire / since last fire
|
111
127
|
def fires_in
|
112
128
|
@offset - @group.current_offset if @offset
|
113
129
|
end
|
114
|
-
|
130
|
+
|
115
131
|
# Inspect a timer
|
116
132
|
def inspect
|
117
|
-
|
118
|
-
|
133
|
+
buffer = "#{to_s[0..-2]} ".dup
|
134
|
+
|
119
135
|
if @offset
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
136
|
+
if fires_in >= 0
|
137
|
+
buffer << "fires in #{fires_in} seconds"
|
138
|
+
else
|
139
|
+
buffer << "fired #{fires_in.abs} seconds ago"
|
140
|
+
end
|
141
|
+
|
142
|
+
buffer << ", recurs every #{interval}" if recurring
|
127
143
|
else
|
128
|
-
|
144
|
+
buffer << "dead"
|
129
145
|
end
|
130
|
-
|
131
|
-
|
146
|
+
|
147
|
+
buffer << ">"
|
148
|
+
|
149
|
+
return buffer
|
132
150
|
end
|
133
151
|
end
|
134
152
|
end
|
data/lib/timers/version.rb
CHANGED
@@ -1,10 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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.
|
7
22
|
|
8
23
|
module Timers
|
9
|
-
VERSION = "4.
|
24
|
+
VERSION = "4.3.3"
|
10
25
|
end
|
data/lib/timers/wait.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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.
|
7
22
|
|
8
23
|
require_relative "interval"
|
9
24
|
|
@@ -13,7 +28,7 @@ module Timers
|
|
13
28
|
def self.for(duration, &block)
|
14
29
|
if duration
|
15
30
|
timeout = new(duration)
|
16
|
-
|
31
|
+
|
17
32
|
timeout.while_time_remaining(&block)
|
18
33
|
else
|
19
34
|
loop do
|
@@ -21,31 +36,31 @@ module Timers
|
|
21
36
|
end
|
22
37
|
end
|
23
38
|
end
|
24
|
-
|
39
|
+
|
25
40
|
def initialize(duration)
|
26
41
|
@duration = duration
|
27
42
|
@remaining = true
|
28
43
|
end
|
29
|
-
|
44
|
+
|
30
45
|
attr_reader :duration
|
31
46
|
attr_reader :remaining
|
32
|
-
|
47
|
+
|
33
48
|
# Yields while time remains for work to be done:
|
34
49
|
def while_time_remaining
|
35
50
|
@interval = Interval.new
|
36
51
|
@interval.start
|
37
|
-
|
52
|
+
|
38
53
|
yield @remaining while time_remaining?
|
39
54
|
ensure
|
40
55
|
@interval.stop
|
41
56
|
@interval = nil
|
42
57
|
end
|
43
|
-
|
58
|
+
|
44
59
|
private
|
45
|
-
|
60
|
+
|
46
61
|
def time_remaining?
|
47
62
|
@remaining = (@duration - @interval.to_f)
|
48
|
-
|
63
|
+
|
49
64
|
@remaining > 0
|
50
65
|
end
|
51
66
|
end
|
metadata
CHANGED
@@ -1,94 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
- Tony Arcieri
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-02-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: covered
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
|
-
name:
|
43
|
+
name: rspec
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '0'
|
48
|
+
version: '3.0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: '0'
|
56
|
-
description:
|
57
|
-
using any API that accepts a timeout.
|
55
|
+
version: '3.0'
|
56
|
+
description:
|
58
57
|
email:
|
59
|
-
- samuel@codeotaku.com
|
60
|
-
- bascule@gmail.com
|
61
58
|
executables: []
|
62
59
|
extensions: []
|
63
60
|
extra_rdoc_files: []
|
64
61
|
files:
|
65
|
-
- ".gitignore"
|
66
|
-
- ".rspec"
|
67
|
-
- ".travis.yml"
|
68
|
-
- Gemfile
|
69
|
-
- README.md
|
70
|
-
- Rakefile
|
71
62
|
- lib/timers.rb
|
72
63
|
- lib/timers/events.rb
|
73
64
|
- lib/timers/group.rb
|
74
65
|
- lib/timers/interval.rb
|
66
|
+
- lib/timers/priority_heap.rb
|
75
67
|
- lib/timers/timer.rb
|
76
68
|
- lib/timers/version.rb
|
77
69
|
- lib/timers/wait.rb
|
78
|
-
- spec/spec_helper.rb
|
79
|
-
- spec/timers/cancel_spec.rb
|
80
|
-
- spec/timers/events_spec.rb
|
81
|
-
- spec/timers/every_spec.rb
|
82
|
-
- spec/timers/group_spec.rb
|
83
|
-
- spec/timers/performance_spec.rb
|
84
|
-
- spec/timers/strict_spec.rb
|
85
|
-
- spec/timers/wait_spec.rb
|
86
|
-
- timers.gemspec
|
87
70
|
homepage: https://github.com/socketry/timers
|
88
71
|
licenses:
|
89
72
|
- MIT
|
90
73
|
metadata: {}
|
91
|
-
post_install_message:
|
74
|
+
post_install_message:
|
92
75
|
rdoc_options: []
|
93
76
|
require_paths:
|
94
77
|
- lib
|
@@ -96,24 +79,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
79
|
requirements:
|
97
80
|
- - ">="
|
98
81
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
82
|
+
version: '0'
|
100
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
84
|
requirements:
|
102
85
|
- - ">="
|
103
86
|
- !ruby/object:Gem::Version
|
104
87
|
version: '0'
|
105
88
|
requirements: []
|
106
|
-
|
107
|
-
|
108
|
-
signing_key:
|
89
|
+
rubygems_version: 3.1.2
|
90
|
+
signing_key:
|
109
91
|
specification_version: 4
|
110
|
-
summary: Pure Ruby one-shot and periodic timers
|
111
|
-
test_files:
|
112
|
-
- spec/spec_helper.rb
|
113
|
-
- spec/timers/cancel_spec.rb
|
114
|
-
- spec/timers/events_spec.rb
|
115
|
-
- spec/timers/every_spec.rb
|
116
|
-
- spec/timers/group_spec.rb
|
117
|
-
- spec/timers/performance_spec.rb
|
118
|
-
- spec/timers/strict_spec.rb
|
119
|
-
- spec/timers/wait_spec.rb
|
92
|
+
summary: Pure Ruby one-shot and periodic timers.
|
93
|
+
test_files: []
|