time_out 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/time_out.rb +49 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 033eb8d99b6aec5067d269eaeaea528a5a37dfc4
4
+ data.tar.gz: 32e0e68b9a52ab80f6d7e46d07327c6fd904482b
5
+ SHA512:
6
+ metadata.gz: 56fb257d54ff0f989aef8e3b3ac0acad12ad03ee5672f2fe566b0f017ae54ba24452ddcd7da4d96adc7a251f0e6132e43f250007588e1306535003ac8e74bcc3
7
+ data.tar.gz: 8623ecef315310159cc1202bdf77113807b6c1ddfc603397a8446e537f755328e346ce484eeca27ae53bf104204b8ee0a6a918f6bfcb7ff7205d2da26e97d615
data/lib/time_out.rb ADDED
@@ -0,0 +1,49 @@
1
+ class TimeOut
2
+ @intervals = [];
3
+
4
+ def self.set_timeout(callback, seconds)
5
+ end_time = Time.now + seconds
6
+
7
+ Thread.new do
8
+ while Time.now <= end_time
9
+ Thread.pass
10
+ end
11
+
12
+ callback.()
13
+ end
14
+ end
15
+
16
+ def self.set_interval(callback, seconds)
17
+ interval = self.new(callback, seconds)
18
+ interval.set
19
+ @intervals << interval
20
+ @intervals.length - 1
21
+ end
22
+
23
+ def self.clear_interval(index)
24
+ @intervals[index].clear
25
+ end
26
+
27
+ def self.wait_for_intervals
28
+ Thread.list.drop(1).each(&:join)
29
+ end
30
+
31
+ def initialize(callback, seconds)
32
+ @callback, @seconds, @active = callback, seconds, true
33
+ end
34
+
35
+ def set
36
+ Thread.new do
37
+ while active
38
+ TimeOut.set_timeout(callback, seconds).join
39
+ end
40
+ end
41
+ end
42
+
43
+ def clear
44
+ @active = false
45
+ end
46
+
47
+ private
48
+ attr_reader :callback, :seconds, :active
49
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_out
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Colson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Non-blocking set_timeout and set_interval
14
+ email: danieljamescolson@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/time_out.rb
20
+ homepage: https://github.com/composerinteralia/time_out
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Async timers
44
+ test_files: []