From ff6bb7d626c923761443f62dcc489d3a3cdb6b30 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Thu, 14 Jul 2022 13:51:37 -0700 Subject: [PATCH] retain the default listeners for future unsubscribing subscribe the hyrax default listeners in batch. provide a hook for looking up the `Hyrax.publisher.default_listeners`. this makes it possible to unsubscribe individual listeners at a later time. these instances have to live in memory for the life of the Publisher anyway, so this shouldn't impact when they are freed for garbage collection. --- config/initializers/listeners.rb | 14 ++++---------- lib/hyrax/publisher.rb | 16 ++++++++++++++++ spec/hyrax/publisher_spec.rb | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 spec/hyrax/publisher_spec.rb diff --git a/config/initializers/listeners.rb b/config/initializers/listeners.rb index e394a76743..2a55fe25f3 100644 --- a/config/initializers/listeners.rb +++ b/config/initializers/listeners.rb @@ -1,16 +1,10 @@ # frozen_string_literal: true -Hyrax.publisher.subscribe(Hyrax::Listeners::AclIndexListener.new) Hyrax.publisher.subscribe(Hyrax::Listeners::ActiveFedoraAclIndexListener.new) unless Hyrax.config.disable_wings -Hyrax.publisher.subscribe(Hyrax::Listeners::BatchNotificationListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::FileMetadataListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::FileSetLifecycleListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::FileSetLifecycleNotificationListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::MemberCleanupListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::MetadataIndexListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::ObjectLifecycleListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::TrophyCleanupListener.new) -Hyrax.publisher.subscribe(Hyrax::Listeners::WorkflowListener.new) + +Hyrax.publisher.default_listeners.each do |listener| + Hyrax.publisher.subscribe(listener) +end # Publish events from old style Hyrax::Callbacks to trigger the listeners # When callbacks are removed and replaced with direct event publication, drop these blocks diff --git a/lib/hyrax/publisher.rb b/lib/hyrax/publisher.rb index e709107f3f..db8b0ec6a9 100644 --- a/lib/hyrax/publisher.rb +++ b/lib/hyrax/publisher.rb @@ -194,5 +194,21 @@ class Publisher # @since 3.2.0 # @macro a_registered_event register_event('object.file.uploaded') + + ## + # @return Array[Object] the listeners Hyrax subscribes by default. + def default_listeners + @default_listeners ||= + [Hyrax::Listeners::AclIndexListener.new, + Hyrax::Listeners::BatchNotificationListener.new, + Hyrax::Listeners::FileMetadataListener.new, + Hyrax::Listeners::FileSetLifecycleListener.new, + Hyrax::Listeners::FileSetLifecycleNotificationListener.new, + Hyrax::Listeners::MemberCleanupListener.new, + Hyrax::Listeners::MetadataIndexListener.new, + Hyrax::Listeners::ObjectLifecycleListener.new, + Hyrax::Listeners::TrophyCleanupListener.new, + Hyrax::Listeners::WorkflowListener.new].freeze + end end end diff --git a/spec/hyrax/publisher_spec.rb b/spec/hyrax/publisher_spec.rb new file mode 100644 index 0000000000..23bdeabd81 --- /dev/null +++ b/spec/hyrax/publisher_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +RSpec.describe Hyrax::Publisher do + subject(:publisher) { described_class.instance } # singleton instance + + describe "#default_listeners" do + it "returns a collection of listeners" do + # listeners can be any Object, so we can't verify they are valid here + expect(publisher.default_listeners).to be_a Enumerable + end + + it "returns the same collection on successive calls" do + expect(publisher.default_listeners).to eql publisher.default_listeners + end + end +end