resugan 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -10
- data/lib/resugan/kernel.rb +9 -9
- data/lib/resugan/object.rb +2 -2
- data/lib/resugan/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c7270a33c2bdc1644287e242de2ff11d60cedcd
|
4
|
+
data.tar.gz: dd89cd1a9e3548a6214cc75ac5dc6a8d518f63a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43c806a87a2b83c278d4d23aa93a3a375009b16eb1514affab58953f77189245576edb7076879c7049fcf2bf8e497180c719c6b84e2a7ee0e192316be62e889e
|
7
|
+
data.tar.gz: 41211f23f39cd001bff7db9f3a098d22404de7b3d1ccd537bf0945a5fcb8ec48681493638fca840388931b5e480aa36b96af22c80ad0fa33fed211cf613e91a0
|
data/README.md
CHANGED
@@ -24,11 +24,11 @@ Or install it yourself as:
|
|
24
24
|
Register listeners:
|
25
25
|
|
26
26
|
```ruby
|
27
|
-
|
27
|
+
_listener :event1 do |params|
|
28
28
|
puts "hello! event 2 has been called!"
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
_listener :hay do |params|
|
32
32
|
puts "hello! someone said hay!"
|
33
33
|
end
|
34
34
|
```
|
@@ -39,9 +39,9 @@ Generate events and wrap them in a resugan block:
|
|
39
39
|
resugan {
|
40
40
|
puts "I am now going to generate an event"
|
41
41
|
|
42
|
-
|
42
|
+
_fire :event2
|
43
43
|
|
44
|
-
|
44
|
+
_fire :hay
|
45
45
|
}
|
46
46
|
```
|
47
47
|
|
@@ -49,7 +49,7 @@ Note that events don't have to be fired at the top level of the block, even obje
|
|
49
49
|
|
50
50
|
The two events should fire and should print:
|
51
51
|
|
52
|
-
```
|
52
|
+
```ruby
|
53
53
|
hello! event 2 has been called!
|
54
54
|
hello! someone said hay!
|
55
55
|
```
|
@@ -65,24 +65,53 @@ Resugan supports namespaces, allowing you to group listeners and trigger them se
|
|
65
65
|
|
66
66
|
|
67
67
|
```ruby
|
68
|
-
|
68
|
+
_listener :event1, namespace: "group1" do |params|
|
69
69
|
puts "hello! event 2 has been called!"
|
70
70
|
end
|
71
71
|
|
72
|
-
|
72
|
+
_listener :event1, namespace: "group2" do |params|
|
73
73
|
puts "hello! someone said hay!"
|
74
74
|
end
|
75
75
|
|
76
76
|
resugan "group1" do
|
77
|
-
|
77
|
+
_fire :event1
|
78
78
|
end
|
79
79
|
|
80
80
|
resugan "group2" do
|
81
|
-
|
81
|
+
_fire :event1
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
Behavior is as expected. Events under group1 will only be handled by listeners under group1 and so on.
|
86
|
+
|
87
|
+
## Customizing the Event dispatcher
|
88
|
+
|
89
|
+
The way events are consumed is entirely customizable. You can register your own event dispatcher:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class MyCustomerDispatcher
|
93
|
+
def dispatch(namespace, events)
|
94
|
+
events.each do |k,v|
|
95
|
+
Resugan::Kernel.invoke(namespace, k, v)
|
96
|
+
end
|
82
97
|
end
|
98
|
+
end
|
83
99
|
```
|
84
100
|
|
85
|
-
|
101
|
+
You need to implement your own dispatch method, captured events are passed as
|
102
|
+
parameters.
|
103
|
+
|
104
|
+
You can then set it as the default dispatcher:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
Resugan::Kernel.set_default_dispatcher(MyCustomerDispatcher)
|
108
|
+
```
|
109
|
+
|
110
|
+
Or assign it to a specific namespace:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
Resugan::Kernel.register_dispatcher(MyCustomerDispatcher, 'CustomGroup')
|
114
|
+
```
|
86
115
|
|
87
116
|
## Development
|
88
117
|
|
data/lib/resugan/kernel.rb
CHANGED
@@ -24,16 +24,16 @@ module Resugan
|
|
24
24
|
|
25
25
|
def self.register_with_namespace(namespace, event, listener_id = nil, block)
|
26
26
|
@listener_ids = {} unless @listener_ids
|
27
|
-
@
|
27
|
+
@_listener = {} unless @_listener
|
28
28
|
|
29
29
|
return self if listener_id && @listener_ids["#{namespace}_#{listener_id}"]
|
30
30
|
|
31
31
|
event = "#{namespace}_#{event}".to_sym
|
32
32
|
|
33
|
-
unless @
|
34
|
-
@
|
33
|
+
unless @_listener[event]
|
34
|
+
@_listener[event] = [block]
|
35
35
|
else
|
36
|
-
@
|
36
|
+
@_listener[event] << block
|
37
37
|
end
|
38
38
|
|
39
39
|
@listener_ids["#{namespace}_#{listener_id}"] = block if listener_id
|
@@ -43,20 +43,20 @@ module Resugan
|
|
43
43
|
|
44
44
|
def self.invoke(namespace, event, payload = [])
|
45
45
|
event = "#{namespace}_#{event}".to_sym
|
46
|
-
if @
|
47
|
-
@
|
48
|
-
|
46
|
+
if @_listener[event]
|
47
|
+
@_listener[event].each do |_listener|
|
48
|
+
_listener.call(payload.map { |p| p[:params] })
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
def self.listeners
|
54
|
-
@
|
54
|
+
@_listener
|
55
55
|
end
|
56
56
|
|
57
57
|
def self.clear
|
58
58
|
@listener_ids.clear if @listener_ids
|
59
|
-
@
|
59
|
+
@_listener.clear if @_listener
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
data/lib/resugan/object.rb
CHANGED
@@ -9,14 +9,14 @@ class Object
|
|
9
9
|
context.invoke
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
12
|
+
def _fire(event, params = {})
|
13
13
|
current_thread = Thread.current
|
14
14
|
if current_thread.resugan_context
|
15
15
|
current_thread.resugan_context.register(event, params)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def _listener(event, options = {}, &block)
|
20
20
|
Resugan::Kernel.register_with_namespace(options[:namespace], event, options[:id], ->(params) {
|
21
21
|
block.call(params)
|
22
22
|
})
|
data/lib/resugan/version.rb
CHANGED