Code Monkey home page Code Monkey logo

kittyevents's Introduction

๐Ÿ˜ป KittyEvents

Gem Version Build Status

Super simple event system built on top of ActiveJob.

KittyEvents implements the publish/subscribe pattern using ActiveJob. You setup your events and list the subscribers for them. When an event is triggered, KittyEvents will fanout the event to each of your subscribers.

Why use this

  • Uses ActiveJob. No need to add a new dependency for pub/sub.
  • Reduce complexity/establish patterns. Can be used to replace after_commit's. This creates easier to follow/read code. Less surprises = good!
  • Replace several perform_later's with a single event trigger. Reducing the amount of I/O happening in request (less I/O = faster response times)

Installation

Add this line to your application's Gemfile:

gem 'kitty_events'

And then execute:

$ bundle install

Usage

In Rails, setup your events and subscribers.

module ApplicationEvents
  extend KittyEvents

  event :user_signup, [
    WelcomeEmailWorker,
    WelcomeTweetWorker,
    SyncProfileImageWorker,
    ExampleWorker.set(wait: 5.minutes), # standard ActiveJob settings work as well!
  ]

  event :user_upvote, [
    SomeWorker,
    AnotherWorker,
  ]
end

You can put this in app/workers or similar folder.

Each subscriber must be an ActiveJob and respond to perform_later(object).

class ExampleWorker < ActiveJob::Base
  def perform(user)
    # do work
  end
end

Then in your application, to trigger an event. Do the following.

ApplicationEvents.trigger(:user_signup, user)

Using the above example, triggering this event would pass user to each of the subscribers defined in our initializer:

WelcomeEmailWorker,
WelcomeTweetWorker,
SyncProfileImageWorker,
ExampleWorker.set(wait: 5.minutes)

Worker Configuration

The worker is configurable via the initializer. Here are examples for catching errors and setting the queue.

# config/initializers/application_events.rb
module ApplicationEvents
  extend KittyEvents

  event :user_signup, [
    WelcomeEmailWorker,
    WelcomeTweetWorker,
  ]

  handle_worker.rescue_from ActiveJob::DeserializationError do |exception|
    # handle deserialization errors
  end

  handle_worker.queue_as :events # use a specific queue
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/producthunt/kittyevents. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

Product Hunt

 _________________
< The MIT License >
 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

kittyevents's People

Contributors

emilov avatar mscoutermarsh avatar rstankov avatar

Stargazers

 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mitchellporter

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.