events 0.9.0 → 0.9.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.
- data/lib/events.rb +22 -1
- data/test/event_emitter_test.rb +62 -3
- metadata +3 -3
data/lib/events.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
module Events # :nodoc:
|
2
|
+
UncaughtError = Class.new(StandardError)
|
2
3
|
|
3
4
|
# The Events::Emitter mixin provides a clone of the Node.js EventEmitter API
|
4
5
|
# for Ruby.
|
@@ -23,6 +24,10 @@ module Events # :nodoc:
|
|
23
24
|
# Outputs "added new listener #<Proc:0x0000000000000000@example.rb:12> for
|
24
25
|
# event connection".
|
25
26
|
#
|
27
|
+
# When an EventEmitter experiences an error, the typical action is to emit an
|
28
|
+
# :error event. Error events are special -- if there is no handler for them
|
29
|
+
# they raise an Events::UncaughtError exception.
|
30
|
+
#
|
26
31
|
module Emitter
|
27
32
|
|
28
33
|
# :call-seq: emitter.listeners(event) -> array
|
@@ -39,7 +44,14 @@ module Events # :nodoc:
|
|
39
44
|
# Execute each of the listeners in order with the supplied arguments.
|
40
45
|
#
|
41
46
|
def emit(event, *args)
|
42
|
-
listeners(event).
|
47
|
+
listeners = listeners(event).dup
|
48
|
+
|
49
|
+
if event == :error && listeners.empty?
|
50
|
+
raise args.first if args.first.kind_of?(Exception)
|
51
|
+
raise Events::UncaughtError.new("Uncaught, unspecified 'error' event.")
|
52
|
+
end
|
53
|
+
|
54
|
+
listeners.each do |listener|
|
43
55
|
listener.call(*args)
|
44
56
|
end.any?
|
45
57
|
end
|
@@ -65,6 +77,15 @@ module Events # :nodoc:
|
|
65
77
|
listeners(event).delete(proc)
|
66
78
|
self
|
67
79
|
end
|
80
|
+
|
81
|
+
# :call-seq: emitter.remove_all_listeners(event) -> emitter
|
82
|
+
#
|
83
|
+
# Removes all listeners from the listener array for the specified event.
|
84
|
+
#
|
85
|
+
def remove_all_listeners(event)
|
86
|
+
listeners(event).clear
|
87
|
+
self
|
88
|
+
end
|
68
89
|
end
|
69
90
|
|
70
91
|
# The Events::EventEmitter class provides a clone of the Node.js EventEmitter
|
data/test/event_emitter_test.rb
CHANGED
@@ -40,6 +40,7 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
40
40
|
@callback1 = Proc.new do
|
41
41
|
callbacks_called.push(:callback1)
|
42
42
|
e.add_listener(:foo, &@callback2)
|
43
|
+
e.add_listener(:foo, &@callback3)
|
43
44
|
e.remove_listener(:foo, @callback1)
|
44
45
|
end
|
45
46
|
|
@@ -48,20 +49,78 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
48
49
|
e.remove_listener(:foo, @callback2)
|
49
50
|
end
|
50
51
|
|
52
|
+
@callback3 = Proc.new do
|
53
|
+
callbacks_called.push(:callback3)
|
54
|
+
e.remove_listener(:foo, @callback3)
|
55
|
+
end
|
56
|
+
|
51
57
|
e.add_listener(:foo, &@callback1)
|
52
58
|
assert_equal(1, e.listeners(:foo).length)
|
53
59
|
|
54
60
|
e.emit(:foo)
|
55
|
-
assert_equal(
|
61
|
+
assert_equal(2, e.listeners(:foo).length)
|
56
62
|
assert_equal([:callback1], callbacks_called)
|
57
63
|
|
58
64
|
e.emit(:foo)
|
59
65
|
assert_equal(0, e.listeners(:foo).length)
|
60
|
-
assert_equal([:callback1, :callback2], callbacks_called)
|
66
|
+
assert_equal([:callback1, :callback2, :callback3], callbacks_called)
|
67
|
+
|
68
|
+
e.emit(:foo)
|
69
|
+
assert_equal(0, e.listeners(:foo).length)
|
70
|
+
assert_equal([:callback1, :callback2, :callback3], callbacks_called)
|
71
|
+
|
72
|
+
e.add_listener(:foo, &@callback1)
|
73
|
+
e.add_listener(:foo, &@callback2)
|
74
|
+
assert_equal(2, e.listeners(:foo).length)
|
75
|
+
e.remove_all_listeners(:foo)
|
76
|
+
assert_equal(0, e.listeners(:foo).length)
|
77
|
+
|
78
|
+
# Verify that removing callbacks while in emit allows emits to propagate to
|
79
|
+
# all listeners
|
80
|
+
callbacks_called = []
|
61
81
|
|
82
|
+
e.add_listener(:foo, &@callback2)
|
83
|
+
e.add_listener(:foo, &@callback3)
|
84
|
+
assert_equal(2, e.listeners(:foo).length)
|
62
85
|
e.emit(:foo)
|
86
|
+
assert_equal([:callback2, :callback3], callbacks_called)
|
63
87
|
assert_equal(0, e.listeners(:foo).length)
|
64
|
-
|
88
|
+
end
|
89
|
+
|
90
|
+
# /test/simple/test-event-emitter-remove-listeners.js
|
91
|
+
def test_event_emitter_remove_listeners
|
92
|
+
count = 0
|
93
|
+
|
94
|
+
listener1 = Proc.new do
|
95
|
+
puts("listener1")
|
96
|
+
count +=1
|
97
|
+
end
|
98
|
+
|
99
|
+
listener2 = Proc.new do
|
100
|
+
puts("listener2")
|
101
|
+
count +=1
|
102
|
+
end
|
103
|
+
|
104
|
+
listener3 = Proc.new do
|
105
|
+
puts("listener3")
|
106
|
+
count +=1
|
107
|
+
end
|
108
|
+
|
109
|
+
e1 = Events::EventEmitter.new
|
110
|
+
e1.add_listener(:hello, &listener1)
|
111
|
+
e1.remove_listener(:hello, listener1)
|
112
|
+
assert_equal([], e1.listeners(:hello))
|
113
|
+
|
114
|
+
e2 = Events::EventEmitter.new
|
115
|
+
e2.add_listener(:hello, &listener1)
|
116
|
+
e2.remove_listener(:hello, listener2)
|
117
|
+
assert_equal([listener1], e2.listeners(:hello))
|
118
|
+
|
119
|
+
e3 = Events::EventEmitter.new
|
120
|
+
e3.add_listener(:hello, &listener1)
|
121
|
+
e3.add_listener(:hello, &listener2)
|
122
|
+
e3.remove_listener(:hello, listener1)
|
123
|
+
assert_equal([listener2], e3.listeners(:hello))
|
65
124
|
end
|
66
125
|
|
67
126
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 1
|
9
|
+
version: 0.9.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matthew Sadler
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-15 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|