Skip to content

Commit

Permalink
retain the default listeners for future unsubscribing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tamsin johnson committed Aug 29, 2022
1 parent 27d62a2 commit 141d96d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
14 changes: 4 additions & 10 deletions config/initializers/listeners.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/hyrax/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/hyrax/publisher_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 141d96d

Please sign in to comment.