event_timer 1.0.0 → 1.0.1
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 +8 -8
- data/lib/event_timer.rb +47 -3
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
MmFjMmE3ZGRiZTY1MTFjZjUzNjI0MzkxNDcwYTNkNzRmZDUyYjIwMQ==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
ZWZkNTQyODQyM2RhMTgwOWVjNmQwNjhhMjMwOTY3NDYzY2E1MjEyMA==
|
|
7
7
|
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
ZGM2YWRhMWU4NTc2ZDU0Y2U2OTU0NjA0MjYxZDJiZDE1MTk3MDc3ZDc2MDM1
|
|
10
|
+
MmMxNTYwMjRmZGQ2Zjk1NmE0M2M2OTU2MDM2N2JjNzVmMDIzNDJjZjBmYTc2
|
|
11
|
+
YzIwOGQ0NTJiN2ViN2VjNDJiOWM1OTY1YTY1NWUyMWRjMWJjODM=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
Y2YyYTdhYTYzOGEwMGYyYjUxOWRiNjQ5MmQxN2U1YjdiYmNkMjUwZjg1NTc0
|
|
14
|
+
YTQyMGIwNmZjYTE3NzU1ZDhlZjZjZDE5M2IzNDIyODFiYmIwZWU5MTJhMTc2
|
|
15
|
+
YTFkYjE2MWNjNTAxOTE2NDZjODRhNjA2MDNiOTVmNzUzZDg1NmM=
|
data/lib/event_timer.rb
CHANGED
|
@@ -1,30 +1,74 @@
|
|
|
1
1
|
require 'ready_pool'
|
|
2
2
|
require 'async_emitter'
|
|
3
|
-
|
|
3
|
+
####################################################################################
|
|
4
|
+
# An event timer with second granularity. It inherits from AsyncEmitter and events are
|
|
5
|
+
# captured using the AsyncEmitter methods on and once.
|
|
6
|
+
# ==Example
|
|
7
|
+
#
|
|
8
|
+
# require 'event_timer'
|
|
9
|
+
#
|
|
10
|
+
# timer = EventTimer.new 4
|
|
11
|
+
#
|
|
12
|
+
# timer.on :one, lambda { |data| puts "#{data}" }
|
|
13
|
+
# timer.on :two, lambda { |data| puts "#{data}" }
|
|
14
|
+
# timer.on :three, lambda { |data| puts "#{data}" }
|
|
15
|
+
# timer.on :four, lambda { |data| puts "#{data}" }
|
|
16
|
+
#
|
|
17
|
+
# timer.start :one, 1, 1
|
|
18
|
+
# timer.start :two, 2, 2
|
|
19
|
+
# timer.start :three, 3, 3
|
|
20
|
+
# timer.start :four, 4, 4
|
|
21
|
+
# timer.cancel :four
|
|
22
|
+
#
|
|
23
|
+
# gets
|
|
24
|
+
#
|
|
25
|
+
# @author Greg Martin
|
|
26
|
+
###################################################################################
|
|
4
27
|
class EventTimer < AsyncEmitter
|
|
28
|
+
|
|
29
|
+
###########################################################################
|
|
30
|
+
# constructor - prepares a ReadyPool for the timers. Timers can be added as
|
|
31
|
+
# needed bu it is more efficient to add them in the constructor
|
|
32
|
+
#
|
|
33
|
+
# @param num_timers [FixedNum] the number of timers expected
|
|
34
|
+
##########################################################################
|
|
5
35
|
def initialize (num_timers)
|
|
6
36
|
super()
|
|
7
37
|
@pool = ReadyPool.new num_timers, lambda { |args| timer_proc args }
|
|
8
38
|
@th_args = {}
|
|
9
39
|
end
|
|
10
40
|
|
|
41
|
+
##########################################################################
|
|
42
|
+
# Start a timer running
|
|
43
|
+
#
|
|
44
|
+
# @param event [Object] the emitter event - and valid hash key
|
|
45
|
+
# @param time [FixedNum] the number pf seconds until the event
|
|
46
|
+
# @param data [Object] data passed to the timer function
|
|
47
|
+
##########################################################################
|
|
11
48
|
def start (event, time, data)
|
|
12
49
|
args = {
|
|
13
50
|
:event => event,
|
|
14
51
|
:time => time,
|
|
15
52
|
:data => data,
|
|
16
53
|
:cancel => false,
|
|
17
|
-
:mutex => Mutex.new
|
|
54
|
+
:mutex => Mutex.new,
|
|
55
|
+
:thread => nil
|
|
18
56
|
}
|
|
19
57
|
|
|
20
58
|
@th_args[args[:event]] = args
|
|
21
59
|
|
|
22
|
-
@pool.start args
|
|
60
|
+
@th_args[args[:event]][:thread] = @pool.start args
|
|
23
61
|
end
|
|
24
62
|
|
|
63
|
+
##########################################################################
|
|
64
|
+
# Cancel an event
|
|
65
|
+
#
|
|
66
|
+
# @param event [Object] the emitter event - and valid hash key
|
|
67
|
+
##########################################################################
|
|
25
68
|
def cancel (event)
|
|
26
69
|
@th_args[event][:mutex].synchronize do
|
|
27
70
|
@th_args[event][:cancel] = true
|
|
71
|
+
@th_args[event][:thread].run
|
|
28
72
|
end
|
|
29
73
|
end
|
|
30
74
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: event_timer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Greg Martin
|
|
@@ -39,7 +39,7 @@ dependencies:
|
|
|
39
39
|
version: '1.1'
|
|
40
40
|
- - ! '>='
|
|
41
41
|
- !ruby/object:Gem::Version
|
|
42
|
-
version: 1.1.
|
|
42
|
+
version: 1.1.1
|
|
43
43
|
type: :runtime
|
|
44
44
|
prerelease: false
|
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -49,7 +49,7 @@ dependencies:
|
|
|
49
49
|
version: '1.1'
|
|
50
50
|
- - ! '>='
|
|
51
51
|
- !ruby/object:Gem::Version
|
|
52
|
-
version: 1.1.
|
|
52
|
+
version: 1.1.1
|
|
53
53
|
description: An event timer for Ruby
|
|
54
54
|
email: greg@softsprocket.com
|
|
55
55
|
executables: []
|