events 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/events.rb +6 -4
- data/test/event_emitter_test.rb +50 -1
- metadata +4 -4
data/lib/events.rb
CHANGED
@@ -103,6 +103,7 @@ module Events # :nodoc:
|
|
103
103
|
emit(:new_listener, event, listener)
|
104
104
|
|
105
105
|
event_listeners = listeners(event)
|
106
|
+
event_listeners.push(listener)
|
106
107
|
|
107
108
|
current = event_listeners.length
|
108
109
|
if max_listeners > 0 && current > max_listeners && !event_listeners.warned
|
@@ -113,7 +114,6 @@ module Events # :nodoc:
|
|
113
114
|
event_listeners.warned = true
|
114
115
|
end
|
115
116
|
|
116
|
-
event_listeners.push(listener)
|
117
117
|
self
|
118
118
|
end
|
119
119
|
alias on add_listener
|
@@ -155,11 +155,13 @@ module Events # :nodoc:
|
|
155
155
|
self
|
156
156
|
end
|
157
157
|
|
158
|
-
# :call-seq: emitter.remove_all_listeners
|
158
|
+
# :call-seq: emitter.remove_all_listeners -> emitter
|
159
|
+
# emitter.remove_all_listeners(event) -> emitter
|
159
160
|
#
|
160
|
-
# Removes all listeners
|
161
|
+
# Removes all listeners, or those of the specified event.
|
161
162
|
#
|
162
|
-
def remove_all_listeners(event)
|
163
|
+
def remove_all_listeners(event=:_remove_all_listeners_default_arg_)
|
164
|
+
@listeners = nil if event == :_remove_all_listeners_default_arg_
|
163
165
|
if @listeners && @listeners.key?(event)
|
164
166
|
@listeners[event].clear
|
165
167
|
@listeners.delete(event)
|
data/test/event_emitter_test.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require "../lib/events"
|
3
3
|
|
4
|
-
# Tests transliterated from javascript originals at
|
4
|
+
# Tests transliterated from javascript originals at
|
5
|
+
# http://github.com/joyent/node/
|
5
6
|
class TestEventEmitter < Test::Unit::TestCase
|
6
7
|
|
7
8
|
# /test/simple/test-event-emitter-add-listeners.js
|
@@ -36,6 +37,35 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
36
37
|
assert_equal(1, times_hello_emited)
|
37
38
|
end
|
38
39
|
|
40
|
+
# /test/simple/test-event-emitter-check-listener-leaks.js
|
41
|
+
def test_event_emitter_check_listener_leaks
|
42
|
+
e = Events::EventEmitter.new
|
43
|
+
|
44
|
+
# default
|
45
|
+
10.times do
|
46
|
+
e.on(:default) {}
|
47
|
+
end
|
48
|
+
assert(!e.listeners(:default).warned)
|
49
|
+
e.on(:default) {}
|
50
|
+
assert(e.listeners(:default).warned)
|
51
|
+
|
52
|
+
# specific
|
53
|
+
e.max_listeners = 5
|
54
|
+
5.times do
|
55
|
+
e.on(:specific) {}
|
56
|
+
end
|
57
|
+
assert(!e.listeners(:specific).warned)
|
58
|
+
e.on(:specific) {}
|
59
|
+
assert(e.listeners(:specific).warned)
|
60
|
+
|
61
|
+
# unlimited
|
62
|
+
e.max_listeners = 0
|
63
|
+
1000.times do
|
64
|
+
e.on(:unlimited) {}
|
65
|
+
end
|
66
|
+
assert(!e.listeners(:unlimited).warned)
|
67
|
+
end
|
68
|
+
|
39
69
|
# /test/simple/test-event-emitter-modify-in-emit.js
|
40
70
|
def test_event_emitter_modify_in_emit
|
41
71
|
callbacks_called = []
|
@@ -92,6 +122,25 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
92
122
|
assert_equal(0, e.listeners(:foo).length)
|
93
123
|
end
|
94
124
|
|
125
|
+
# /test/simple/test-event-emitter-remove-all-listeners.js
|
126
|
+
def test_event_emitter_remove_all_listeners
|
127
|
+
listener = Proc.new {}
|
128
|
+
|
129
|
+
e1 = Events::EventEmitter.new
|
130
|
+
e1.add_listener(:foo, &listener)
|
131
|
+
e1.add_listener(:bar, &listener)
|
132
|
+
e1.remove_all_listeners(:foo)
|
133
|
+
assert_equal([], e1.listeners(:foo))
|
134
|
+
assert_equal([listener], e1.listeners(:bar))
|
135
|
+
|
136
|
+
e2 = Events::EventEmitter.new
|
137
|
+
e2.add_listener(:foo, &listener)
|
138
|
+
e2.add_listener(:bar, &listener)
|
139
|
+
e2.remove_all_listeners
|
140
|
+
assert_equal([], e2.listeners(:foo))
|
141
|
+
assert_equal([], e2.listeners(:bar))
|
142
|
+
end
|
143
|
+
|
95
144
|
# /test/simple/test-event-emitter-remove-listeners.js
|
96
145
|
def test_event_emitter_remove_listeners
|
97
146
|
count = 0
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 55
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 6
|
10
|
+
version: 0.9.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matthew Sadler
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-05-25 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|