Skip to content

Commit e950fbc

Browse files
committed
Add unloader for EventManager
To handle zeitwerk autoloader
1 parent 5a39cd0 commit e950fbc

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

app/helpers/flex/event_manager.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Flex
22
class EventManager
3+
@@subscriptions = []
4+
35
class << self
46
def subscribe(event_key, callback)
57
subscription = ActiveSupport::Notifications.subscribe(event_key) do |name, _started, _finished, _unique_id, payload|
@@ -9,13 +11,21 @@ def subscribe(event_key, callback)
911
})
1012
end
1113

14+
@@subscriptions << subscription
1215
subscription
1316
end
1417

1518
def unsubscribe(subscription)
1619
ActiveSupport::Notifications.unsubscribe(subscription)
1720
end
1821

22+
def unsubscribe_all
23+
@@subscriptions.each do |subscription|
24+
ActiveSupport::Notifications.unsubscribe(subscription)
25+
end
26+
@@subscriptions.clear
27+
end
28+
1929
def publish(event_key, payload = {})
2030
Rails.logger.debug "Event Manager: Publishing event '#{event_key}' with payload: #{payload.inspect}"
2131
ActiveSupport::Notifications.instrument(event_key, payload)

lib/flex/engine.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@ class Engine < ::Rails::Engine
1111
initializer "flex.previews" do |app|
1212
config.lookbook.preview_paths << Flex::Engine.root.join("app", "previews") if config.respond_to?(:lookbook)
1313
end
14+
15+
config.after_initialize do
16+
Rails.autoloaders.main.on_unload("Flex::EventManager") do |klass|
17+
klass.unsubscribe_all
18+
end
19+
end
1420
end
1521
end

0 commit comments

Comments
 (0)