timers 4.1.2 → 4.3.2
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 52ef1e26aab6a7bbbf5c9d687028bdc36f52810dd7ebc0c76873b6bef9f30ca2
|
4
|
+
data.tar.gz: 26105fe04a6d2bfcdf9efb46ebd83d9562a08af3c2f113892a8be57713be6112
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a3ae0e0d9644627fef3aeffdfde86456d74c28ab89194486743fee2a25e8832c5aafb05f88174f6ee27d4f9402e05b4f80776c49cc34d70da527e63f9097685
|
7
|
+
data.tar.gz: 1f30b97aa11f6f42b3ce1786ae42f06334a86064306601635774b8d84cc88df765e8312a455798984520d44e3db35f8094fb23ec35cb736eb2191bfa27dece0c
|
data/lib/timers.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
# Copyright, 2020, 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.
|
4
22
|
|
5
|
-
|
6
|
-
|
23
|
+
require_relative "timers/version"
|
24
|
+
|
25
|
+
require_relative "timers/group"
|
26
|
+
require_relative "timers/wait"
|
data/lib/timers/events.rb
CHANGED
@@ -1,111 +1,143 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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 "timer"
|
7
24
|
|
8
25
|
module Timers
|
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
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
26
|
+
# Maintains an ordered list of events, which can be cancelled.
|
27
|
+
class Events
|
28
|
+
# Represents a cancellable handle for a specific timer event.
|
29
|
+
class Handle
|
30
|
+
def initialize(time, callback)
|
31
|
+
@time = time
|
32
|
+
@callback = callback
|
33
|
+
end
|
34
|
+
|
35
|
+
# The absolute time that the handle should be fired at.
|
36
|
+
attr_reader :time
|
37
|
+
|
38
|
+
# Cancel this timer, O(1).
|
39
|
+
def cancel!
|
40
|
+
# The simplest way to keep track of cancelled status is to nullify the
|
41
|
+
# callback. This should also be optimal for garbage collection.
|
42
|
+
@callback = nil
|
43
|
+
end
|
44
|
+
|
45
|
+
# Has this timer been cancelled? Cancelled timer's don't fire.
|
46
|
+
def cancelled?
|
47
|
+
@callback.nil?
|
48
|
+
end
|
49
|
+
|
50
|
+
def > other
|
51
|
+
@time > other.to_f
|
52
|
+
end
|
53
|
+
|
54
|
+
def >= other
|
55
|
+
@time >= other.to_f
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_f
|
59
|
+
@time
|
60
|
+
end
|
61
|
+
|
62
|
+
# Fire the callback if not cancelled with the given time parameter.
|
63
|
+
def fire(time)
|
64
|
+
@callback.call(time) if @callback
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def initialize
|
69
|
+
# A sequence of handles, maintained in sorted order, future to present.
|
70
|
+
# @sequence.last is the next event to be fired.
|
71
|
+
@sequence = []
|
72
|
+
@queue = []
|
73
|
+
end
|
74
|
+
|
75
|
+
# Add an event at the given time.
|
76
|
+
def schedule(time, callback)
|
77
|
+
handle = Handle.new(time.to_f, callback)
|
78
|
+
|
79
|
+
@queue << handle
|
80
|
+
|
81
|
+
return handle
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns the first non-cancelled handle.
|
85
|
+
def first
|
86
|
+
merge!
|
87
|
+
|
88
|
+
while (handle = @sequence.last)
|
89
|
+
return handle unless handle.cancelled?
|
90
|
+
@sequence.pop
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the number of pending (possibly cancelled) events.
|
95
|
+
def size
|
96
|
+
@sequence.size + @queue.size
|
97
|
+
end
|
98
|
+
|
99
|
+
# Fire all handles for which Handle#time is less than the given time.
|
100
|
+
def fire(time)
|
101
|
+
merge!
|
102
|
+
|
103
|
+
while handle = @sequence.last and handle.time <= time
|
104
|
+
@sequence.pop
|
105
|
+
handle.fire(time)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def merge!
|
112
|
+
while handle = @queue.pop
|
113
|
+
next if handle.cancelled?
|
114
|
+
|
115
|
+
index = bisect_right(@sequence, handle)
|
116
|
+
|
117
|
+
if current_handle = @sequence[index] and current_handle.cancelled?
|
118
|
+
# puts "Replacing handle at index: #{index} due to cancellation in array containing #{@sequence.size} item(s)."
|
119
|
+
@sequence[index] = handle
|
120
|
+
else
|
121
|
+
# puts "Inserting handle at index: #{index} in array containing #{@sequence.size} item(s)."
|
122
|
+
@sequence.insert(index, handle)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Return the right-most index where to insert item e, in a list a, assuming
|
128
|
+
# a is sorted in descending order.
|
129
|
+
def bisect_right(a, e, l = 0, u = a.length)
|
130
|
+
while l < u
|
131
|
+
m = l + (u - l).div(2)
|
132
|
+
|
133
|
+
if a[m] >= e
|
134
|
+
l = m + 1
|
135
|
+
else
|
136
|
+
u = m
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
l
|
141
|
+
end
|
142
|
+
end
|
111
143
|
end
|
data/lib/timers/group.rb
CHANGED
@@ -1,127 +1,147 @@
|
|
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
|
require "set"
|
4
24
|
require "forwardable"
|
5
|
-
require "hitimes"
|
6
25
|
|
7
|
-
|
8
|
-
|
26
|
+
require_relative "interval"
|
27
|
+
require_relative "timer"
|
28
|
+
require_relative "events"
|
9
29
|
|
10
30
|
module Timers
|
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
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
31
|
+
# A collection of timers which may fire at different times
|
32
|
+
class Group
|
33
|
+
include Enumerable
|
34
|
+
|
35
|
+
extend Forwardable
|
36
|
+
def_delegators :@timers, :each, :empty?
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@events = Events.new
|
40
|
+
|
41
|
+
@timers = Set.new
|
42
|
+
@paused_timers = Set.new
|
43
|
+
|
44
|
+
@interval = Interval.new
|
45
|
+
@interval.start
|
46
|
+
end
|
47
|
+
|
48
|
+
# Scheduled events:
|
49
|
+
attr_reader :events
|
50
|
+
|
51
|
+
# Active timers:
|
52
|
+
attr_reader :timers
|
53
|
+
|
54
|
+
# Paused timers:
|
55
|
+
attr_reader :paused_timers
|
56
|
+
|
57
|
+
# Call the given block after the given interval. The first argument will be
|
58
|
+
# the time at which the group was asked to fire timers for.
|
59
|
+
def after(interval, &block)
|
60
|
+
Timer.new(self, interval, false, &block)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Call the given block immediately, and then after the given interval. The first
|
64
|
+
# argument will be the time at which the group was asked to fire timers for.
|
65
|
+
def now_and_after(interval, &block)
|
66
|
+
yield
|
67
|
+
after(interval, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Call the given block periodically at the given interval. The first
|
71
|
+
# argument will be the time at which the group was asked to fire timers for.
|
72
|
+
def every(interval, recur = true, &block)
|
73
|
+
Timer.new(self, interval, recur, &block)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Call the given block immediately, and then periodically at the given interval. The first
|
77
|
+
# argument will be the time at which the group was asked to fire timers for.
|
78
|
+
def now_and_every(interval, recur = true, &block)
|
79
|
+
yield
|
80
|
+
every(interval, recur, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Wait for the next timer and fire it. Can take a block, which should behave
|
84
|
+
# like sleep(n), except that n may be nil (sleep forever) or a negative
|
85
|
+
# number (fire immediately after return).
|
86
|
+
def wait
|
87
|
+
if block_given?
|
88
|
+
yield wait_interval
|
89
|
+
|
90
|
+
while (interval = wait_interval) && interval > 0
|
91
|
+
yield interval
|
92
|
+
end
|
93
|
+
else
|
94
|
+
while (interval = wait_interval) && interval > 0
|
95
|
+
# We cannot assume that sleep will wait for the specified time, it might be +/- a bit.
|
96
|
+
sleep interval
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
fire
|
101
|
+
end
|
102
|
+
|
103
|
+
# Interval to wait until when the next timer will fire.
|
104
|
+
# - nil: no timers
|
105
|
+
# - -ve: timers expired already
|
106
|
+
# - 0: timers ready to fire
|
107
|
+
# - +ve: timers waiting to fire
|
108
|
+
def wait_interval(offset = current_offset)
|
109
|
+
handle = @events.first
|
110
|
+
handle.time - Float(offset) if handle
|
111
|
+
end
|
112
|
+
|
113
|
+
# Fire all timers that are ready.
|
114
|
+
def fire(offset = current_offset)
|
115
|
+
@events.fire(offset)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Pause all timers.
|
119
|
+
def pause
|
120
|
+
@timers.dup.each(&:pause)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Resume all timers.
|
124
|
+
def resume
|
125
|
+
@paused_timers.dup.each(&:resume)
|
126
|
+
end
|
127
|
+
|
128
|
+
alias continue resume
|
129
|
+
|
130
|
+
# Delay all timers.
|
131
|
+
def delay(seconds)
|
132
|
+
@timers.each do |timer|
|
133
|
+
timer.delay(seconds)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Cancel all timers.
|
138
|
+
def cancel
|
139
|
+
@timers.dup.each(&:cancel)
|
140
|
+
end
|
141
|
+
|
142
|
+
# The group's current time.
|
143
|
+
def current_offset
|
144
|
+
@interval.to_f
|
145
|
+
end
|
146
|
+
end
|
127
147
|
end
|