2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06: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 02:19:43 -05:00
|
|
|
class DiscourseEvent
|
2013-02-12 22:45:10 -06: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 13:16:51 -06:00
|
|
|
|
2023-01-09 04:26:39 -06:00
|
|
|
def self.trigger(event_name, *args, **kwargs)
|
2013-02-12 22:45:10 -06:00
|
|
|
events[event_name].each { |event| event.call(*args, **kwargs) }
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-12 22:45:10 -06:00
|
|
|
def self.on(event_name, &block)
|
2019-04-18 10:48:01 -05:00
|
|
|
if event_name == :site_setting_saved
|
2021-12-02 09:33:03 -06:00
|
|
|
Discourse.deprecate(
|
|
|
|
"The :site_setting_saved event is deprecated. Please use :site_setting_changed instead",
|
|
|
|
since: "2.3.0beta8",
|
|
|
|
drop_from: "2.4",
|
|
|
|
raise_error: true,
|
|
|
|
)
|
2019-04-18 10:48:01 -05:00
|
|
|
end
|
2013-02-12 22:45:10 -06:00
|
|
|
events[event_name] << block
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
def self.off(event_name, &block)
|
2020-11-11 13:46:13 -06:00
|
|
|
raise ArgumentError.new "DiscourseEvent.off must reference a block" if block.nil?
|
2015-03-31 11:58:56 -05:00
|
|
|
events[event_name].delete(block)
|
|
|
|
end
|
|
|
|
|
2021-06-22 13:00:04 -05:00
|
|
|
def self.all_off(event_name)
|
|
|
|
events.delete(event_name)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|