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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 107c4a8a97789c9580117a8eca6cb72af1390d54
4
- data.tar.gz: 6687f8c8b655ce609a3ecf0a4519dd001baf4e2b
2
+ SHA256:
3
+ metadata.gz: e1665f074c230a801e0eb6648214a6d0248d4baada8da1f4e60abd0983bba517
4
+ data.tar.gz: de4eac12ecba1826d6ae5ef1d6958a451595b1a9e8e54dba7f1718d7f15182dd
5
5
  SHA512:
6
- metadata.gz: 5f838fdcc30e595940ffc40325aea12578b16bf91a279feeafa247eb2d349d2724ec96715ab6d392531efcf7ff9f9e9a44a19b977bfb13a32629ead0a4358bda
7
- data.tar.gz: 1fba4a0bad3f7b284ed4a45e6cc1e156cf66082041ec7a767cad14471e3b82a0c4bf068ea874d5e9f6bb90e3a8a571c73ebb710cb50df0d809dc3d65b470fffb
6
+ metadata.gz: c0e005f02975d324b7fbe7d8e284a2d5318be78352e129ce00abf20594d3e6a18b30103e1137b2d9fe91bd5a2251e7b22fb5cdc255f5153f219bf90bd3b6160d
7
+ data.tar.gz: 64210a85d0a8efe2540d9ee6da050c5d8298a5332bd2ba14758bd784a7f991ad40b1f828b47320525ab025249859ca6419f172421492fe6ced56a76e34820541
data/.rspec CHANGED
@@ -1,5 +1,3 @@
1
- --color
2
1
  --format documentation
3
- --backtrace
4
- --order random
5
2
  --warnings
3
+ --require spec_helper
data/.travis.yml CHANGED
@@ -1,18 +1,19 @@
1
- rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.2
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
- fast_finish: true
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: jruby-head
15
- - rvm: rbx-2
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
- [![Gem Version](https://badge.fury.io/rb/timers.png)](http://rubygems.org/gems/timers)
4
- [![Build Status](https://secure.travis-ci.org/celluloid/timers.png?branch=master)](http://travis-ci.org/celluloid/timers)
5
- [![Code Climate](https://codeclimate.com/github/celluloid/timers.png)](https://codeclimate.com/github/celluloid/timers)
6
- [![Coverage Status](https://coveralls.io/repos/celluloid/timers/badge.png?branch=master)](https://coveralls.io/r/celluloid/timers)
1
+ # Timers
7
2
 
8
- Ruby timer collections. Schedule several procs to fire after configurable delays
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
- This gem is especially useful when you are faced with an API that accepts a
12
- single timeout but you want to run multiple timers on top of it. An example of
13
- such a library is [nio4r](https://github.com/celluloid/nio4r), a cross-platform
14
- Ruby library for using system calls like epoll and kqueue.
5
+ [![Build Status](https://secure.travis-ci.org/socketry/timers.svg)](https://travis-ci.org/socketry/timers)
6
+ [![Code Climate](https://codeclimate.com/github/socketry/timers.svg)](https://codeclimate.com/github/socketry/timers)
7
+ [![Coverage Status](https://coveralls.io/repos/socketry/timers/badge.svg)](https://coveralls.io/r/socketry/timers)
15
8
 
16
- Usage
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
- License
98
- -------
99
-
100
- Copyright (c) 2014 Celluloid timers project developers (given in the file
101
- AUTHORS.md).
102
-
103
- Distributed under the MIT License. See LICENSE file for further details.
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
@@ -1,7 +1,6 @@
1
- #!/usr/bin/env rake
2
1
  require "bundler/gem_tasks"
3
- require 'rspec/core/rake_task'
2
+ require "rspec/core/rake_task"
4
3
 
5
- RSpec::Core::RakeTask.new
4
+ RSpec::Core::RakeTask.new(:test)
6
5
 
7
- task :default => :spec
6
+ task :default => :test
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
- require 'forwardable'
3
- require 'hitimes'
4
-
5
- require 'timers/timer'
8
+ require_relative "timer"
6
9
 
7
10
  module Timers
8
- # Maintains an ordered list of events, which can be cancelled.
9
- class Events
10
- # Represents a cancellable handle for a specific timer event.
11
- class Handle
12
- def initialize(time, callback)
13
- @time = time
14
- @callback = callback
15
- end
16
-
17
- # The absolute time that the handle should be fired at.
18
- attr :time
19
-
20
- # Cancel this timer, O(1).
21
- def cancel!
22
- # The simplest way to keep track of cancelled status is to nullify the
23
- # callback. This should also be optimal for garbage collection.
24
- @callback = nil
25
- end
26
-
27
- # Has this timer been cancelled? Cancelled timer's don't fire.
28
- def cancelled?
29
- @callback.nil?
30
- end
31
-
32
- def > other
33
- @time > other.to_f
34
- end
35
-
36
- def to_f
37
- @time
38
- end
39
-
40
- # Fire the callback if not cancelled with the given time parameter.
41
- def fire(time)
42
- if @callback
43
- @callback.call(time)
44
- end
45
- end
46
- end
47
-
48
- def initialize
49
- # A sequence of handles, maintained in sorted order, future to present.
50
- # @sequence.last is the next event to be fired.
51
- @sequence = []
52
- end
53
-
54
- # Add an event at the given time.
55
- def schedule(time, callback)
56
- handle = Handle.new(time.to_f, callback)
57
-
58
- index = bisect_left(@sequence, handle)
59
-
60
- # Maintain sorted order, O(logN) insertion time.
61
- @sequence.insert(index, handle)
62
-
63
- return handle
64
- end
65
-
66
- # Returns the first non-cancelled handle.
67
- def first
68
- while handle = @sequence.last
69
- if handle.cancelled?
70
- @sequence.pop
71
- else
72
- return handle
73
- end
74
- end
75
- # @sequence.reverse.find { |handle| !handle.cancelled? }
76
- end
77
-
78
- # Returns the number of pending (possibly cancelled) events.
79
- def size
80
- @sequence.size
81
- end
82
-
83
- # Fire all handles for which Handle#time is less than the given time.
84
- def fire(time)
85
- pop(time).reverse_each do |handle|
86
- handle.fire(time)
87
- end
88
- end
89
-
90
- private
91
-
92
- # Efficiently take k handles for which Handle#time is less than the given
93
- # time.
94
- def pop(time)
95
- index = bisect_left(@sequence, time)
96
-
97
- @sequence.pop(@sequence.size - index)
98
- end
99
-
100
- # Return the left-most index where to insert item e, in a list a, assuming
101
- # a is sorted in descending order.
102
- def bisect_left(a, e, l = 0, u = a.length)
103
- while l < u
104
- m = l + (u-l).div(2)
105
-
106
- if a[m] > e
107
- l = m+1
108
- else
109
- u = m
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