timers 4.2.1 → 4.3.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 +4 -4
 - data/lib/timers/events.rb +34 -24
 - data/lib/timers/group.rb +1 -1
 - data/lib/timers/timer.rb +1 -1
 - data/lib/timers/version.rb +1 -1
 - data/spec/spec_helper.rb +4 -2
 - data/spec/timers/performance_spec.rb +0 -1
 - data/spec/timers/strict_spec.rb +5 -7
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: ccf7aa14dc18086e3a47fb44eb3a12569ba6752e6f7d22aecf7486684a594331
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 0c37cdab96fc3ae8a1fbd0deb07b69800260b32118c7d1b37d2a4114258c32e8
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: dcb1826464d594d78d0a67bf18f89bb3137ffdbdcb36306176bbaa961aab387b93442c4827f5a8e331b90de056dbc9f9f63dbdeb22671dcdd35203741f415967
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 43dcaccab4fab882ca498ec196f08d47beb2636044b62103640406e5a0411d1eb6d837363eae50266c11642a59880179eca6a9f4053aefab1b4daf11a28319bd
         
     | 
    
        data/lib/timers/events.rb
    CHANGED
    
    | 
         @@ -32,10 +32,14 @@ module Timers 
     | 
|
| 
       32 
32 
     | 
    
         
             
            				@callback.nil?
         
     | 
| 
       33 
33 
     | 
    
         
             
            			end
         
     | 
| 
       34 
34 
     | 
    
         | 
| 
       35 
     | 
    
         
            -
            			def > 
     | 
| 
      
 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 
     | 
    
         
            -
            			 
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
            			 
     | 
| 
       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 
     | 
    
         
            -
            			 
     | 
| 
      
 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 
     | 
    
         
            -
            		 
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
       96 
     | 
    
         
            -
             
     | 
| 
       97 
     | 
    
         
            -
             
     | 
| 
       98 
     | 
    
         
            -
             
     | 
| 
       99 
     | 
    
         
            -
             
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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]  
     | 
| 
      
 118 
     | 
    
         
            +
            				if a[m] >= e
         
     | 
| 
       109 
119 
     | 
    
         
             
            					l = m + 1
         
     | 
| 
       110 
120 
     | 
    
         
             
            				else
         
     | 
| 
       111 
121 
     | 
    
         
             
            					u = m
         
     | 
    
        data/lib/timers/group.rb
    CHANGED
    
    
    
        data/lib/timers/timer.rb
    CHANGED
    
    
    
        data/lib/timers/version.rb
    CHANGED
    
    
    
        data/spec/spec_helper.rb
    CHANGED
    
    | 
         @@ -25,8 +25,10 @@ if ENV['COVERAGE'] || ENV['TRAVIS'] 
     | 
|
| 
       25 
25 
     | 
    
         
             
            	end
         
     | 
| 
       26 
26 
     | 
    
         
             
            end
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
     | 
    
         
            -
            require  
     | 
| 
       29 
     | 
    
         
            -
            require 
     | 
| 
      
 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
         
     | 
    
        data/spec/timers/strict_spec.rb
    CHANGED
    
    | 
         @@ -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. 
     | 
| 
      
 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- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2019-01-21 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: bundler
         
     |