-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathmessage_bus_helpers.rb
52 lines (46 loc) · 1.91 KB
/
message_bus_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Current status of this: concept..
# Todo: How to add this to controller lifecycle management (see method todo's)
# Adds a method 'message_bus_subscription' to your controller which makes it easier to
# subscribe to message bus
# Usage: message_bus_subscription :my_event, :my_method
# You can also pass a proc instead of a method. The instance method or proc will be called
# every time the event is fired. The listeners will be removed automatically as soon as the
# controller is not needed anymore.
module Volt
module MessageBusHelpers
module ClassMethods
def message_bus_subscription event, callback
callback = callback.to_sym unless callback.is_a?(Proc)
@message_bus_subscriptions ||= []
@message_bus_subscriptions << {event: event, callback: callback}
end
end
def self.included(base)
base.extend ClassMethods
end
# todo: Call this method automatically on controller startup, but only once!
# before_action won't fit here, and hook in initialize either:
# the block is executed many times (5x) (why?) on start up / with a test on main_controller
def register_message_bus
@message_bus_listeners = []
subscriptions = self.class.instance_variable_get :@message_bus_subscriptions
subscriptions ||= []
subscriptions.each do |subscription|
@message_bus_listeners << Volt.current_app.message_bus.on(subscription[:event]) do |*params|
case subscription[:callback]
when Symbol
send(subscription[:callback])
when Proc
instance_eval(&subscription[:callback])
end
end
end
end
# todo: call this automatically once controller is not needed anymore
# how to integrate this into controller lifecycle management?
def remove_message_bus_listeners
return if @message_bus_listeners.nil?
@message_bus_listeners.each &:remove
end
end
end