events 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -101,7 +101,9 @@ module Events # :nodoc:
101
101
  unless listener.respond_to?(:call)
102
102
  raise ArgumentError.new("Listener must respond to #call")
103
103
  end
104
- emit(:new_listener, event, listener)
104
+
105
+ to_emit = OnceWrapper === listener ? listener.original : listener
106
+ emit(:new_listener, event, to_emit)
105
107
 
106
108
  event_listeners = listeners(event)
107
109
  event_listeners.push(listener)
@@ -151,7 +153,6 @@ module Events # :nodoc:
151
153
  @listeners[event].delete_if do |lis|
152
154
  lis == proc || lis.respond_to?(:original) && lis.original == proc
153
155
  end
154
- @listeners.delete(event) if @listeners[event].empty?
155
156
  end
156
157
  self
157
158
  end
@@ -165,7 +166,6 @@ module Events # :nodoc:
165
166
  @listeners = nil if event == :_remove_all_listeners_default_arg_
166
167
  if @listeners && @listeners.key?(event)
167
168
  @listeners[event].clear
168
- @listeners.delete(event)
169
169
  end
170
170
  self
171
171
  end
@@ -1,4 +1,4 @@
1
- =Events::Emitter
1
+ =Events::Emitter {<img src=https://travis-ci.org/matsadler/rb-event-emitter.png>}[http://travis-ci.org/matsadler/rb-event-emitter]
2
2
 
3
3
  The Events::Emitter mixin provides a clone of the Node.js EventEmitter API for
4
4
  Ruby.
@@ -1,5 +1,5 @@
1
1
  require "test/unit"
2
- require "../lib/events"
2
+ require File.expand_path("../../lib/events", __FILE__)
3
3
  require "stringio"
4
4
 
5
5
  class TestEvents < Test::Unit::TestCase
@@ -1,5 +1,5 @@
1
1
  require "test/unit"
2
- require "../lib/events"
2
+ require File.expand_path("../../lib/events", __FILE__)
3
3
 
4
4
  # Tests transliterated from javascript originals at
5
5
  # http://github.com/joyent/node/
@@ -10,19 +10,25 @@ class TestEventEmitter < Test::Unit::TestCase
10
10
  e = Events::EventEmitter.new
11
11
 
12
12
  events_new_listener_emited = []
13
+ listeners_new_listener_emited = []
13
14
  times_hello_emited = 0
14
15
 
15
16
  e.add_listener(:new_listener) do |event, listener|
16
17
  puts "new_listener: #{event}"
17
18
  events_new_listener_emited.push(event)
19
+ listeners_new_listener_emited.push(listener)
18
20
  end
19
21
 
20
- e.on(:hello) do |a, b|
22
+ hello = Proc.new do |a, b|
21
23
  puts "hello"
22
24
  times_hello_emited += 1
23
25
  assert_equal("a", a)
24
26
  assert_equal("b", b)
25
27
  end
28
+ e.on(:hello, hello)
29
+
30
+ foo = Proc.new {}
31
+ e.once(:foo, foo)
26
32
 
27
33
  puts "start"
28
34
 
@@ -33,7 +39,8 @@ class TestEventEmitter < Test::Unit::TestCase
33
39
  f = Events::EventEmitter.new
34
40
  assert_nothing_raised {f.max_listeners = 0}
35
41
 
36
- assert_equal([:hello], events_new_listener_emited)
42
+ assert_equal([:hello, :foo], events_new_listener_emited)
43
+ assert_equal([hello, foo], listeners_new_listener_emited)
37
44
  assert_equal(1, times_hello_emited)
38
45
  end
39
46
 
@@ -58,6 +65,13 @@ class TestEventEmitter < Test::Unit::TestCase
58
65
  e.on(:specific) {}
59
66
  assert(e.listeners(:specific).warned)
60
67
 
68
+ # only one
69
+ e.max_listeners = 1
70
+ e.on(:only_one) {}
71
+ assert(!e.listeners(:only_one).warned)
72
+ e.on(:only_one) {}
73
+ assert(e.listeners(:only_one).warned)
74
+
61
75
  # unlimited
62
76
  e.max_listeners = 0
63
77
  1000.times do
@@ -177,9 +191,14 @@ class TestEventEmitter < Test::Unit::TestCase
177
191
  e1 = Events::EventEmitter.new
178
192
  e1.add_listener(:foo, &listener)
179
193
  e1.add_listener(:bar, &listener)
194
+ foo_listeners = e1.listeners(:foo)
195
+ bar_listeners = e1.listeners(:bar)
180
196
  e1.remove_all_listeners(:foo)
181
197
  assert_equal([], e1.listeners(:foo))
182
198
  assert_equal([listener], e1.listeners(:bar))
199
+ # identity check, the array should not change
200
+ assert_same(foo_listeners, e1.listeners(:foo))
201
+ assert_same(bar_listeners, e1.listeners(:bar))
183
202
 
184
203
  e2 = Events::EventEmitter.new
185
204
  e2.add_listener(:foo, &listener)
@@ -209,10 +228,15 @@ class TestEventEmitter < Test::Unit::TestCase
209
228
  end
210
229
 
211
230
  e1 = Events::EventEmitter.new
231
+ e1_listeners = e1.listeners(:hello)
212
232
  e1.add_listener(:hello, &listener1)
233
+ assert_equal(1, e1_listeners.length)
213
234
  e1.remove_listener(:hello, listener1)
214
235
  assert_equal([], e1.listeners(:hello))
215
236
 
237
+ # identity check, listeners array should be the same
238
+ assert_same(e1_listeners, e1.listeners(:hello))
239
+
216
240
  e2 = Events::EventEmitter.new
217
241
  e2.add_listener(:hello, &listener1)
218
242
  e2.remove_listener(:hello, listener2)
@@ -221,8 +245,13 @@ class TestEventEmitter < Test::Unit::TestCase
221
245
  e3 = Events::EventEmitter.new
222
246
  e3.add_listener(:hello, &listener1)
223
247
  e3.add_listener(:hello, &listener2)
248
+ e3_listeners = e3.listeners(:hello)
249
+ assert_equal(2, e3_listeners.length)
224
250
  e3.remove_listener(:hello, listener1)
251
+ assert_equal(1, e3_listeners.length)
225
252
  assert_equal([listener2], e3.listeners(:hello))
253
+
254
+ assert_same(e3_listeners, e3.listeners(:hello))
226
255
  end
227
256
 
228
257
  end
@@ -0,0 +1 @@
1
+ Dir["**/*_test.rb"].each {|test| require File.expand_path(test)}
metadata CHANGED
@@ -1,67 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: events
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 9
8
- - 7
9
- version: 0.9.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.8
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Matthew Sadler
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-08-06 00:00:00 +01:00
18
- default_executable:
12
+ date: 2012-05-06 00:00:00.000000000Z
19
13
  dependencies: []
20
-
21
- description: The Events::Emitter mixin provides a clone of the Node.js EventEmitter API for Ruby.
14
+ description: The Events::Emitter mixin provides a clone of the Node.js EventEmitter
15
+ API for Ruby.
22
16
  email: mat@sourcetagsandcodes.com
23
17
  executables: []
24
-
25
18
  extensions: []
26
-
27
- extra_rdoc_files:
19
+ extra_rdoc_files:
28
20
  - readme.rdoc
29
- files:
21
+ files:
30
22
  - lib/events.rb
31
23
  - test/events_test.rb
32
24
  - test/node_transliterated_test.rb
25
+ - test/runner.rb
33
26
  - readme.rdoc
34
- has_rdoc: true
35
27
  homepage: http://github.com/matsadler/rb-event-emitter
36
28
  licenses: []
37
-
38
29
  post_install_message:
39
- rdoc_options:
30
+ rdoc_options:
40
31
  - --main
41
32
  - readme.rdoc
42
- require_paths:
33
+ require_paths:
43
34
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- segments:
49
- - 0
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
- version: "0"
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
58
47
  requirements: []
59
-
60
48
  rubyforge_project:
61
- rubygems_version: 1.3.6
49
+ rubygems_version: 1.8.10
62
50
  signing_key:
63
51
  specification_version: 3
64
52
  summary: Clone of the node.js EventEmitter api for Ruby
65
- test_files:
53
+ test_files:
66
54
  - test/events_test.rb
67
55
  - test/node_transliterated_test.rb
56
+ - test/runner.rb