Giter VIP home page Giter VIP logo

event_emitter's Introduction

event_emitter

Build Status

Install

% gem install event_emitter

Synopsys

load rubygem

require "rubygems"
require "event_emitter"

include

class User
  include EventEmitter
  attr_accessor :name
end

regist event listener

user = User.new
user.name = "shokai"
user.on :go do |data|
  puts "#{name} go to #{data[:place]}"
end

call event

user.emit :go, {:place => "mountain"}
# => "shokai go to mountain"

regist event using "once"

user.once :eat do |what, where|
  puts "#{name} -> eat #{what} at #{where}"
end

call

user.emit :eat, "BEEF", "zanmai"  # =>  "shokai -> eat BEEF at zanmai"
user.emit :eat, "Ramen", "marutomo"  # => do not call. call only first time.

apply as instance-specific method

class Foo
end

foo = Foo.new
EventEmitter.apply foo

remove event listener

user.remove_listener :go
user.remove_listener event_id

catch all events

user.on :* do |event_name, args|
  puts event_name + " called"
  p args
end

see samples https://github.com/shokai/event_emitter/tree/master/samples

Test

% gem install bundler
% bundle install
% rake test

Benchmark

% rake benchmark

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

event_emitter's People

Contributors

cfcosta avatar shokai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

event_emitter's Issues

on/emit multiple arguments

support multiple arguments

EventEmitter.apply foo

foo.on :chat do |user, message, id|
  puts "#{user} : #{message} <#{id}>"
end

foo.emit :chat, "shokai", "hello!!", 1234

method names prefix

solution for method names "on" and "emit" conflict.

EventEmitter::prefix = '___'

class Foo
  include EventEmitter
end

foo = Foo.new

prefix was set

foo.___on(type, listener)
foo.___emit(type, data)

emit speed up

ruby 2.0.0-p0 (2013-02-24) [x86_64-darwin12.3.0]
       user     system      total        real
100Kon_1emit  2.910000   0.110000   3.020000 (  3.268702)
1Kon_1Kemit  5.290000   0.030000   5.320000 (  5.593177)
1on_100Kemit  0.830000   0.010000   0.840000 (  0.884736)
ruby 1.9.3-p392 (2013-02-22) [x86_64-darwin12.3.0]
       user     system      total        real
100Kon_1emit  2.500000   0.110000   2.610000 (  2.744506)
1Kon_1Kemit  4.610000   0.020000   4.630000 (  4.828477)
1on_100Kemit  0.840000   0.010000   0.850000 (  0.909776)
ruby 1.9.2-p320 (2012-04-20) [x86_64-darwin12.3.0]
      user     system      total        real
100Kon_1emit  2.440000   0.110000   2.550000 (  2.688111)
1Kon_1Kemit  4.820000   0.030000   4.850000 (  5.053036)
1on_100Kemit  0.690000   0.010000   0.700000 (  0.751837)
ruby 1.8.7-p371 (2012-10-12) [i686-darwin12.3.0]
      user     system      total        real
100Kon_1emit  6.360000   0.470000   6.830000 (  7.210291)
1Kon_1Kemit  9.590000   0.040000   9.630000 ( 10.141170)
1on_100Kemit  1.730000   0.000000   1.730000 (  1.789773)
ruby 1.9.3-p385 (2013-02-21) [java]
       user     system      total        real
100Kon_1emit  6.330000   0.190000   6.520000 (  4.425000)
1Kon_1Kemit  5.540000   0.050000   5.590000 (  5.412000)
1on_100Kemit  0.990000   0.010000   1.000000 (  1.052000)

rename events -> __events

eventsという名前は、onやemitに比べてクラスにmixinした時に既存のメソッド名に衝突しやすそう

そのうえeventsに直接アクセスする事はほとんど無いので、名前を変えたほうがいい

apply on/emit as instance-specific method

インスタンスへの特異メソッドとしてon/emitを装備させる

class User
end

user = User.new
EventEmitter.apply user

user.on :go do |data|
  puts data
end

catch all event

現状

登録して

foo.on :bar do |arg1, arg2|
  puts arg1
  puts arg2
end

呼び出す

foo.emit :bar, 'hoge', 'asdfasdf'

:* で全部キャッチ

:* で登録すると全てのemitをキャッチできる

foo.on :* do |event_name, data|
end

"once" does not call

once1~4 should call from first bar

#!/usr/bin/env ruby
require 'rubygems'
require 'event_emitter'
require 'pp'

class Foo
  include EventEmitter
end

foo = Foo.new
foo.on :bar do
  puts "bar!!  <#{__events.size}>"
end

foo.once :bar do
  puts "once1"
end

foo.once :bar do
  puts "once2"
end

foo.once :bar do
  puts "once3"
end

foo.once :bar do
  puts "once4"
end

pp foo.__events

foo.emit :bar
foo.emit :bar
foo.emit :bar

puts "events:#{foo.__events.size}"
[{:type=>:bar,
  :listener=>#<Proc:[email protected]:11>,
  :params=>{},
  :id=>0},
 {:type=>:bar,
  :listener=>#<Proc:[email protected]:15>,
  :params=>{:once=>true},
  :id=>1},
 {:type=>:bar,
  :listener=>#<Proc:[email protected]:19>,
  :params=>{:once=>true},
  :id=>2},
 {:type=>:bar,
  :listener=>#<Proc:[email protected]:23>,
  :params=>{:once=>true},
  :id=>3},
 {:type=>:bar,
  :listener=>#<Proc:[email protected]:27>,
  :params=>{:once=>true},
  :id=>4}]
bar!!  <5>
once1
once3
bar!!  <3>
once2
bar!!  <2>
once4
events:1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.