tmtms_timer 0.2.0 → 0.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/.rubocop.yml +15 -1
- data/lib/tmtms/timer.rb +30 -8
- data/lib/tmtms_timer/version.rb +1 -1
- 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: e9f3d60079ed5978997a58b2aa0cc2fae5dfeabf8e76cba7536eec4f67397cdf
|
4
|
+
data.tar.gz: ff386b2fea04c7da9a3e9e17ef781adaf90dd863089635b9b0ee4e55c585f421
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f03ffd1613106842d7c6c2b6d82fb0c9045a1e36e93670ce79816cc084656c15b33389c263cbb4ccacab1cb940c90c8c15c43f6b850c8ba4e1735fc28d8e108c
|
7
|
+
data.tar.gz: b7793430bc906cc0f0fa5d4c0c291fe1553a49ca689226c2afee1d2780b1b93b3903258e7eb6fb021950570615bb0442732dffac50c8ca873b173e023013ce07
|
data/.rubocop.yml
CHANGED
@@ -18,10 +18,15 @@ Layout/SpaceInsideBlockBraces:
|
|
18
18
|
Enabled: false
|
19
19
|
|
20
20
|
Metrics/AbcSize:
|
21
|
+
Max: 40
|
22
|
+
|
23
|
+
Metrics/BlockLength:
|
24
|
+
Exclude:
|
25
|
+
- 'spec/**/*'
|
21
26
|
Max: 30
|
22
27
|
|
23
28
|
Metrics/CyclomaticComplexity:
|
24
|
-
Max:
|
29
|
+
Max: 30
|
25
30
|
|
26
31
|
Metrics/MethodLength:
|
27
32
|
Max: 40
|
@@ -35,6 +40,15 @@ Style/FrozenStringLiteralComment:
|
|
35
40
|
Style/IfUnlessModifier:
|
36
41
|
Enabled: false
|
37
42
|
|
43
|
+
Style/KeywordParametersOrder:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/Next:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/ParallelAssignment:
|
50
|
+
Enabled: false
|
51
|
+
|
38
52
|
Style/StringLiterals:
|
39
53
|
Enabled: false
|
40
54
|
|
data/lib/tmtms/timer.rb
CHANGED
@@ -7,14 +7,16 @@ module Tmtms
|
|
7
7
|
# t.set(1){puts "hoge"}
|
8
8
|
# sleep 5 # puts "hoge" after 1 second.
|
9
9
|
class Timer
|
10
|
+
# Timer job
|
10
11
|
class Job
|
11
|
-
|
12
|
+
attr_accessor :time
|
13
|
+
attr_reader :repeat, :block
|
12
14
|
|
13
15
|
# @param timer [Tmtms::Timer]
|
14
16
|
# @param time [Time]
|
15
17
|
# @param block [Proc]
|
16
|
-
def initialize(timer:, time:, block:)
|
17
|
-
@timer, @time, @block = timer, time, block
|
18
|
+
def initialize(timer:, time:, repeat: nil, block:)
|
19
|
+
@timer, @time, @repeat, @block = timer, time, repeat, block
|
18
20
|
end
|
19
21
|
|
20
22
|
# cancel this job.
|
@@ -33,17 +35,19 @@ module Tmtms
|
|
33
35
|
|
34
36
|
# execute block after sec seconds.
|
35
37
|
# @param sec [Numeric]
|
38
|
+
# @param repeat [Numeric] interval seconds
|
36
39
|
# @return [Tmtms::Timer::Job] timer job
|
37
|
-
def set(sec, &block)
|
38
|
-
at(Time.now + sec, &block)
|
40
|
+
def set(sec, repeat: nil, &block)
|
41
|
+
at(Time.now + sec, repeat: repeat, &block)
|
39
42
|
end
|
40
43
|
|
41
44
|
# execute block at the specified time.
|
42
45
|
# @param time [Time]
|
46
|
+
# @param repeat [Numeric] interval seconds
|
43
47
|
# @return [Tmtms::Timer::Job] timer job
|
44
|
-
def at(time, &block)
|
48
|
+
def at(time, repeat: nil, &block)
|
45
49
|
@mutex.synchronize do
|
46
|
-
job = Job.new(timer: self, time: time, block: block)
|
50
|
+
job = Job.new(timer: self, time: time, repeat: repeat, block: block)
|
47
51
|
@jobs.push job
|
48
52
|
@jobs.sort_by!(&:time)
|
49
53
|
@wfd.syswrite('.')
|
@@ -51,6 +55,13 @@ module Tmtms
|
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
58
|
+
# repeat block every sec seconds.
|
59
|
+
# @param sec [Numeric] interval seconds
|
60
|
+
# @return [Tmtms::Timer::Job] timer job
|
61
|
+
def repeat(sec, &block)
|
62
|
+
at(Time.now, repeat: sec, &block)
|
63
|
+
end
|
64
|
+
|
54
65
|
# cancle the timer.
|
55
66
|
# @param job [Tmtms::Timer::Job] timer job
|
56
67
|
# @return [Tmtms::Timer::Job] if the job was available.
|
@@ -74,11 +85,22 @@ module Tmtms
|
|
74
85
|
@rfd.sysread(1)
|
75
86
|
end
|
76
87
|
loop do
|
88
|
+
job = nil
|
77
89
|
block = @mutex.synchronize do
|
78
|
-
|
90
|
+
if @jobs[0] && @jobs[0].time <= Time.now
|
91
|
+
job = @jobs.shift
|
92
|
+
job.block
|
93
|
+
end
|
79
94
|
end
|
80
95
|
break unless block
|
81
96
|
block.call
|
97
|
+
if job.repeat
|
98
|
+
@mutex.synchronize do
|
99
|
+
job.time = Time.now + job.repeat
|
100
|
+
@jobs.push job
|
101
|
+
@jobs.sort_by!(&:time)
|
102
|
+
end
|
103
|
+
end
|
82
104
|
end
|
83
105
|
end
|
84
106
|
end
|
data/lib/tmtms_timer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tmtms_timer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TOMITA Masahiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Timer
|
14
14
|
email:
|