events 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ module Events # :nodoc:
2
+
3
+ # The Events::Emitter mixin provides a clone of the Node.js EventEmitter API
4
+ # for Ruby.
5
+ #
6
+ # Instances of classes including Events::Emitter will emit events, events are
7
+ # represented by symbols, underscored in the usual Ruby fashion. Examples:
8
+ # :connection, :data, :message_begin
9
+ #
10
+ # Blocks can be attached to objects to be executed when an event is emitted,
11
+ # these blocks are called listeners.
12
+ #
13
+ # All EventEmitters emit the event :new_listener when new listeners are added,
14
+ # this listener is provided with the event and new listener added.
15
+ # Example:
16
+ # server.add_listener(:new_listener) do |event, listener|
17
+ # puts "added new listener #{listener} for event #{event}"
18
+ # end
19
+ #
20
+ # server.add_listener(:connection) do |socket|
21
+ # puts "someone connected!"
22
+ # end
23
+ # Outputs "added new listener #<Proc:0x0000000000000000@example.rb:12> for
24
+ # event connection".
25
+ #
26
+ module Emitter
27
+
28
+ # :call-seq: emitter.listeners(event) -> array
29
+ #
30
+ # Returns an array of listeners for the specified event. This array can be
31
+ # manipulated, e.g. to remove listeners.
32
+ #
33
+ def listeners(event)
34
+ (@listeners ||= Hash.new {|hash, key| hash[key] = []})[event]
35
+ end
36
+
37
+ # :call-seq: emitter.emit(event[, arguments...]) -> bool
38
+ #
39
+ # Execute each of the listeners in order with the supplied arguments.
40
+ #
41
+ def emit(event, *args)
42
+ listeners(event).each do |listener|
43
+ listener.call(*args)
44
+ end.any?
45
+ end
46
+
47
+ # :call-seq: emitter.add_listener(event, &block) -> emitter
48
+ #
49
+ # Adds a listener to the end of the listeners array for the specified event.
50
+ # server.add_listener(:connection) do |socket|
51
+ # puts "someone connected!"
52
+ # end
53
+ #
54
+ def add_listener(event, &block)
55
+ emit(:new_listener, event, block)
56
+ listeners(event).push(block)
57
+ self
58
+ end
59
+
60
+ # :call-seq: emitter.remove_listener(event, proc) -> emitter
61
+ #
62
+ # Remove a listener from the listener array for the specified event.
63
+ #
64
+ def remove_listener(event, proc)
65
+ listeners(event).delete(proc)
66
+ self
67
+ end
68
+ end
69
+
70
+ # The Events::EventEmitter class provides a clone of the Node.js EventEmitter
71
+ # API for Ruby.
72
+ #
73
+ # It simply includes the Events::Emitter module, and is provided as a
74
+ # convenience for those who wish to inherit from a class.
75
+ #
76
+ # See Events::Emitter for more.
77
+ #
78
+ class EventEmitter
79
+ include Emitter
80
+ end
81
+ end
@@ -0,0 +1,51 @@
1
+ =Events::Emitter
2
+
3
+ The Events::Emitter mixin provides a clone of the Node.js EventEmitter API for
4
+ Ruby.
5
+
6
+ Instances of classes including Events::Emitter will emit events, events are
7
+ represented by symbols, underscored in the usual Ruby fashion. Examples:
8
+ :connection, :data, :message_begin
9
+
10
+ Blocks can be attached to objects to be executed when an event is emitted, these
11
+ blocks are called listeners.
12
+
13
+ All EventEmitters emit the event :new_listener when new listeners are added,
14
+ this listener is provided with the event and new listener added.
15
+ Example:
16
+ server.add_listener(:new_listener) do |event, listener|
17
+ puts "added new listener #{listener} for event #{event}"
18
+ end
19
+
20
+ server.add_listener(:connection) do |socket|
21
+ puts "someone connected!"
22
+ end
23
+ Outputs "added new listener #<Proc:0x0000000000000000@example.rb:12> for event
24
+ connection".
25
+
26
+ ==Events::EventEmitter
27
+
28
+ The Events::EventEmitter class is provided as a convenience for those who wish
29
+ to inherit from a class. It simply includes the Events::Emitter module.
30
+
31
+ ==Methods
32
+
33
+ emitter.add_listener(event, &block)
34
+
35
+ Adds a listener to the end of the listeners array for the specified event.
36
+ server.add_listener(:connection) do |socket|
37
+ puts "someone connected!"
38
+ end
39
+
40
+ emitter.remove_listener(event, proc)
41
+
42
+ Remove a listener from the listener array for the specified event.
43
+
44
+ emitter.listeners(event)
45
+
46
+ Returns an array of listeners for the specified event. This array can be
47
+ manipulated, e.g. to remove listeners.
48
+
49
+ emitter.emit(event[, arguments...])
50
+
51
+ Execute each of the listeners in order with the supplied arguments.
@@ -0,0 +1,67 @@
1
+ require "test/unit"
2
+ require "../lib/events"
3
+
4
+ # Tests transliterated from javascript originals at http://github.com/ry/node/
5
+ class TestEventEmitter < Test::Unit::TestCase
6
+
7
+ # /test/simple/test-event-emitter-add-listeners.js
8
+ def test_event_emitter_add_listeners
9
+ e = Events::EventEmitter.new
10
+
11
+ events_new_listener_emited = []
12
+ times_hello_emited = 0
13
+
14
+ e.add_listener(:new_listener) do |event, listener|
15
+ puts "new_listener: #{event}"
16
+ events_new_listener_emited.push(event)
17
+ end
18
+
19
+ e.add_listener(:hello) do |a, b|
20
+ puts "hello"
21
+ times_hello_emited += 1
22
+ assert_equal("a", a)
23
+ assert_equal("b", b)
24
+ end
25
+
26
+ puts "start"
27
+
28
+ e.emit(:hello, "a", "b")
29
+
30
+ assert_equal([:hello], events_new_listener_emited)
31
+ assert_equal(1, times_hello_emited)
32
+ end
33
+
34
+ # /test/simple/test-event-emitter-modify-in-emit.js
35
+ def test_event_emitter_modify_in_emit
36
+ callbacks_called = []
37
+
38
+ e = Events::EventEmitter.new
39
+
40
+ @callback1 = Proc.new do
41
+ callbacks_called.push(:callback1)
42
+ e.add_listener(:foo, &@callback2)
43
+ e.remove_listener(:foo, @callback1)
44
+ end
45
+
46
+ @callback2 = Proc.new do
47
+ callbacks_called.push(:callback2)
48
+ e.remove_listener(:foo, @callback2)
49
+ end
50
+
51
+ e.add_listener(:foo, &@callback1)
52
+ assert_equal(1, e.listeners(:foo).length)
53
+
54
+ e.emit(:foo)
55
+ assert_equal(1, e.listeners(:foo).length)
56
+ assert_equal([:callback1], callbacks_called)
57
+
58
+ e.emit(:foo)
59
+ assert_equal(0, e.listeners(:foo).length)
60
+ assert_equal([:callback1, :callback2], callbacks_called)
61
+
62
+ e.emit(:foo)
63
+ assert_equal(0, e.listeners(:foo).length)
64
+ assert_equal([:callback1, :callback2], callbacks_called)
65
+ end
66
+
67
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: events
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 0
9
+ version: 0.9.0
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Sadler
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-07 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: The Events::Emitter mixin provides a clone of the Node.js EventEmitter API for Ruby.
22
+ email: mat@sourcetagsandcodes.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - readme.rdoc
29
+ files:
30
+ - lib/events.rb
31
+ - test/event_emitter_test.rb
32
+ - readme.rdoc
33
+ has_rdoc: true
34
+ homepage: http://github.com/matsadler/rb-event-emitter
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --main
40
+ - readme.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Clone of the node.js EventEmitter api for Ruby
64
+ test_files:
65
+ - test/event_emitter_test.rb