2013-02-05 14:16:51 -05:00
|
|
|
# This is meant to be used by plugins to trigger and listen to events
|
|
|
|
# So we can execute code when things happen.
|
|
|
|
module DiscourseEvent
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
# Defaults to a hash where default values are empty sets.
|
|
|
|
def self.events
|
|
|
|
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
def self.trigger(event_name, *params)
|
|
|
|
events[event_name].each do |event|
|
|
|
|
event.call(*params)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-12 23:45:10 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
def self.on(event_name, &block)
|
|
|
|
events[event_name] << block
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
def self.off(event_name, &block)
|
|
|
|
events[event_name].delete(block)
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
def self.clear
|
|
|
|
@events = nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|