Coder Social home page Coder Social logo

event_bus's Introduction

EventBus

A simple pubsub event bus for Ruby applications.

Build Status Dependency Status Code Climate

Features

  • Simple, global support for the Observer pattern, aka Publisher-Subscriber.
  • Publish and subscribe to events throughout your Ruby application.
  • Listen for events without coupling to the publishing object or class.
  • Subscribe to events using names or regex patterns.
  • Works with Rails.
  • Works without Rails.
  • Completely synchronous (use the event_bg_bus gem for async operation using Sidekiq).

Installation

Install the gem

gem install event_bus

Or add it to your Gemfile and run bundle.

gem 'event_bus'

Usage

Publishing events

Publish events whenever something significant happens in your application:

class PlaceOrder
  //...
  EventBus.announce(:order_placed, order: current_order, customer: current_user)
end

The event name (first argument) can be a String or a Symbol. The Hash is optional and supplies a payload of information to any subscribers.

(If you don't like the method name announce you can use publish or broadcast instead.)

Subscribing to events

There are three ways to subscribe to events.

  1. Subscribe a listener object:

    EventBus.subscribe(StatsRecorder.new)

    The event will be handled by a method whose name matches the event name:

    class StatsRecorder
      def order_placed(payload)
        order = payload[:order]
        //...
      end
    end

    If the object has no matching method, it doesn't receive the event.

  2. Specify the method to be called when the event fires:

    EventBus.subscribe(:order_placed, StatsRecorder.new, :print_order)

    In this case the event will be handled by the print_order method:

    class StatsRecorder
      def print_order(payload)
        order = payload[:order]
        //...
      end
    end

    The first argument to subscribe can be a String, a Symbol or a Regexp:

    EventBus.subscribe(/order/, StatsRecorder.new, :print_order)
  3. Subscribe a block:

    EventBus.subscribe(:order_placed) do |payload|
      order = payload[:order]
      //...
    end

    The argument to subscribe can be a String, a Symbol or a Regexp:

    EventBus.subscribe(/order/) do |payload|
      order = payload[:order]
      //...
    end

See the specs for more detailed usage scenarios.

Error Handling

Any exception raised by the event listeners will be swallowed, and will not affect the execution flow. In order to handle errors you can register a global error handler:

EventBus.on_error do |listener, payload|
  #handle the error
end

The supplied block will be called once for each error that is raised by any listener, for any event.

The block will be provided with two parameters, the listener that errored, and the payload of the event. The payload of the event will have an extra error value containing the exception object that was raised:

EventBus.on_error do |listener, payload|
  puts listener.inspect        # => #<Proc:0x007fec9305a500@lib/event_bus_example.rb:10>
  puts payload.inspect         # => {:event_name=>:order_placed, :order_id=>1, :error=>#<StandardError: something went wrong>}
  puts payload[:error].inspect # => #<StandardError: something went wrong>
end

EventBus.subscribe(:order_placed) do |payload|
  raise StandardError, 'something went wrong'
end

EventBus.announce(:order_placed, order_id: 1)

Compatibility

Tested with Ruby 2.1, 2.0, 1.9.x, JRuby. See the build status for details.

License

(The MIT License)

Copyright (c) 2013 Kevin Rutherford

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

event_bus's People

Contributors

jacegu avatar jmoses avatar kevinrutherford avatar stuartrexking 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

event_bus's Issues

Pass the event name with events

Subscribers who are listening via a regex may need to know which event in particular was triggered. So we need to add the event name to the details hash.

Multiple busses

For thread-safety and/or usage of multiple busses, it would be nice to be able to instantiate the EventBus class.

Something like this:

bus = EventBus.new
bus.subscribe(:event, &block)

It could be backwards compatible with the class methods that exists now using a default instance of EventBus.

Is this something you would consider a good thing?
In that case I will be happy to implement :)

Release 1.1.0

master contains some useful things (e.g. EventBus.with_temporary_subscriber) that I'd like in a release. Would it be possible to ship 1.1.0?

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.