Lightweight event library based on C# Events.
Add this to your application's shard.yml
:
dependencies:
graphite:
github: panickbr/graphite
require "graphite"
- Define a Custom EventArgs
class MyEventArgs < Graphite::EventArgs
getter string : String
def initialize(@string = "")
end
end
- Define a Publisher
class Publisher
getter on_event = Graphite::Event.new(:on_event)
def event(string)
on_event.fire MyEventArgs.new(string)
end
end
:on_event
is the Event
's signature. It's what a Subscriber
will check to react to an Event
accordingly
- Define a Subscriber
class Subscriber
include Graphite::Subscriber # Required
def on_notify(e : Symbol, args : EventArgs) # Required
case e
when :on_event
# You have to check args type to access its methods/variables
if args.is_a? MyEventArgs
call_some_method(args.string)
end
end
end
end
- Subscribe to an Event
pub = Publisher.new
sub = Subscriber.new
pub.on_event << sub
When inside the Subscriber, pass itself
to subscribe:
class Subscriber
# ...
pub.on_event << itself
# ...
end
Done. Now every time Publisher#event
or Publisher#on_event.fire
is called, all on_event
subscribers will be notified and react according to its own #on_notify
method.
TODO: Write development instructions here
- Fork it ( https://github.com/[your-github-name]/graphite/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- panickbr panickbr - creator, maintainer