timers 4.1.1 → 4.2.0
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/.rspec +1 -3
- data/.travis.yml +15 -14
- data/Gemfile +12 -3
- data/README.md +67 -21
- data/Rakefile +3 -4
- data/lib/timers/events.rb +109 -112
- data/lib/timers/group.rb +127 -127
- data/lib/timers/interval.rb +43 -0
- data/lib/timers/timer.rb +131 -125
- data/lib/timers/version.rb +8 -1
- data/lib/timers/wait.rb +48 -44
- data/lib/timers.rb +9 -3
- data/spec/spec_helper.rb +32 -13
- data/spec/timers/cancel_spec.rb +50 -0
- data/spec/timers/events_spec.rb +61 -0
- data/spec/timers/every_spec.rb +38 -0
- data/spec/timers/group_spec.rb +260 -0
- data/spec/timers/performance_spec.rb +125 -0
- data/spec/{strict_spec.rb → timers/strict_spec.rb} +12 -8
- data/spec/timers/wait_spec.rb +35 -0
- data/timers.gemspec +28 -18
- metadata +48 -49
- data/.coveralls.yml +0 -1
- data/AUTHORS.md +0 -15
- data/CHANGES.md +0 -54
- data/LICENSE +0 -23
- 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/timeout_spec.rb +0 -29
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e1665f074c230a801e0eb6648214a6d0248d4baada8da1f4e60abd0983bba517
|
|
4
|
+
data.tar.gz: de4eac12ecba1826d6ae5ef1d6958a451595b1a9e8e54dba7f1718d7f15182dd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c0e005f02975d324b7fbe7d8e284a2d5318be78352e129ce00abf20594d3e6a18b30103e1137b2d9fe91bd5a2251e7b22fb5cdc255f5153f219bf90bd3b6160d
|
|
7
|
+
data.tar.gz: 64210a85d0a8efe2540d9ee6da050c5d8298a5332bd2ba14758bd784a7f991ad40b1f828b47320525ab025249859ca6419f172421492fe6ced56a76e34820541
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
- ruby-head
|
|
6
|
-
- jruby
|
|
7
|
-
- jruby-head
|
|
8
|
-
- rbx-2
|
|
1
|
+
language: ruby
|
|
2
|
+
sudo: false
|
|
3
|
+
dist: xenial
|
|
4
|
+
cache: bundler
|
|
9
5
|
|
|
10
6
|
matrix:
|
|
11
|
-
|
|
7
|
+
include:
|
|
8
|
+
- rvm: 2.3
|
|
9
|
+
- rvm: 2.4
|
|
10
|
+
- rvm: 2.5
|
|
11
|
+
- rvm: 2.6
|
|
12
|
+
- rvm: jruby-head
|
|
13
|
+
- rvm: truffleruby
|
|
14
|
+
- rvm: ruby-head
|
|
15
|
+
- rvm: rbx-3
|
|
12
16
|
allow_failures:
|
|
13
17
|
- rvm: ruby-head
|
|
14
|
-
- rvm:
|
|
15
|
-
- rvm:
|
|
16
|
-
|
|
17
|
-
notifications:
|
|
18
|
-
irc: "irc.freenode.org#celluloid"
|
|
18
|
+
- rvm: rbx-3
|
|
19
|
+
- rvm: truffleruby
|
data/Gemfile
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
source 'https://rubygems.org'
|
|
2
2
|
|
|
3
|
-
gem 'coveralls', :require => false
|
|
4
|
-
|
|
5
|
-
# Specify your gem's dependencies in timers.gemspec
|
|
6
3
|
gemspec
|
|
4
|
+
|
|
5
|
+
group :development do
|
|
6
|
+
gem 'pry'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
group :test do
|
|
10
|
+
gem 'benchmark-ips'
|
|
11
|
+
gem "ruby-prof", platform: :mri
|
|
12
|
+
|
|
13
|
+
gem 'simplecov'
|
|
14
|
+
gem 'coveralls'
|
|
15
|
+
end
|
data/README.md
CHANGED
|
@@ -1,20 +1,30 @@
|
|
|
1
|
-
Timers
|
|
2
|
-
======
|
|
3
|
-
[](http://rubygems.org/gems/timers)
|
|
4
|
-
[](http://travis-ci.org/celluloid/timers)
|
|
5
|
-
[](https://codeclimate.com/github/celluloid/timers)
|
|
6
|
-
[](https://coveralls.io/r/celluloid/timers)
|
|
1
|
+
# Timers
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
or at periodic intervals.
|
|
3
|
+
Collections of one-shot and periodic timers, intended for use with event loops such as [async].
|
|
10
4
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Ruby library for using system calls like epoll and kqueue.
|
|
5
|
+
[](https://travis-ci.org/socketry/timers)
|
|
6
|
+
[](https://codeclimate.com/github/socketry/timers)
|
|
7
|
+
[](https://coveralls.io/r/socketry/timers)
|
|
15
8
|
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
[async]: https://github.com/socketry/async
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem 'timers'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ gem install timers
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
18
28
|
|
|
19
29
|
Create a new timer group with `Timers::Group.new`:
|
|
20
30
|
|
|
@@ -94,10 +104,46 @@ timers.resume
|
|
|
94
104
|
10.times { timers.wait } # will fire all timers
|
|
95
105
|
```
|
|
96
106
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
1. Fork it
|
|
110
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
111
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
112
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
113
|
+
5. Create new Pull Request
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
Released under the MIT license.
|
|
118
|
+
|
|
119
|
+
Copyright, 2018, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
|
120
|
+
Copyright, 2016, by [Tony Arcieri](bascule@gmail.com).
|
|
121
|
+
Copyright, 2016, by Jeremy Hinegardner.
|
|
122
|
+
Copyright, 2016, by Sean Gregory.
|
|
123
|
+
Copyright, 2016, by Chuck Remes.
|
|
124
|
+
Copyright, 2016, by Utenmiki.
|
|
125
|
+
Copyright, 2016, by Ron Evans.
|
|
126
|
+
Copyright, 2016, by Larry Lv.
|
|
127
|
+
Copyright, 2016, by Bruno Enten.
|
|
128
|
+
Copyright, 2016, by Jesse Cooke.
|
|
129
|
+
Copyright, 2016, by Nicholas Evans.
|
|
130
|
+
Copyright, 2016, by Dimitrij Denissenko.
|
|
131
|
+
Copyright, 2016, by Ryan LeCompte.
|
|
132
|
+
|
|
133
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
134
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
135
|
+
in the Software without restriction, including without limitation the rights
|
|
136
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
137
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
138
|
+
furnished to do so, subject to the following conditions:
|
|
139
|
+
|
|
140
|
+
The above copyright notice and this permission notice shall be included in
|
|
141
|
+
all copies or substantial portions of the Software.
|
|
142
|
+
|
|
143
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
144
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
145
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
146
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
147
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
148
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
149
|
+
THE SOFTWARE.
|
data/Rakefile
CHANGED
data/lib/timers/events.rb
CHANGED
|
@@ -1,116 +1,113 @@
|
|
|
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
|
+
#
|
|
1
7
|
|
|
2
|
-
|
|
3
|
-
require 'hitimes'
|
|
4
|
-
|
|
5
|
-
require 'timers/timer'
|
|
8
|
+
require_relative "timer"
|
|
6
9
|
|
|
7
10
|
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
|
-
end
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
return l
|
|
114
|
-
end
|
|
115
|
-
end
|
|
11
|
+
# Maintains an ordered list of events, which can be cancelled.
|
|
12
|
+
class Events
|
|
13
|
+
# Represents a cancellable handle for a specific timer event.
|
|
14
|
+
class Handle
|
|
15
|
+
def initialize(time, callback)
|
|
16
|
+
@time = time
|
|
17
|
+
@callback = callback
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The absolute time that the handle should be fired at.
|
|
21
|
+
attr_reader :time
|
|
22
|
+
|
|
23
|
+
# Cancel this timer, O(1).
|
|
24
|
+
def cancel!
|
|
25
|
+
# The simplest way to keep track of cancelled status is to nullify the
|
|
26
|
+
# callback. This should also be optimal for garbage collection.
|
|
27
|
+
@callback = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Has this timer been cancelled? Cancelled timer's don't fire.
|
|
31
|
+
def cancelled?
|
|
32
|
+
@callback.nil?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def >(other)
|
|
36
|
+
@time > other.to_f
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_f
|
|
40
|
+
@time
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Fire the callback if not cancelled with the given time parameter.
|
|
44
|
+
def fire(time)
|
|
45
|
+
@callback.call(time) if @callback
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def initialize
|
|
50
|
+
# A sequence of handles, maintained in sorted order, future to present.
|
|
51
|
+
# @sequence.last is the next event to be fired.
|
|
52
|
+
@sequence = []
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Add an event at the given time.
|
|
56
|
+
def schedule(time, callback)
|
|
57
|
+
handle = Handle.new(time.to_f, callback)
|
|
58
|
+
|
|
59
|
+
index = bisect_left(@sequence, handle)
|
|
60
|
+
|
|
61
|
+
# Maintain sorted order, O(logN) insertion time.
|
|
62
|
+
@sequence.insert(index, handle)
|
|
63
|
+
|
|
64
|
+
handle
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Returns the first non-cancelled handle.
|
|
68
|
+
def first
|
|
69
|
+
while (handle = @sequence.last)
|
|
70
|
+
return handle unless handle.cancelled?
|
|
71
|
+
@sequence.pop
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Returns the number of pending (possibly cancelled) events.
|
|
76
|
+
def size
|
|
77
|
+
@sequence.size
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Fire all handles for which Handle#time is less than the given time.
|
|
81
|
+
def fire(time)
|
|
82
|
+
pop(time).reverse_each do |handle|
|
|
83
|
+
handle.fire(time)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
# Efficiently take k handles for which Handle#time is less than the given
|
|
90
|
+
# time.
|
|
91
|
+
def pop(time)
|
|
92
|
+
index = bisect_left(@sequence, time)
|
|
93
|
+
|
|
94
|
+
@sequence.pop(@sequence.size - index)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Return the left-most index where to insert item e, in a list a, assuming
|
|
98
|
+
# a is sorted in descending order.
|
|
99
|
+
def bisect_left(a, e, l = 0, u = a.length)
|
|
100
|
+
while l < u
|
|
101
|
+
m = l + (u - l).div(2)
|
|
102
|
+
|
|
103
|
+
if a[m] > e
|
|
104
|
+
l = m + 1
|
|
105
|
+
else
|
|
106
|
+
u = m
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
l
|
|
111
|
+
end
|
|
112
|
+
end
|
|
116
113
|
end
|