2013-02-06 03:16:51 +08:00
|
|
|
# This is meant to be used by plugins to trigger and listen to events
|
|
|
|
# So we can execute code when things happen.
|
2017-06-01 15:19:43 +08:00
|
|
|
class DiscourseEvent
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-13 12:45:10 +08: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-06 03:16:51 +08:00
|
|
|
|
2013-02-13 12:45:10 +08:00
|
|
|
def self.trigger(event_name, *params)
|
|
|
|
events[event_name].each do |event|
|
|
|
|
event.call(*params)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-13 12:45:10 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-13 12:45:10 +08:00
|
|
|
def self.on(event_name, &block)
|
2019-04-18 23:48:01 +08:00
|
|
|
if event_name == :site_setting_saved
|
|
|
|
Discourse.deprecate("The :site_setting_saved event is deprecated. Please use :site_setting_changed instead", since: "2.3.0beta8", drop_from: "2.4")
|
|
|
|
end
|
2013-02-13 12:45:10 +08:00
|
|
|
events[event_name] << block
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-04-01 00:58:56 +08:00
|
|
|
def self.off(event_name, &block)
|
|
|
|
events[event_name].delete(block)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|