events 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/events.rb +35 -4
- data/readme.rdoc +8 -0
- data/test/event_emitter_test.rb +40 -0
- metadata +17 -5
data/lib/events.rb
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
module Events # :nodoc:
|
2
2
|
UncaughtError = Class.new(StandardError)
|
3
3
|
|
4
|
+
class OnceProxy # :nodoc:
|
5
|
+
attr_reader :event, :caller, :delegate
|
6
|
+
|
7
|
+
def initialize(event, caller, delegate)
|
8
|
+
@event, @caller, @delegate = event, caller, delegate
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(*args)
|
12
|
+
caller.remove_listener(event, self)
|
13
|
+
delegate.call(*args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
4
17
|
# The Events::Emitter mixin provides a clone of the Node.js EventEmitter API
|
5
18
|
# for Ruby.
|
6
19
|
#
|
@@ -56,20 +69,38 @@ module Events # :nodoc:
|
|
56
69
|
end.any?
|
57
70
|
end
|
58
71
|
|
59
|
-
# :call-seq: emitter.on(event
|
72
|
+
# :call-seq: emitter.on(event) {|args...| block} -> emitter
|
73
|
+
# emitter.on(event, proc) -> emitter
|
60
74
|
#
|
61
75
|
# Adds a listener to the end of the listeners array for the specified event.
|
62
76
|
# server.on(:connection) do |socket|
|
63
77
|
# puts "someone connected!"
|
64
78
|
# end
|
65
79
|
#
|
66
|
-
|
67
|
-
|
68
|
-
|
80
|
+
# Rather than a block, can take a second argument of a Proc (or any object
|
81
|
+
# with a #call method).
|
82
|
+
#
|
83
|
+
def add_listener(event, proc=nil, &block)
|
84
|
+
emit(:new_listener, event, proc || block)
|
85
|
+
listeners(event).push(proc || block)
|
69
86
|
self
|
70
87
|
end
|
71
88
|
alias on add_listener
|
72
89
|
|
90
|
+
|
91
|
+
# :call-seq: emitter.once(event) {|args...| block} -> emitter
|
92
|
+
# emitter.once(event, proc) -> emitter
|
93
|
+
#
|
94
|
+
# Adds a one time listener for the event. The listener is invoked only the
|
95
|
+
# first time the event is fired, after which it is removed.
|
96
|
+
# server.once(:connection) do |socket|
|
97
|
+
# puts "Ah, we have our first user!"
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
def once(event, proc=nil, &block)
|
101
|
+
add_listener(event, OnceProxy.new(event, self, proc || block))
|
102
|
+
end
|
103
|
+
|
73
104
|
# :call-seq: emitter.remove_listener(event, proc) -> emitter
|
74
105
|
#
|
75
106
|
# Remove a listener from the listener array for the specified event.
|
data/readme.rdoc
CHANGED
@@ -37,6 +37,14 @@ Adds a listener to the end of the listeners array for the specified event.
|
|
37
37
|
puts "someone connected!"
|
38
38
|
end
|
39
39
|
|
40
|
+
emitter.once(event, &block)
|
41
|
+
|
42
|
+
Adds a one time listener for the event. The listener is invoked only the
|
43
|
+
first time the event is fired, after which it is removed.
|
44
|
+
server.once(:connection) do |socket|
|
45
|
+
puts "Ah, we have our first user!"
|
46
|
+
end
|
47
|
+
|
40
48
|
emitter.remove_listener(event, proc)
|
41
49
|
|
42
50
|
Remove a listener from the listener array for the specified event.
|
data/test/event_emitter_test.rb
CHANGED
@@ -123,4 +123,44 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
123
123
|
assert_equal([listener2], e3.listeners(:hello))
|
124
124
|
end
|
125
125
|
|
126
|
+
# /test/simple/test-event-emitter-once.js
|
127
|
+
def test_event_emitter_once
|
128
|
+
e = Events::EventEmitter.new
|
129
|
+
times_hello_emited = 0
|
130
|
+
|
131
|
+
e.once(:hello) do |a, b|
|
132
|
+
times_hello_emited += 1
|
133
|
+
end
|
134
|
+
|
135
|
+
e.emit(:hello, "a", "b")
|
136
|
+
e.emit(:hello, "a", "b")
|
137
|
+
e.emit(:hello, "a", "b")
|
138
|
+
e.emit(:hello, "a", "b")
|
139
|
+
|
140
|
+
assert_equal(1, times_hello_emited)
|
141
|
+
end
|
142
|
+
|
143
|
+
# /test/simple/test-event-emitter-num-args.js
|
144
|
+
def test_event_emitter_num_args
|
145
|
+
e = Events::EventEmitter.new
|
146
|
+
num_args_emited = []
|
147
|
+
|
148
|
+
e.on(:num_args) do |*args|
|
149
|
+
num_args = args.length
|
150
|
+
puts "num_args: #{num_args}"
|
151
|
+
num_args_emited.push(num_args)
|
152
|
+
end
|
153
|
+
|
154
|
+
puts "start"
|
155
|
+
|
156
|
+
e.emit(:num_args)
|
157
|
+
e.emit(:num_args, nil)
|
158
|
+
e.emit(:num_args, nil, nil)
|
159
|
+
e.emit(:num_args, nil, nil, nil)
|
160
|
+
e.emit(:num_args, nil, nil, nil, nil)
|
161
|
+
e.emit(:num_args, nil, nil, nil, nil, nil)
|
162
|
+
|
163
|
+
assert_equal([0, 1, 2, 3, 4, 5], num_args_emited)
|
164
|
+
end
|
165
|
+
|
126
166
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 61
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 9
|
9
|
+
- 3
|
10
|
+
version: 0.9.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Matthew Sadler
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-01-30 00:00:00 +00:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -36,21 +42,27 @@ rdoc_options:
|
|
36
42
|
require_paths:
|
37
43
|
- lib
|
38
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
39
46
|
requirements:
|
40
47
|
- - ">="
|
41
48
|
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
42
52
|
version: "0"
|
43
|
-
version:
|
44
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
45
55
|
requirements:
|
46
56
|
- - ">="
|
47
57
|
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
48
61
|
version: "0"
|
49
|
-
version:
|
50
62
|
requirements: []
|
51
63
|
|
52
64
|
rubyforge_project:
|
53
|
-
rubygems_version: 1.3.
|
65
|
+
rubygems_version: 1.3.7
|
54
66
|
signing_key:
|
55
67
|
specification_version: 3
|
56
68
|
summary: Clone of the node.js EventEmitter api for Ruby
|