GithubHelp home page GithubHelp logo

amineasli / event_dispatcher Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 296 KB

event_dispatcher implements a lightweight version of the Observer design pattern

License: GNU Lesser General Public License v3.0

Ruby 100.00%
ruby mediator event-dispatcher events emit-events

event_dispatcher's Introduction

event_dispatcher

Gem Version

event_dispatcher gem provides a simple observer implementation, allowing you to subscribe and listen for events in your application with a simple and effective way.

It is very strongly inspired by the Symfony EventDispatcher component

Install

Install the gem :

gem install event_dispatcher

Accessing the gem :

require 'event_dispatcher'

Usage

Creating an Event

When an event is dispatched, it's identified by a unique name, which any number of listeners might be listening to. An event instance is also created and passed to all of the listeners :

class UserEvent
   attr_reader :user, :login_time
   
   def initialize(user, login_time)
      @user = user
      @login_time = login_time
   end
 
   def log
     "#{login_time} : User #{user} has just logged in."
   end
end

Creating an EventDispatcher

The Dispatcher is the commander of the event dispatcher system, it maintains a registry of listeners. It's responsible for notifying listeners when events are dispatched :

dispatcher = EventDispatcher::Dispatcher.new

Connecting Listeners

To take advantage of an existing event, a listener needs to be connected to the dispatcher so that it can be notified when the event is dispatched. There are two ways for attaching a listener to the dispatcher.

Using a block :

listener = lambda do |event|
   puts event.log
end

dispatcher.add_listener(:user_login, listener)

Or using a class instance with a handler method for the event :

class UserEventListener
   def handle(event)
      puts event.log
   end
end

listener = UserEventListener.new     
dispatcher.add_listener(:user_login, listener.method(:handle))

You may also specify a priority when subscribing to events. Listeners with higher priority will be run first :

dispatcher.add_listener(:user_login, listener1, 15) 
dispatcher.add_listener(:user_login, listener2, 10) 
dispatcher.add_listener(:user_login, listener3, 2) 

Dispatching an Event

Notifies all listeners of the given event, so the event instance is then passed to each listener :

event = UserEvent.new('Bobby', Time.now) 
dispatcher.dispatch(:user_login, event)

Stopping Event propagation

You may wish to stop the propagation of an event to other listeners. To do so you need to first mixing the module Event in your custom event class, then setting the event instance variable stop_propagation to true :

class UserEvent
   include EventDispatcher::Event
   # ... 
   # ... 
end

listener = lambda do |event|
   puts event.log
   event.stop_propagation = true
end

Removing Listeners

Removes an event listener from the specified event :

dispatcher.remove_listener!(:user_login, listener)

Removes a collection of listeners from the specified event :

dispatcher.remove_listeners!(:user_login) # calling remove_listeners! without argument will simply reset the entire listeners container

Tests

rake test

License

LGPL, see LICENSE.

event_dispatcher's People

Contributors

phobosapp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.