discourse/lib/discourse_event.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

36 lines
1008 B
Ruby
Raw Normal View History

# frozen_string_literal: true
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.
class 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
def self.trigger(event_name, *args, **kwargs)
2013-02-12 23:45:10 -05:00
events[event_name].each { |event| event.call(*args, **kwargs) }
end
2013-02-05 14:16:51 -05:00
2013-02-12 23:45:10 -05:00
def self.on(event_name, &block)
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",
raise_error: true,
)
end
2013-02-12 23:45:10 -05:00
events[event_name] << block
end
2013-02-05 14:16:51 -05:00
def self.off(event_name, &block)
raise ArgumentError.new "DiscourseEvent.off must reference a block" if block.nil?
events[event_name].delete(block)
end
def self.all_off(event_name)
events.delete(event_name)
end
2013-02-05 14:16:51 -05:00
end