mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:21:12 +08:00
Move discourse_plugin to lib
This commit is contained in:
24
lib/discourse_event.rb
Normal file
24
lib/discourse_event.rb
Normal file
@ -0,0 +1,24 @@
|
||||
# This is meant to be used by plugins to trigger and listen to events
|
||||
# So we can execute code when things happen.
|
||||
module DiscourseEvent
|
||||
|
||||
# Defaults to a hash where default values are empty sets.
|
||||
def self.events
|
||||
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
||||
end
|
||||
|
||||
def self.trigger(event_name, *params)
|
||||
events[event_name].each do |event|
|
||||
event.call(*params)
|
||||
end
|
||||
end
|
||||
|
||||
def self.on(event_name, &block)
|
||||
events[event_name] << block
|
||||
end
|
||||
|
||||
def self.clear
|
||||
@events = nil
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user