mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 13:09:18 +08:00
29 lines
541 B
Ruby
29 lines
541 B
Ruby
|
# This is meant to be used by plugins to trigger and listen to events
|
||
|
# So we can execute code when things happen.
|
||
|
module DiscourseEvent
|
||
|
|
||
|
class << self
|
||
|
|
||
|
def trigger(event_name, *params)
|
||
|
|
||
|
return unless @events
|
||
|
return unless event_list = @events[event_name]
|
||
|
|
||
|
event_list.each do |ev|
|
||
|
ev.call(*params)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def on(event_name, &block)
|
||
|
@events ||= {}
|
||
|
@events[event_name] ||= Set.new
|
||
|
@events[event_name] << block
|
||
|
end
|
||
|
|
||
|
def clear
|
||
|
@events = {}
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|