event_aggregator 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -2
- data/README.md +37 -18
- data/event_aggregator.gemspec +1 -1
- data/lib/event_aggregator.rb +1 -1
- data/lib/event_aggregator/aggregator.rb +3 -3
- data/lib/event_aggregator/listener.rb +1 -1
- data/lib/event_aggregator/version.rb +1 -1
- data/spec/spec_helper.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
SHA1:
|
3
|
-
data.tar.gz: 8e02703a95246297b726756d8d37d3a19a518d1e
|
4
|
-
metadata.gz: 7fc37e093b95d0031783fe65badfa94aab39e8d8
|
5
2
|
SHA512:
|
6
|
-
|
7
|
-
|
3
|
+
metadata.gz: e881aefb5e0e91954065b076a7812c034893cb7d453ee2bb83d7cc21547028ebff2255f0b8284a7495c480df6068e7013c791dfde491cd0c82ce1a4ecbe383c0
|
4
|
+
data.tar.gz: 5e6d092c11a2c3740e91385823fb3f5c66f63759eee94c4e17a578b927cccb1c666b4853202a8ec47d7c068220220bc0944d8bd39711d4ad59261b676caa00b9
|
5
|
+
SHA1:
|
6
|
+
metadata.gz: 7c6f7f2e65af541dd7a64557fbae4f5ca2838cb2
|
7
|
+
data.tar.gz: 89ba6cafac97b7bd659843c1510aaa4c75281f7a
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -74,24 +74,16 @@ Or install it yourself as:
|
|
74
74
|
#=> data
|
75
75
|
|
76
76
|
### IMPORTANT: Asynchronous by Default
|
77
|
-
Message.publish is asynchronous by default. This means that if you run event_aggregator in a script that terminates, there is a chance that the script will terminate before the workers have processed the messages
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
/Users/user/.rvm/gems/ruby-1.9.3-p429/gems/celluloid-0.15.2/lib/celluloid/actor.rb:185:in `run'
|
88
|
-
/Users/user/.rvm/gems/ruby-1.9.3-p429/gems/celluloid-0.15.2/lib/celluloid/actor.rb:157:in `block in initialize'
|
89
|
-
/Users/user/.rvm/gems/ruby-1.9.3-p429/gems/celluloid-0.15.2/lib/celluloid/thread_handle.rb:13:in `block in initialize'
|
90
|
-
/Users/user/.rvm/gems/ruby-1.9.3-p429/gems/celluloid-0.15.2/lib/celluloid/internal_pool.rb:100:in `call'
|
91
|
-
/Users/user/.rvm/gems/ruby-1.9.3-p429/gems/celluloid-0.15.2/lib/celluloid/internal_pool.rb:100:in `block in create'
|
92
|
-
W, [2013-12-29T11:17:29.660271 #48097] WARN -- : Terminating task: type=:finalizer, meta={:method_name=>:__shutdown__}, status=:callwait
|
93
|
-
|
94
|
-
To make it synchronous (not recommended) use the following:
|
77
|
+
Message.publish is asynchronous by default. This means that if you run event_aggregator in a script that terminates, there is a chance that the script will terminate before the workers have processed the messages. This might cause errors.
|
78
|
+
|
79
|
+
A simple way to get around this problem is to do the following:
|
80
|
+
|
81
|
+
#....setup...
|
82
|
+
EventAggregator::Message.new("foo2", "data").publish
|
83
|
+
|
84
|
+
gets #This will wait for user input.
|
85
|
+
|
86
|
+
To make the message processing synchronous (not recommended) use the following:
|
95
87
|
|
96
88
|
EventAggregator::Message.new("foo", "data", false).publish
|
97
89
|
#=> data
|
@@ -129,6 +121,33 @@ This enables the following:
|
|
129
121
|
#=> "foo bar bar bar bar"
|
130
122
|
|
131
123
|
|
124
|
+
## Producers
|
125
|
+
In version 1.1+ the concept of producers are added. They are blocks or methods that responds to requests. A producer must be registered, which is done like this:
|
126
|
+
|
127
|
+
#listener is an instance of a class that includes EventAggregator::Listener, similar to the Foo class above.
|
128
|
+
listener.producer_register("MultiplyByTwo", lambda{|data| return data*2})
|
129
|
+
|
130
|
+
Then, somewhere in your code, you can do the following:
|
131
|
+
|
132
|
+
number = EventAggregator::Message.new("MultiplyByTwo", 3).request
|
133
|
+
puts number
|
134
|
+
# => 6
|
135
|
+
|
136
|
+
The producers are a good way to abstract away the retrieval of certain information.
|
137
|
+
|
138
|
+
Note: Message reqests are always blocking.
|
139
|
+
|
140
|
+
## Message translation
|
141
|
+
In version 1.1+ the concept of message translation is added. This allows you to have messages on a specific type spawn other messages. To translate message type "type_1" into "type_2" you do:
|
142
|
+
|
143
|
+
#Anywhere in your code
|
144
|
+
EventAggregator::Aggregator.translate_message_with("type_1", "type_2")
|
145
|
+
|
146
|
+
It is also possible to transform the data in the conversion. To double the data value between "type_1" and "type_2" you do:
|
147
|
+
|
148
|
+
EventAggregator::Aggregator.translate_message_with("type_1", "type_2", lambda{|data| data*2})
|
149
|
+
|
150
|
+
This is often very usefull when you have one module that has a specific task, and it should be truly independent of other objects, even the message type they produce. The message translation allows you to have one file where you list all translations to give you a good overview and high maintainability.
|
132
151
|
|
133
152
|
## Usage Considerations
|
134
153
|
All messages are processed asynchronous by default. This means that there might be raise conditions in your code.
|
data/event_aggregator.gemspec
CHANGED
data/lib/event_aggregator.rb
CHANGED
@@ -11,7 +11,7 @@ module EventAggregator
|
|
11
11
|
# end
|
12
12
|
class Aggregator
|
13
13
|
class <<self; private :new; end
|
14
|
-
|
14
|
+
@@pool = Thread.pool(4)
|
15
15
|
@@listeners = Hash.new{|h, k| h[k] = Hash.new }
|
16
16
|
@@listeners_all = Hash.new
|
17
17
|
@@message_translation = Hash.new{|h, k| h[k] = Hash.new }
|
@@ -156,8 +156,8 @@ module EventAggregator
|
|
156
156
|
private
|
157
157
|
def self.perform_message_job(data, callback, async, consisten_data)
|
158
158
|
case [async, consisten_data]
|
159
|
-
when [true, true] then
|
160
|
-
when [true, false] then
|
159
|
+
when [true, true] then @@pool.process{ EventAggregator::MessageJob.new.perform(data, callback) }
|
160
|
+
when [true, false] then @@pool.process{ EventAggregator::MessageJob.new.perform(data.clone, callback) }
|
161
161
|
when [false, true] then EventAggregator::MessageJob.new.perform(data, callback)
|
162
162
|
when [false, false] then EventAggregator::MessageJob.new.perform(data.clone, callback)
|
163
163
|
end
|
@@ -71,7 +71,7 @@ module EventAggregator
|
|
71
71
|
#
|
72
72
|
# Excample:
|
73
73
|
# listener.producer_register("MultiplyByTwo", lambda{|data| return data*2})
|
74
|
-
# number = EventAggregator::Message.new("MultiplyByTwo", 3)
|
74
|
+
# number = EventAggregator::Message.new("MultiplyByTwo", 3).request
|
75
75
|
# # => 6
|
76
76
|
#
|
77
77
|
def producer_register(message_type, callback)
|
data/spec/spec_helper.rb
CHANGED
@@ -4,10 +4,20 @@ require 'rspec'
|
|
4
4
|
#require 'rack/test'
|
5
5
|
require "faker"
|
6
6
|
require "event_aggregator"
|
7
|
+
#require "sucker_punch/testing/inline"
|
7
8
|
require 'coveralls'
|
8
9
|
Coveralls.wear!
|
9
10
|
|
10
11
|
|
12
|
+
|
13
|
+
class Thread::Pool
|
14
|
+
|
15
|
+
# Public: Overriding the process-call of the thread::pool so we can do tests better
|
16
|
+
def process (*args, &block)
|
17
|
+
block.call(*args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
11
21
|
RSpec.configure do |config|
|
12
22
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
23
|
config.run_all_when_everything_filtered = true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: event_aggregator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephan Eriksen
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-07 00:00:00 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -75,7 +75,7 @@ dependencies:
|
|
75
75
|
type: :development
|
76
76
|
version_requirements: *id006
|
77
77
|
- !ruby/object:Gem::Dependency
|
78
|
-
name:
|
78
|
+
name: thread
|
79
79
|
prerelease: false
|
80
80
|
requirement: &id007 !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|