timers 4.2.0 → 4.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/timers/timer.rb CHANGED
@@ -1,9 +1,24 @@
1
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
- #
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
7
22
 
8
23
  module Timers
9
24
  # An individual timer set to fire a given proc at a given time. A timer is
@@ -14,67 +29,68 @@ module Timers
14
29
  class Timer
15
30
  include Comparable
16
31
  attr_reader :interval, :offset, :recurring
17
-
32
+
18
33
  def initialize(group, interval, recurring = false, offset = nil, &block)
19
34
  @group = group
20
-
35
+
21
36
  @interval = interval
22
37
  @recurring = recurring
23
38
  @block = block
24
39
  @offset = offset
25
-
40
+
26
41
  @handle = nil
27
-
42
+
28
43
  # If a start offset was supplied, use that, otherwise use the current timers offset.
29
44
  reset(@offset || @group.current_offset)
30
45
  end
31
-
46
+
32
47
  def paused?
33
48
  @group.paused_timers.include? self
34
49
  end
35
-
50
+
36
51
  def pause
37
52
  return if paused?
38
-
53
+
39
54
  @group.timers.delete self
40
55
  @group.paused_timers.add self
41
-
56
+
42
57
  @handle.cancel! if @handle
43
58
  @handle = nil
44
59
  end
45
-
60
+
46
61
  def resume
47
62
  return unless paused?
48
-
63
+
49
64
  @group.paused_timers.delete self
50
-
65
+
51
66
  # This will add us back to the group:
52
67
  reset
53
68
  end
54
-
69
+
55
70
  alias continue resume
56
-
71
+
57
72
  # Extend this timer
58
73
  def delay(seconds)
59
74
  @handle.cancel! if @handle
60
-
75
+
61
76
  @offset += seconds
62
-
77
+
63
78
  @handle = @group.events.schedule(@offset, self)
64
79
  end
65
-
80
+
66
81
  # Cancel this timer. Do not call while paused.
67
82
  def cancel
68
83
  return unless @handle
69
-
84
+
70
85
  @handle.cancel! if @handle
71
86
  @handle = nil
72
-
87
+
73
88
  # This timer is no longer valid:
74
89
  @group.timers.delete self if @group
75
90
  end
76
-
91
+
77
92
  # Reset this timer. Do not call while paused.
93
+ # @param offset [Numeric] the duration to add to the timer.
78
94
  def reset(offset = @group.current_offset)
79
95
  # This logic allows us to minimise the interaction with @group.timers.
80
96
  # A timer with a handle is always registered with the group.
@@ -83,12 +99,12 @@ module Timers
83
99
  else
84
100
  @group.timers << self
85
101
  end
86
-
102
+
87
103
  @offset = Float(offset) + @interval
88
-
104
+
89
105
  @handle = @group.events.schedule(@offset, self)
90
106
  end
91
-
107
+
92
108
  # Fire the block.
93
109
  def fire(offset = @group.current_offset)
94
110
  if recurring == :strict
@@ -99,36 +115,38 @@ module Timers
99
115
  else
100
116
  @offset = offset
101
117
  end
102
-
103
- @block.call(offset)
104
-
118
+
119
+ @block.call(offset, self)
120
+
105
121
  cancel unless recurring
106
122
  end
107
-
123
+
108
124
  alias call fire
109
-
125
+
110
126
  # Number of seconds until next fire / since last fire
111
127
  def fires_in
112
128
  @offset - @group.current_offset if @offset
113
129
  end
114
-
130
+
115
131
  # Inspect a timer
116
132
  def inspect
117
- str = "#{to_s[0..-2]} ".dup
118
-
133
+ buffer = "#{to_s[0..-2]} ".dup
134
+
119
135
  if @offset
120
- str << if fires_in >= 0
121
- "fires in #{fires_in} seconds"
122
- else
123
- "fired #{fires_in.abs} seconds ago"
124
- end
125
-
126
- str << ", recurs every #{interval}" if recurring
136
+ if fires_in >= 0
137
+ buffer << "fires in #{fires_in} seconds"
138
+ else
139
+ buffer << "fired #{fires_in.abs} seconds ago"
140
+ end
141
+
142
+ buffer << ", recurs every #{interval}" if recurring
127
143
  else
128
- str << "dead"
144
+ buffer << "dead"
129
145
  end
130
-
131
- str << ">"
146
+
147
+ buffer << ">"
148
+
149
+ return buffer
132
150
  end
133
151
  end
134
152
  end
@@ -1,10 +1,25 @@
1
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
- #
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
7
22
 
8
23
  module Timers
9
- VERSION = "4.2.0"
24
+ VERSION = "4.3.3"
10
25
  end
data/lib/timers/wait.rb CHANGED
@@ -1,9 +1,24 @@
1
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
- #
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
7
22
 
8
23
  require_relative "interval"
9
24
 
@@ -13,7 +28,7 @@ module Timers
13
28
  def self.for(duration, &block)
14
29
  if duration
15
30
  timeout = new(duration)
16
-
31
+
17
32
  timeout.while_time_remaining(&block)
18
33
  else
19
34
  loop do
@@ -21,31 +36,31 @@ module Timers
21
36
  end
22
37
  end
23
38
  end
24
-
39
+
25
40
  def initialize(duration)
26
41
  @duration = duration
27
42
  @remaining = true
28
43
  end
29
-
44
+
30
45
  attr_reader :duration
31
46
  attr_reader :remaining
32
-
47
+
33
48
  # Yields while time remains for work to be done:
34
49
  def while_time_remaining
35
50
  @interval = Interval.new
36
51
  @interval.start
37
-
52
+
38
53
  yield @remaining while time_remaining?
39
54
  ensure
40
55
  @interval.stop
41
56
  @interval = nil
42
57
  end
43
-
58
+
44
59
  private
45
-
60
+
46
61
  def time_remaining?
47
62
  @remaining = (@duration - @interval.to_f)
48
-
63
+
49
64
  @remaining > 0
50
65
  end
51
66
  end
metadata CHANGED
@@ -1,94 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timers
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  - Tony Arcieri
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-06 00:00:00.000000000 Z
12
+ date: 2021-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '1.3'
20
+ version: '0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '1.3'
27
+ version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: rspec
29
+ name: covered
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: '3.6'
34
+ version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '3.6'
41
+ version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: rake
43
+ name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '3.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
56
- description: Schedule procs to run after a certain time, or at periodic intervals,
57
- using any API that accepts a timeout.
55
+ version: '3.0'
56
+ description:
58
57
  email:
59
- - samuel@codeotaku.com
60
- - bascule@gmail.com
61
58
  executables: []
62
59
  extensions: []
63
60
  extra_rdoc_files: []
64
61
  files:
65
- - ".gitignore"
66
- - ".rspec"
67
- - ".travis.yml"
68
- - Gemfile
69
- - README.md
70
- - Rakefile
71
62
  - lib/timers.rb
72
63
  - lib/timers/events.rb
73
64
  - lib/timers/group.rb
74
65
  - lib/timers/interval.rb
66
+ - lib/timers/priority_heap.rb
75
67
  - lib/timers/timer.rb
76
68
  - lib/timers/version.rb
77
69
  - lib/timers/wait.rb
78
- - spec/spec_helper.rb
79
- - spec/timers/cancel_spec.rb
80
- - spec/timers/events_spec.rb
81
- - spec/timers/every_spec.rb
82
- - spec/timers/group_spec.rb
83
- - spec/timers/performance_spec.rb
84
- - spec/timers/strict_spec.rb
85
- - spec/timers/wait_spec.rb
86
- - timers.gemspec
87
70
  homepage: https://github.com/socketry/timers
88
71
  licenses:
89
72
  - MIT
90
73
  metadata: {}
91
- post_install_message:
74
+ post_install_message:
92
75
  rdoc_options: []
93
76
  require_paths:
94
77
  - lib
@@ -96,24 +79,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
79
  requirements:
97
80
  - - ">="
98
81
  - !ruby/object:Gem::Version
99
- version: 2.2.1
82
+ version: '0'
100
83
  required_rubygems_version: !ruby/object:Gem::Requirement
101
84
  requirements:
102
85
  - - ">="
103
86
  - !ruby/object:Gem::Version
104
87
  version: '0'
105
88
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.7.7
108
- signing_key:
89
+ rubygems_version: 3.1.2
90
+ signing_key:
109
91
  specification_version: 4
110
- summary: Pure Ruby one-shot and periodic timers
111
- test_files:
112
- - spec/spec_helper.rb
113
- - spec/timers/cancel_spec.rb
114
- - spec/timers/events_spec.rb
115
- - spec/timers/every_spec.rb
116
- - spec/timers/group_spec.rb
117
- - spec/timers/performance_spec.rb
118
- - spec/timers/strict_spec.rb
119
- - spec/timers/wait_spec.rb
92
+ summary: Pure Ruby one-shot and periodic timers.
93
+ test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp