event_emitter 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.2 2012-11-07
2
+
3
+ * add function "once"
4
+
1
5
  === 0.0.1 2012-11-07
2
6
 
3
7
  * implement "on" and "emit"
data/README.rdoc CHANGED
@@ -32,6 +32,25 @@ call event
32
32
  foo.emit :chat, {:user => 'shokai', :message => 'hello!!'}
33
33
  # => chat - shokai : hello!!
34
34
 
35
+ regist event using "once"
36
+
37
+ foo.once :bar do |data|
38
+ puts data
39
+ end
40
+
41
+ call
42
+
43
+ foo.emit :bar, 100 # => call only first time
44
+ foo.emit :bar, 200 # => not call
45
+
46
+ see samples https://github.com/shokai/event_emitter/tree/master/samples
47
+
48
+ == TEST:
49
+
50
+ % gem install bundler
51
+ % bundle install
52
+ % rake test
53
+
35
54
  == LICENSE:
36
55
 
37
56
  (The MIT License)
data/lib/event_emitter.rb CHANGED
@@ -4,5 +4,5 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'event_emitter/emitter'
5
5
 
6
6
  module EventEmitter
7
- VERSION = '0.0.1'
7
+ VERSION = '0.0.2'
8
8
  end
@@ -13,16 +13,46 @@ module EventEmitter
13
13
  @events ||= []
14
14
  end
15
15
 
16
- def on(type, &block)
16
+ def add_listener(type, params={}, &block)
17
17
  raise ArgumentError, 'listener block not given' unless block_given?
18
- events << {:type => type.to_sym, :listener => block}
18
+ id = events.empty? ? 0 : events.last[:id]+1
19
+ events << {
20
+ :type => type.to_sym,
21
+ :listener => block,
22
+ :params => params,
23
+ :id => id
24
+ }
25
+ id
26
+ end
27
+
28
+ alias :on :add_listener
29
+
30
+ def remove_listener(id_or_type)
31
+ if id_or_type.class == Fixnum
32
+ events.delete_if do |e|
33
+ e[:id] == id_or_type
34
+ end
35
+ elsif [String, Symbol].include? id_or_type.class
36
+ events.delete_if do |e|
37
+ e[:type] == id_or_type.to_sym
38
+ end
39
+ end
19
40
  end
20
41
 
21
42
  def emit(type, data)
22
43
  events.each do |e|
23
- e[:listener].call data if e[:type] == type.to_sym
44
+ if e[:type] == type.to_sym
45
+ listener = e[:listener]
46
+ remove_listener e[:id] if e[:params][:once]
47
+ listener.call data
48
+ end
24
49
  end
25
50
  end
51
+
52
+ def once(type, &block)
53
+ add_listener type, {:once => true}, &block
54
+ end
55
+
26
56
  end
27
57
 
28
58
  end
data/samples/sample.rb CHANGED
@@ -12,13 +12,20 @@ foo.on 'chat' do |data|
12
12
  puts "chat - #{data[:user]} : #{data[:message]}"
13
13
  end
14
14
 
15
- foo.on 'sum' do |data|
15
+ foo.on :sum do |data|
16
16
  puts data.inject{|a,b|
17
17
  a+b
18
18
  }
19
19
  end
20
20
 
21
+ foo.once :bar do |data|
22
+ puts "hello #{data}"
23
+ end
24
+
21
25
  foo.emit 'chat', {:user => 'shokai', :message => 'hello world'}
22
26
  foo.emit :chat, :user => 'ymrl', :message => 'hello work'
23
27
 
24
- foo.emit 'sum', [1,2,3,45]
28
+ foo.emit :sum, [1,2,3,45]
29
+
30
+ foo.emit :bar, 'shokai'
31
+ foo.emit :bar, 'shooooookai' # not call
@@ -13,7 +13,6 @@ class TestEventEmitter < MiniTest::Unit::TestCase
13
13
  def test_on_emit
14
14
  result = nil
15
15
  @foo.on :chat do |data|
16
- puts "chat - #{data[:user]} : #{data[:message]}"
17
16
  result = data
18
17
  end
19
18
 
@@ -22,4 +21,48 @@ class TestEventEmitter < MiniTest::Unit::TestCase
22
21
  assert result[:user] == 'shokai'
23
22
  assert result[:message] == 'hello world'
24
23
  end
24
+
25
+ def test_add_listener
26
+ result = nil
27
+ @foo.add_listener :chat do |data|
28
+ result = data
29
+ end
30
+
31
+ @foo.emit :chat, :user => 'shokai', :message => 'hello world'
32
+
33
+ assert result[:user] == 'shokai'
34
+ assert result[:message] == 'hello world'
35
+ end
36
+
37
+ def test_remove_listener
38
+ @foo.on :bar do |data|
39
+ puts "bar #{data}"
40
+ end
41
+ @foo.on :bar do |data|
42
+ puts "barbar: #{data}"
43
+ end
44
+
45
+ id = @foo.on :baz do |data|
46
+ p data
47
+ end
48
+
49
+ assert @foo.events.size == 3, 'check registerd listener count'
50
+ @foo.remove_listener id
51
+ assert @foo.events.size == 2, 'remove listener by id'
52
+
53
+ @foo.remove_listener :bar
54
+ assert @foo.events.size == 0, 'remove all "bar" listener'
55
+ end
56
+
57
+ def test_once
58
+ total = 0
59
+ @foo.once :add do |data|
60
+ total += data
61
+ end
62
+
63
+ @foo.emit :add, 1
64
+ assert total == 1, 'first call'
65
+ @foo.emit :add, 1
66
+ assert total == 1, 'call listener only first time'
67
+ end
25
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -101,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  segments:
103
103
  - 0
104
- hash: 4410593862136715009
104
+ hash: 1154001054108273116
105
105
  required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements: