async_emitter 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/async_emitter.rb +49 -30
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTYyM2Y2ZWRhY2I3MTUyMzY0YTAxYzAxZTY2ZjhhNDQ5OTQwYzA3NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjNhMmM1ZjliZTM5ZWY3Y2FiNWNhMTRiMmNhYmUxNzY4MTdiNTdlOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Njg5MDFjMjI0YTk0Y2NkOGI3YTk1ZjdiZmY4ZGZlMTkwNDhjMjZkYjViOTJh
|
10
|
+
ZDIyNjRkNjY3OThmMjc2ODJiZWYxMjU2OGVmMjQ0OTJhZWQyMjAzZDkzNmNm
|
11
|
+
M2YzYTdiMTIxZTUxNGQ5OGFlNjc3MmUwZDYyMzA5ZDdiNTk1ZmI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzA0MzQzYzkyZmIyODBiYjFkYzZjY2ExNTkwYjg5M2YyZmY2M2IyMDIyZjYz
|
14
|
+
YjRiNzBmMjg1MTAzYTZiOWNkNWJhYjYyNWFlMjdmN2UzZjk5OTgzOTkyYWE2
|
15
|
+
OGU5ZjI0ZmJkMTdkMDQzNzg2YmI4YWEyMWQxNjIzM2M1MDk1MTU=
|
data/lib/async_emitter.rb
CHANGED
@@ -1,40 +1,42 @@
|
|
1
1
|
require 'thread'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
emitter
|
13
|
-
emitter.on :
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
data
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
3
|
+
####################################################################################
|
4
|
+
# The AsyncEmitter class provides a mechanism for asyncronous communication
|
5
|
+
# in Ruby programs. Each instantiation provides notification of events
|
6
|
+
# registered using any object that is valid as a Hash key. Multiple
|
7
|
+
# events can be registered for each key and listeners can be registered
|
8
|
+
# for one or many events. Listeners for a key event can be released.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# emitter = AsyncEmitter.new
|
12
|
+
# emitter.on :error, lambda { |e| puts "Error: #{e}" }
|
13
|
+
# emitter.on :data, lambda { |data| puts "Data: #{data}" }
|
14
|
+
#
|
15
|
+
# begin
|
16
|
+
# data = get_data_from_somewhere
|
17
|
+
# emitter.emit :data, data
|
18
|
+
# rescue Exception => e
|
19
|
+
# emitter.emit :error, e
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# Where more then one listener is registered for an event they are
|
23
|
+
# notified in the order they are recieved.
|
24
|
+
#
|
25
|
+
# @author Greg Martin
|
26
|
+
####################################################################################
|
26
27
|
|
28
|
+
class AsyncEmitter
|
27
29
|
def initialize
|
28
30
|
@emissions = {}
|
29
31
|
end
|
30
32
|
|
31
|
-
|
32
33
|
########################################################################
|
33
34
|
# Register for notification
|
34
35
|
#
|
35
|
-
# token
|
36
|
-
# p
|
37
|
-
# once_only
|
36
|
+
# @param token [Object] any valid Hash key representing the event
|
37
|
+
# @param p [Proc] a procedure to be called on notification
|
38
|
+
# @param once_only [Boolean] defualts to false, if true the notification
|
39
|
+
# is removed after being fired once
|
38
40
|
# ######################################################################
|
39
41
|
def on (token, p, once_only=false)
|
40
42
|
@emissions[token] ||= {}
|
@@ -67,13 +69,20 @@ class AsyncEmitter
|
|
67
69
|
# Register for single notification - convenience and self documenting
|
68
70
|
# method for: on token, proc, true
|
69
71
|
#
|
70
|
-
# token
|
71
|
-
# p
|
72
|
+
# @param token [Object] any valid Hash key representing the event
|
73
|
+
# @param p [Proc] a procedure to be called on notification
|
72
74
|
# ######################################################################
|
73
75
|
def once (token, p)
|
74
76
|
self.on token, p, true
|
75
77
|
end
|
76
78
|
|
79
|
+
|
80
|
+
#######################################################################
|
81
|
+
# Send notification of an event
|
82
|
+
#
|
83
|
+
# @param token [Object] the Hash key representing the event
|
84
|
+
# @param data [Object] argument to be passed to the events procedure
|
85
|
+
# #####################################################################
|
77
86
|
def emit (token, data)
|
78
87
|
@emissions[token][:semaphore] ||= Mutex.new
|
79
88
|
@emissions[token][:cv] ||= ConditionVariable.new
|
@@ -90,13 +99,23 @@ class AsyncEmitter
|
|
90
99
|
########################################################################
|
91
100
|
# Remove notification for an event
|
92
101
|
#
|
93
|
-
# token
|
102
|
+
# @param token [Object] Hash key representing the event
|
94
103
|
########################################################################
|
95
104
|
def release (token)
|
96
105
|
@emissions[token][:active] = false
|
97
106
|
Thread.kill @emissions[token][:thread]
|
98
107
|
end
|
99
108
|
|
109
|
+
########################################################################
|
110
|
+
# Remove all notifications
|
111
|
+
########################################################################
|
112
|
+
def release_all
|
113
|
+
@emissions.each do |key, value|
|
114
|
+
value[:active] = false
|
115
|
+
Thread.kill value[:thread]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
100
119
|
protected
|
101
120
|
def post_data_for (token)
|
102
121
|
@emissions[token][:p].each_index do |i|
|