Add documentation to Houdini::EventPublisher

This commit is contained in:
Eric Schultz 2021-01-11 12:46:40 -06:00 committed by Eric Schultz
parent 04b48408c8
commit 31deb4cce8

View file

@ -4,17 +4,61 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
require 'wisper' require 'wisper'
require 'wisper/activejob' require 'wisper/activejob'
###
# @description: An event publisher in Houdini for calling listening to async events or responding to them
# Normally, you would use `Houdini.event_publisher` to access the active event publisher after initialization
#
# If you want to add an event listener as part of initialization (which is normally what you want to do),
# you should push your listener class to Rails.application.config.houdini.listeners, which is an array.
#
# Example: Rails.application.config.houdini.listeners.push(PaymentCreationEventListener)
#
# Listener classes have the methods with the name of every event they want to listen to
# as class methods. As an example the if the listener class SupporterListener wanted to run
# some code when the `supporter_create`, or `supporter_merge` event was announced,
# Supporter Listener would look like so:
#
# class SupporterListener
# def self.supporter_create(supporter)
# # run some code
# end
#
# def self.supporter_merge(*supporters)
# # run some code
# end
# end
#
# Currently, all listeners are called asynchronously using ActiveJob. This may change soonish.
###
class Houdini::EventPublisher class Houdini::EventPublisher
include Wisper::Publisher include Wisper::Publisher
def announce(event, *args) ###
broadcast(event, *args) # @description: announce a new event to all of the listeners listening for `event_type`
# @param event_type {symbol}: the type of event being announced.
# @param args {array}: the arguments to be passed to the event listeners.
###
def announce(event_type, *args)
broadcast(event_type, *args)
end end
###
# @description: asynchronously listen for an event to occur
# Normally, you don't call this directly, you'll push an listener class to the
# Rails.application.config.houdini.listeners array in an initializer
# @param listener {Class}: the listener class
###
def subscribe_async(listener, options = {}) def subscribe_async(listener, options = {})
subscribe(listener, options.merge(async: true)) subscribe(listener, options.merge(async: true))
end end
###
# @description: having an array of listeners
# Normally, you don't call this directly, you'll push a set of listener classes
# to Rails.application.config.houdini.listeners array in an initializer
# @param listeners {Array}: an array of listener classes
###
def subscribe_all(listeners, options = {}) def subscribe_all(listeners, options = {})
listeners.each do |listener| listeners.each do |listener|
subscribe(listener, options.merge(async: true)) subscribe(listener, options.merge(async: true))