timers 4.2.1 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 327fbcdf5ce2ea7d16dc2d5605046c64bdaccc917d4cab8bd5765d0817883f7d
4
- data.tar.gz: dcf2c56295c5ee1d2c5160c9a45189ae98efa54bcdced8dc69c86b211769db89
3
+ metadata.gz: ccf7aa14dc18086e3a47fb44eb3a12569ba6752e6f7d22aecf7486684a594331
4
+ data.tar.gz: 0c37cdab96fc3ae8a1fbd0deb07b69800260b32118c7d1b37d2a4114258c32e8
5
5
  SHA512:
6
- metadata.gz: b067c4a96490380d699e47d3712e74a6c723921c0931f9a877a9c4ab924e50187e61427bf39e88f887d487efeac6fa5fe493712852e0306531241a9130020840
7
- data.tar.gz: efdcc4c8b6e6079327977b7bfdca001e30835e87bac0fb2597a5612a62e4c493cc09574c9be9eaf5220275f7569235e177ede23e2d3e40da4dddc481dc456cf8
6
+ metadata.gz: dcb1826464d594d78d0a67bf18f89bb3137ffdbdcb36306176bbaa961aab387b93442c4827f5a8e331b90de056dbc9f9f63dbdeb22671dcdd35203741f415967
7
+ data.tar.gz: 43dcaccab4fab882ca498ec196f08d47beb2636044b62103640406e5a0411d1eb6d837363eae50266c11642a59880179eca6a9f4053aefab1b4daf11a28319bd
@@ -32,10 +32,14 @@ module Timers
32
32
  @callback.nil?
33
33
  end
34
34
 
35
- def >(other)
35
+ def > other
36
36
  @time > other.to_f
37
37
  end
38
38
 
39
+ def >= other
40
+ @time >= other.to_f
41
+ end
42
+
39
43
  def to_f
40
44
  @time
41
45
  end
@@ -50,27 +54,22 @@ module Timers
50
54
  # A sequence of handles, maintained in sorted order, future to present.
51
55
  # @sequence.last is the next event to be fired.
52
56
  @sequence = []
57
+ @queue = []
53
58
  end
54
59
 
55
60
  # Add an event at the given time.
56
61
  def schedule(time, callback)
57
62
  handle = Handle.new(time.to_f, callback)
58
-
59
- index = bisect_left(@sequence, handle)
60
-
61
- if current_handle = @sequence[index] and current_handle.cancelled?
62
- # puts "Replacing handle at index: #{index} due to cancellation in array containing #{@sequence.size} item(s)."
63
- @sequence[index] = handle
64
- else
65
- # puts "Inserting handle at index: #{index} in array containing #{@sequence.size} item(s)."
66
- @sequence.insert(index, handle)
67
- end
68
-
69
- handle
63
+
64
+ @queue << handle
65
+
66
+ return handle
70
67
  end
71
68
 
72
69
  # Returns the first non-cancelled handle.
73
70
  def first
71
+ merge!
72
+
74
73
  while (handle = @sequence.last)
75
74
  return handle unless handle.cancelled?
76
75
  @sequence.pop
@@ -79,33 +78,44 @@ module Timers
79
78
 
80
79
  # Returns the number of pending (possibly cancelled) events.
81
80
  def size
82
- @sequence.size
81
+ @sequence.size + @queue.size
83
82
  end
84
83
 
85
84
  # Fire all handles for which Handle#time is less than the given time.
86
85
  def fire(time)
87
- pop(time).reverse_each do |handle|
86
+ merge!
87
+
88
+ while handle = @sequence.last and handle.time <= time
89
+ @sequence.pop
88
90
  handle.fire(time)
89
91
  end
90
92
  end
91
93
 
92
94
  private
93
95
 
94
- # Efficiently take k handles for which Handle#time is less than the given
95
- # time.
96
- def pop(time)
97
- index = bisect_left(@sequence, time)
98
-
99
- @sequence.pop(@sequence.size - index)
96
+ def merge!
97
+ while handle = @queue.pop
98
+ next if handle.cancelled?
99
+
100
+ index = bisect_right(@sequence, handle)
101
+
102
+ if current_handle = @sequence[index] and current_handle.cancelled?
103
+ # puts "Replacing handle at index: #{index} due to cancellation in array containing #{@sequence.size} item(s)."
104
+ @sequence[index] = handle
105
+ else
106
+ # puts "Inserting handle at index: #{index} in array containing #{@sequence.size} item(s)."
107
+ @sequence.insert(index, handle)
108
+ end
109
+ end
100
110
  end
101
111
 
102
- # Return the left-most index where to insert item e, in a list a, assuming
112
+ # Return the right-most index where to insert item e, in a list a, assuming
103
113
  # a is sorted in descending order.
104
- def bisect_left(a, e, l = 0, u = a.length)
114
+ def bisect_right(a, e, l = 0, u = a.length)
105
115
  while l < u
106
116
  m = l + (u - l).div(2)
107
117
 
108
- if a[m] > e
118
+ if a[m] >= e
109
119
  l = m + 1
110
120
  else
111
121
  u = m
@@ -81,7 +81,7 @@ module Timers
81
81
  sleep interval
82
82
  end
83
83
  end
84
-
84
+
85
85
  fire
86
86
  end
87
87
 
@@ -101,7 +101,7 @@ module Timers
101
101
  @offset = offset
102
102
  end
103
103
 
104
- @block.call(offset)
104
+ @block.call(offset, self)
105
105
 
106
106
  cancel unless recurring
107
107
  end
@@ -6,5 +6,5 @@
6
6
  #
7
7
 
8
8
  module Timers
9
- VERSION = "4.2.1"
9
+ VERSION = "4.3.0"
10
10
  end
@@ -25,8 +25,10 @@ if ENV['COVERAGE'] || ENV['TRAVIS']
25
25
  end
26
26
  end
27
27
 
28
- require "bundler/setup"
29
- require "timers"
28
+ require 'bundler/setup'
29
+ Bundler.require(:test)
30
+
31
+ require 'timers'
30
32
 
31
33
  RSpec.configure do |config|
32
34
  # Enable flags like --only-failures and --next-failure
@@ -6,7 +6,6 @@
6
6
  #
7
7
 
8
8
  require "spec_helper"
9
- require "ruby-prof" unless RUBY_PLATFORM =~ /java|rbx/
10
9
 
11
10
  # Event based timers:
12
11
 
@@ -24,18 +24,16 @@ RSpec.describe Timers::Group do
24
24
  expect(fired - start_offset).to be_within(quantum).of(iterations * quantum)
25
25
  end
26
26
 
27
- it "should only fire once" do
28
- fired = :not_fired_yet
27
+ it "should only fire 0-interval timer once per iteration" do
29
28
  count = 0
30
-
29
+
31
30
  start_offset = subject.current_offset
32
- Timers::Timer.new(subject, 0, :strict, start_offset) do |offset|
33
- fired = offset
31
+ Timers::Timer.new(subject, 0, :strict, start_offset) do |offset, timer|
34
32
  count += 1
35
33
  end
36
-
34
+
37
35
  subject.wait
38
-
36
+
39
37
  expect(count).to be == 1
40
38
  end
41
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timers
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-20 00:00:00.000000000 Z
12
+ date: 2019-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler