spectrum v0.2.0

A simple event framework using syringe DI.

Spectrum

A simple event framework using syringe DI.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
       spectrum:
        github: elliotize/spectrum
    
  2. Run shards install

Usage

require "spectrum"

class FooBarOneEvent
  include Spectrum::Event
  def initialize(@id : Int32)
  end
end

class FooBarTwoEvent
  include Spectrum::Event
  def initialize(@id : Int32)
  end
end

class FooBarOneEventHandler
  include Spectrum::EventHandler
  Syringe.injectable

  def on_event(event : FooBarOneEvent)
    # Do something
  end
end

class FooBarTwoEventHandler
  include Spectrum::EventHandler
  Syringe.injectable

  def on_event(event : FooBarTwoEvent)
    # Do something
  end
end

class FooBarBothEventHandler
  include Spectrum::EventHandler
  Syringe.injectable

  def on_event(event : FooBarOneEvent)
    # Do something
  end

  def on_event(event : FooBarTwoEvent)
    # Do something
  end
end

## Sync event
event_one = FooBarOneEvent.new(id: 1)
Spectrum.dispatch_sync_event(event_one)
event_two = FooBarTwoEvent.new(id: 2)
Spectrum.dispatch_sync_event(event_two)

## Async event
Spectrum::EventQueue.start(:my_queue)
event_one = FooBarOneEvent.new(id: 1)
Spectrum.dispatch_async_event(event_one, :my_queue)
event_two = FooBarTwoEvent.new(id: 2)
Spectrum.dispatch_async_event(event_two, :my_queue)
Spectrum::EventQueue.stop(:my_queue)

## Advanced Queue Settings
Spectrum::EventQueue.start(
  name: :my_queue,
  number_of_workers: 10
  queue_size: 10
)

Contributing

  1. Fork it (https://github.com/elliotize/spectrum/fork)
  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 a new Pull Request

Contributors

Repository

spectrum

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 1
  • over 4 years ago
  • July 12, 2019
License

MIT License

Links
Synced at

Thu, 07 Nov 2024 03:49:12 GMT

Languages