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.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/async_emitter.rb +49 -30
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGI1OGE0ZDUzZTIwN2JiMmFmMWY5N2VhNDVlYWRjM2NjOTIwNGVjOQ==
4
+ YTYyM2Y2ZWRhY2I3MTUyMzY0YTAxYzAxZTY2ZjhhNDQ5OTQwYzA3NQ==
5
5
  data.tar.gz: !binary |-
6
- NzFlMzk0NzAxNjJiZWQxNWI1ZDI3Y2MxNDRjMDQ1MDUzNDA4YzNjZQ==
6
+ ZjNhMmM1ZjliZTM5ZWY3Y2FiNWNhMTRiMmNhYmUxNzY4MTdiNTdlOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTRmNTJkOTBjMTRmYmJlYjIyZmRkM2Y4NzBmYjRiMmE0OWMwMmMzZWQ2NTRj
10
- YjI0OGMwM2MyODgyOTA5NGIyNjU5Yjk5MTY3OTllNTJiZjI4NWVhZTA5NDlm
11
- MmI2ZjMzMTY1MmVjNTRhYmU5NzJkMzlkMWNlOGU1NmQ1YzA1OGI=
9
+ Njg5MDFjMjI0YTk0Y2NkOGI3YTk1ZjdiZmY4ZGZlMTkwNDhjMjZkYjViOTJh
10
+ ZDIyNjRkNjY3OThmMjc2ODJiZWYxMjU2OGVmMjQ0OTJhZWQyMjAzZDkzNmNm
11
+ M2YzYTdiMTIxZTUxNGQ5OGFlNjc3MmUwZDYyMzA5ZDdiNTk1ZmI=
12
12
  data.tar.gz: !binary |-
13
- OGVjMTJmYjc0MjYxZDdhMzMzYjU3NzkxMjU3OTQ2YmUyMTU2YjkyYmVmZTgw
14
- MDcyMmNkZTUwNjdhNTIyMGM0ZmM1MDk3YzQ2ZmViMjQwYjAzOGNjMDU1MjY1
15
- N2YwZjkxM2EzMzJkMWUwOWI4OTAwY2M5MzkxMjllYWU0Y2Q2YTc=
13
+ YzA0MzQzYzkyZmIyODBiYjFkYzZjY2ExNTkwYjg5M2YyZmY2M2IyMDIyZjYz
14
+ YjRiNzBmMjg1MTAzYTZiOWNkNWJhYjYyNWFlMjdmN2UzZjk5OTgzOTkyYWE2
15
+ OGU5ZjI0ZmJkMTdkMDQzNzg2YmI4YWEyMWQxNjIzM2M1MDk1MTU=
data/lib/async_emitter.rb CHANGED
@@ -1,40 +1,42 @@
1
1
  require 'thread'
2
2
 
3
- class AsyncEmitter
4
- =begin
5
- The AsyncEmitter class provides a mechanism for asyncronous communication
6
- in Ruby programs. Each instantiation provides notification of events
7
- registered using any object that is valid as a Hash key. Multiple
8
- events can be registered for each key and listeners can be registered
9
- for one or many events. Listeners for a key event can be released.
10
-
11
- example:
12
- emitter = AsyncEmitter.new
13
- emitter.on :error, lambda { |e| puts "Error: #{e}" }
14
- emitter.on :data, lambda { |data| puts "Data: #{data}" }
15
-
16
- begin
17
- data = get_data_from_somewhere
18
- emitter.emit :data, data
19
- rescue Exception => e
20
- emitter.emit :error, e
21
- end
22
-
23
- Where more then one listener is registered for an event they are
24
- notified in the order they are recieved.
25
- =end
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 - any valid Hash key representing the event
36
- # p - a procedure to be called on notification
37
- # once_only - if true the notification is removed after being fired once
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 - any valid Hash key representing the event
71
- # p - a procedure to be called on notification
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 - Hash key representing the event
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|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Martin