tmtms_timer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 23aec65ce6dc7f5c2b6d4159d37233880ad036b20279cf72397a0e7418e2142c
4
+ data.tar.gz: '009f5abbf0e98ad066ae26bd024ad946d2998a4d31f557859e89d5f65411bfa3'
5
+ SHA512:
6
+ metadata.gz: 10432a3fe57222646eb21f312af33fce7666b4a33e3af78f46ebd3e89e901437ce52bf3e424ca34ca199d628e76a4d90402f1944b0699a2ec5414acec8436f6d
7
+ data.tar.gz: a01bd1219031563cbab3169db528686b63f88dfea75a236ec7b08ae52034cea656b12f94a2ad6a994d3457bc6f6ce406e64dee05ee13c6fb67257e499dabf92d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Layout/EmptyLineAfterGuardClause:
6
+ Enabled: false
7
+
8
+ Layout/ExtraSpacing:
9
+ Enabled: false
10
+
11
+ Layout/LineLength:
12
+ Max: 120
13
+
14
+ Layout/SpaceBeforeBlockBraces:
15
+ Enabled: false
16
+
17
+ Layout/SpaceInsideBlockBraces:
18
+ Enabled: false
19
+
20
+ Metrics/AbcSize:
21
+ Max: 30
22
+
23
+ Metrics/CyclomaticComplexity:
24
+ Max: 20
25
+
26
+ Metrics/MethodLength:
27
+ Max: 40
28
+
29
+ Metrics/PerceivedComplexity:
30
+ Max: 10
31
+
32
+ Style/FrozenStringLiteralComment:
33
+ Enabled: false
34
+
35
+ Style/IfUnlessModifier:
36
+ Enabled: false
37
+
38
+ Style/StringLiterals:
39
+ Enabled: false
40
+
41
+ Style/StringLiteralsInInterpolation:
42
+ Enabled: true
43
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-18
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tmtms_timer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+ gem "rubocop-rake"
14
+ gem "rubocop-rspec"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TOMITA Masahiro
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Tmtms::Timer
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'tmtms_timer'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tmtms-timer
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'tmtms_timer'
23
+ timer = Tmtms::Timer.new
24
+ timer.set(10){puts "hoge"}
25
+ sleep 20
26
+ # puts "hoge" after 10 seconds
27
+ timer.at(Time.local(2022,7,18,12,34,56)){puts "fuga"}
28
+ sleep
29
+ # puts "fuga" at 2022-07-18 12:34:56
30
+ ```
31
+
32
+ To cancel timer:
33
+
34
+ ```ruby
35
+ id = timer.set(10){puts "hoge"}
36
+ timer.cancel(id)
37
+ ```
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,71 @@
1
+ require 'io/wait'
2
+
3
+ module Tmtms
4
+ # Usage
5
+ # @example
6
+ # t = Tmtms::Timer.new
7
+ # t.set(1){puts "hoge"}
8
+ # sleep 5 # puts "hoge" after 1 second.
9
+ class Timer
10
+ Job = Struct.new(:id, :time, :block, keyword_init: true)
11
+
12
+ def initialize
13
+ @rfd, @wfd = IO.pipe
14
+ @jobs = [] #=> [job, ...]
15
+ @mutex = Mutex.new
16
+ @id = 0
17
+ do_loop
18
+ end
19
+
20
+ # execute block after sec seconds.
21
+ # @param sec [Numeric]
22
+ # @return [Integer] timer ID
23
+ def set(sec, &block)
24
+ at(Time.now + sec, &block)
25
+ end
26
+
27
+ # execute block at the specified time.
28
+ # @param time [Time]
29
+ # @return [Integer] timer ID
30
+ def at(time, &block)
31
+ @mutex.synchronize do
32
+ @id += 1
33
+ @jobs.push Job.new(id: @id, time: time, block: block)
34
+ @jobs.sort_by!(&:time)
35
+ end
36
+ @wfd.syswrite('.')
37
+ @id
38
+ end
39
+
40
+ # cancle the timer.
41
+ # @param id [Integer] timer ID
42
+ def cancel(id)
43
+ @mutex.synchronize do
44
+ @jobs.delete_if{|job| job.id == id}
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def do_loop
51
+ Thread.new do
52
+ loop do
53
+ tout = @mutex.synchronize do
54
+ @jobs.empty? ? nil : @jobs[0].time - Time.now
55
+ end
56
+ tout = 0 if tout&.negative?
57
+ if @rfd.wait_readable(tout)
58
+ @rfd.sysread(1)
59
+ end
60
+ loop do
61
+ block = @mutex.synchronize do
62
+ @jobs.shift.block if @jobs[0] && @jobs[0].time <= Time.now
63
+ end
64
+ break unless block
65
+ block.call
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TmtmsTimer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "tmtms/timer"
2
+ require_relative "tmtms_timer/version"
@@ -0,0 +1,4 @@
1
+ module TmtmsTimer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "lib/tmtms_timer/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "tmtms_timer"
5
+ spec.version = TmtmsTimer::VERSION
6
+ spec.authors = ["TOMITA Masahiro"]
7
+ spec.email = ["tommy@tmtm.org"]
8
+
9
+ spec.summary = "Timer"
10
+ spec.description = "Timer"
11
+ spec.homepage = "https://gitlab.com/tmtms/tmtms_timer"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "https://gitlab.com/tmtms/tmtms_timer/blob/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.metadata['rubygems_mfa_required'] = 'true'
30
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmtms_timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOMITA Masahiro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Timer
14
+ email:
15
+ - tommy@tmtm.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/tmtms/timer.rb
28
+ - lib/tmtms_timer.rb
29
+ - lib/tmtms_timer/version.rb
30
+ - sig/tmtms_timer.rbs
31
+ - tmtms_timer.gemspec
32
+ homepage: https://gitlab.com/tmtms/tmtms_timer
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://gitlab.com/tmtms/tmtms_timer
37
+ source_code_uri: https://gitlab.com/tmtms/tmtms_timer
38
+ changelog_uri: https://gitlab.com/tmtms/tmtms_timer/blob/CHANGELOG.md
39
+ rubygems_mfa_required: 'true'
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.6.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.3.7
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Timer
59
+ test_files: []