timers 4.1.1 → 4.3.1
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 +24 -3
- data/lib/timers/events.rb +138 -111
- data/lib/timers/group.rb +144 -129
- data/lib/timers/interval.rb +58 -0
- data/lib/timers/timer.rb +147 -125
- data/lib/timers/version.rb +23 -1
- data/lib/timers/wait.rb +63 -44
- metadata +25 -53
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -17
- data/.rspec +0 -5
- data/.travis.yml +0 -18
- data/AUTHORS.md +0 -15
- data/CHANGES.md +0 -54
- data/Gemfile +0 -6
- data/LICENSE +0 -23
- data/README.md +0 -103
- data/Rakefile +0 -7
- data/spec/cancel_spec.rb +0 -46
- data/spec/events_spec.rb +0 -57
- data/spec/every_spec.rb +0 -34
- data/spec/group_spec.rb +0 -254
- data/spec/performance_spec.rb +0 -100
- data/spec/spec_helper.rb +0 -19
- data/spec/strict_spec.rb +0 -37
- data/spec/timeout_spec.rb +0 -29
- data/timers.gemspec +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5f9fdee45ad2b102af90e2d529a1384a5f6b975d6ec3112dfafebac2d8574b0f
|
4
|
+
data.tar.gz: 5bec94f4fb24de10acaf4fba0e93514aa2912444f19188e6ecd1c6b6e3d25340
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 712db02d6c449fca924459a952df2c9ae21058b7c492ce81d4b86f4989c69aa9ef90cc98ae421ab2f13934da6488d7d91a5718c2f1070714476147302c3d9b6e
|
7
|
+
data.tar.gz: c4a358d3bdc7e108afd016e42fdb478b8e717e3c400ae6cf0694e880b91112c9b118e7609d1c31143247d5d87db2577851b1e6002f2731e524a40d7e96c1cc28
|
data/lib/timers.rb
CHANGED
@@ -1,5 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
|
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.
|
3
22
|
|
4
|
-
|
5
|
-
|
23
|
+
require_relative "timers/version"
|
24
|
+
|
25
|
+
require_relative "timers/group"
|
26
|
+
require_relative "timers/wait"
|
data/lib/timers/events.rb
CHANGED
@@ -1,116 +1,143 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
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.
|
4
22
|
|
5
|
-
|
23
|
+
require_relative "timer"
|
6
24
|
|
7
25
|
module Timers
|
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
|
-
|
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
|
-
|
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
|
116
143
|
end
|
data/lib/timers/group.rb
CHANGED
@@ -1,132 +1,147 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
+
require "set"
|
24
|
+
require "forwardable"
|
25
|
+
|
26
|
+
require_relative "interval"
|
27
|
+
require_relative "timer"
|
28
|
+
require_relative "events"
|
8
29
|
|
9
30
|
module Timers
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
# The group's current time.
|
128
|
-
def current_offset
|
129
|
-
@interval.to_f
|
130
|
-
end
|
131
|
-
end
|
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
|
132
147
|
end
|