timers 4.0.0 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/lib/timers/events.rb +1 -1
- data/lib/timers/timer.rb +22 -11
- data/lib/timers/version.rb +1 -1
- data/spec/cancel_spec.rb +46 -0
- data/spec/events_spec.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff04e8dedf4d39bd0ff36650200e6c3537669cd8
|
4
|
+
data.tar.gz: fd2f6c05f04a626f73401ea9c06e8be89956024e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b829c8ffc7da2676e9b32e0f16fc54135dce451825db5c669f40566d6bd8a4334dd8d6b28d06dcd191cc92cd55a27dcd3fe9476b16fcb56a80c23b335fcb2f4
|
7
|
+
data.tar.gz: 08314ad60709460ff1d4f55cf4c61ccb0333bf7d67186702d5e3268cfe9ac2ba9e511c936b63be0ed4f9d6532a5f3ca0235bd65ceba319b812ffa4564115f014
|
data/CHANGES.md
CHANGED
data/lib/timers/events.rb
CHANGED
data/lib/timers/timer.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
|
2
2
|
module Timers
|
3
|
-
# An individual timer set to fire a given proc at a given time
|
3
|
+
# An individual timer set to fire a given proc at a given time. A timer is
|
4
|
+
# always connected to a Timer::Group but it would ONLY be in @group.timers
|
5
|
+
# if it also has a @handle specified. Otherwise it is either PAUSED or has
|
6
|
+
# been FIRED and is not recurring. You can manually enter this state by
|
7
|
+
# calling #cancel and resume normal operation by calling #reset.
|
4
8
|
class Timer
|
5
9
|
include Comparable
|
6
10
|
attr_reader :interval, :offset, :recurring
|
@@ -8,8 +12,6 @@ module Timers
|
|
8
12
|
def initialize(group, interval, recurring = false, offset = nil, &block)
|
9
13
|
@group = group
|
10
14
|
|
11
|
-
@group.timers << self
|
12
|
-
|
13
15
|
@interval = interval
|
14
16
|
@recurring = recurring
|
15
17
|
@block = block
|
@@ -38,9 +40,9 @@ module Timers
|
|
38
40
|
def resume
|
39
41
|
return unless paused?
|
40
42
|
|
41
|
-
@group.timers.add self
|
42
43
|
@group.paused_timers.delete self
|
43
44
|
|
45
|
+
# This will add us back to the group:
|
44
46
|
reset
|
45
47
|
end
|
46
48
|
|
@@ -54,27 +56,34 @@ module Timers
|
|
54
56
|
|
55
57
|
@handle = @group.events.schedule(@offset, self)
|
56
58
|
end
|
57
|
-
|
58
|
-
# Cancel this timer
|
59
|
+
|
60
|
+
# Cancel this timer. Do not call while paused.
|
59
61
|
def cancel
|
62
|
+
return unless @handle
|
63
|
+
|
60
64
|
@handle.cancel! if @handle
|
61
65
|
@handle = nil
|
62
66
|
|
63
67
|
# This timer is no longer valid:
|
64
|
-
@group.timers.delete self
|
65
|
-
@group = nil
|
68
|
+
@group.timers.delete self if @group
|
66
69
|
end
|
67
70
|
|
68
|
-
# Reset this timer
|
71
|
+
# Reset this timer. Do not call while paused.
|
69
72
|
def reset(offset = @group.current_offset)
|
70
|
-
|
73
|
+
# This logic allows us to minimise the interaction with @group.timers.
|
74
|
+
# A timer with a handle is always registered with the group.
|
75
|
+
if @handle
|
76
|
+
@handle.cancel!
|
77
|
+
else
|
78
|
+
@group.timers << self
|
79
|
+
end
|
71
80
|
|
72
81
|
@offset = Float(offset) + @interval
|
73
82
|
|
74
83
|
@handle = @group.events.schedule(@offset, self)
|
75
84
|
end
|
76
85
|
|
77
|
-
# Fire the block
|
86
|
+
# Fire the block.
|
78
87
|
def fire(offset = @group.current_offset)
|
79
88
|
if recurring == :strict
|
80
89
|
# ... make the next interval strictly the last offset + the interval:
|
@@ -86,6 +95,8 @@ module Timers
|
|
86
95
|
end
|
87
96
|
|
88
97
|
@block.call(offset)
|
98
|
+
|
99
|
+
cancel unless recurring
|
89
100
|
end
|
90
101
|
|
91
102
|
alias_method :call, :fire
|
data/lib/timers/version.rb
CHANGED
data/spec/cancel_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
RSpec.describe Timers::Group do
|
5
|
+
it "should be able to cancel twice" do
|
6
|
+
fired = false
|
7
|
+
|
8
|
+
timer = subject.after(0.1) { fired = true }
|
9
|
+
|
10
|
+
2.times do
|
11
|
+
timer.cancel
|
12
|
+
subject.wait
|
13
|
+
end
|
14
|
+
|
15
|
+
expect(fired).to be false
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be possble to reset after cancel" do
|
19
|
+
fired = false
|
20
|
+
|
21
|
+
timer = subject.after(0.1) { fired = true }
|
22
|
+
timer.cancel
|
23
|
+
|
24
|
+
subject.wait
|
25
|
+
|
26
|
+
timer.reset
|
27
|
+
|
28
|
+
subject.wait
|
29
|
+
|
30
|
+
expect(fired).to be true
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should cancel and remove one shot timers after they fire" do
|
34
|
+
x = 0
|
35
|
+
|
36
|
+
Timers::Wait.for(2) do |remaining|
|
37
|
+
timer = subject.every(0.2) { x += 1 }
|
38
|
+
subject.after(0.1) { timer.cancel }
|
39
|
+
|
40
|
+
subject.wait
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(subject.timers).to be_empty
|
44
|
+
expect(x).to be == 0
|
45
|
+
end
|
46
|
+
end
|
data/spec/events_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0.
|
4
|
+
version: 4.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Arcieri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hitimes
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/timers/timer.rb
|
76
76
|
- lib/timers/version.rb
|
77
77
|
- lib/timers/wait.rb
|
78
|
+
- spec/cancel_spec.rb
|
78
79
|
- spec/events_spec.rb
|
79
80
|
- spec/every_spec.rb
|
80
81
|
- spec/group_spec.rb
|
@@ -109,6 +110,7 @@ specification_version: 4
|
|
109
110
|
summary: Schedule procs to run after a certain time, or at periodic intervals, using
|
110
111
|
any API that accepts a timeout
|
111
112
|
test_files:
|
113
|
+
- spec/cancel_spec.rb
|
112
114
|
- spec/events_spec.rb
|
113
115
|
- spec/every_spec.rb
|
114
116
|
- spec/group_spec.rb
|