Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all_perform callbacks #365

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/active_interactor/interactor/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ def execute_context!
end

def execute_context_with_callbacks!
interactor.run_callbacks :perform do
result = interactor.run_callbacks :perform do
execute_context_with_validation_check!
@context = interactor.finalize_context!
end

if context&.success? && interactor.respond_to?(:run_after_perform_callbacks_on_children)
interactor.run_after_perform_callbacks_on_children
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronmallen if I leave the callbacks in are you okay with a PR that includes just this change to move where the deferred callbacks get executed?


result
end

def execute_context_with_validation!
Expand Down
124 changes: 1 addition & 123 deletions lib/active_interactor/organizer/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,133 +141,11 @@ def around_each_perform(*filters, &block)
def before_each_perform(*filters, &block)
set_callback(:each_perform, :before, *filters, &block)
end

# Define a callback to call after all {Organizer::Organize::ClassMethods#organized organized}
# {ActiveInteractor::Base interactors'} {Interactor::Perform#perform #perform} methods have been called.
#
# @since v1.2.0
#
# @example
# class MyInteractor1 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor1'
# end
# end
#
# class MyInteractor2 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor2'
# end
# end
#
# class MyOrganizer < ActiveInteractor::Organizer
# after_all_perform :print_done
#
# organized MyInteractor1, MyInteractor2
#
# private
#
# def print_done
# puts 'Done'
# end
# end
#
# MyOrganizer.perform
# "MyInteractor1"
# "MyInteractor2"
# "Done"
# #=> <MyOrganizer::Context>
def after_all_perform(*filters, &block)
set_callback(:all_perform, :after, *filters, &block)
end

# Define a callback to call around all {Organizer::Organize::ClassMethods#organized organized}
# {ActiveInteractor::Base interactors'} {Interactor::Perform#perform #perform} method calls.
#
# @since v1.2.0
#
# @example
# class MyInteractor1 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor1'
# sleep(1)
# end
# end
#
# class MyInteractor2 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor2'
# sleep(1)
# end
# end
#
# class MyOrganizer < ActiveInteractor::Organizer
# around_all_perform :print_time
#
# organized MyInteractor1, MyInteractor2
#
# private
#
# def print_time
# puts Time.now.utc
# yield
# puts Time.now.utc
# end
# end
#
# MyOrganizer.perform
# "2019-04-01 00:00:00 UTC"
# "MyInteractor1"
# "MyInteractor2"
# "2019-04-01 00:00:02 UTC"
# #=> <MyOrganizer::Context>
def around_all_perform(*filters, &block)
set_callback(:all_perform, :around, *filters, &block)
end

# Define a callback to call before all {Organizer::Organize::ClassMethods#organized organized}
# {ActiveInteractor::Base interactors'} {Interactor::Perform#perform #perform} methods have been called.
#
# @since v1.2.0
#
# @example
# class MyInteractor1 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor1'
# end
# end
#
# class MyInteractor2 < ActiveInteractor::Base
# def perform
# puts 'MyInteractor2'
# end
# end
#
# class MyOrganizer < ActiveInteractor::Organizer
# before_all_perform :print_starting
#
# organized MyInteractor1, MyInteractor2
#
# private
#
# def print_starting
# puts 'Starting'
# end
# end
#
# MyOrganizer.perform
# "Starting"
# "MyInteractor1"
# "MyInteractor2"
# #=> <MyOrganizer::Context>
def before_all_perform(*filters, &block)
set_callback(:all_perform, :before, *filters, &block)
end
end

def self.included(base)
base.class_eval do
define_callbacks :each_perform, :all_perform
define_callbacks :each_perform
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/active_interactor/organizer/interactor_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ def execute_deferred_after_perform_callbacks(context)
def init_deferred_after_perform_callbacks
after_callbacks_deferred = interactor_class.present? &&
interactor_class.after_callbacks_deferred_when_organized
@deferred_after_perform_callbacks = after_callbacks_deferred ? interactor_class._perform_callbacks : nil
@deferred_after_perform_callbacks = (interactor_class._perform_callbacks if after_callbacks_deferred)
end

def skip_deferred_after_perform_callbacks
return unless deferred_after_perform_callbacks.present?

deferred_after_perform_callbacks.each do |callback|
next unless callback.kind == :after && callback.name == :perform

interactor_class.skip_callback(:perform, :after, callback.filter, raise: false)
end
end
Expand Down
23 changes: 8 additions & 15 deletions lib/active_interactor/organizer/perform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ def self.included(base)
# {Interactor::Perform#perform #perform}. An {Base organizer} is expected not to define its own
# {Interactor::Perform#perform #perform} method in favor of this default implementation.
def perform
run_callbacks :all_perform do
if self.class.parallel
perform_in_parallel
else
perform_in_order
end
self.class.parallel ? perform_in_parallel : perform_in_order
end

def run_after_perform_callbacks_on_children
self.class.organized.each do |interface|
next unless interface.interactor_class.after_callbacks_deferred_when_organized

context.merge!(interface.execute_deferred_after_perform_callbacks(context))
end
run_after_perform_callbacks_on_interactors if context.success?
end

private
Expand Down Expand Up @@ -100,14 +101,6 @@ def perform_in_parallel
end
merge_contexts(results.map(&:value))
end

def run_after_perform_callbacks_on_interactors
self.class.organized.each do |interface|
next unless interface.interactor_class.after_callbacks_deferred_when_organized

context.merge!(interface.execute_deferred_after_perform_callbacks(context))
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@
defer_after_callbacks_when_organized

after_perform do
context.after_perform_1a = context.after_perform_1b + 1
context.steps << 'after_perform_1a'
end

after_perform do
context.after_perform_1b = context.after_perform_2 + 1
context.steps << 'after_perform_1b'
end

def perform
context.perform_1 = 1
context.steps = []
context.steps << 'perform_1'
end
end
end

let!(:test_interactor_2) do
build_interactor('TestInteractor2') do
after_perform do
context.after_perform_2 = context.perform_2 + 1
context.steps << 'after_perform_2'
end

def perform
context.perform_2 = context.after_perform_3a + 1
context.steps << 'perform_2'
end
end
end
Expand All @@ -38,31 +39,31 @@ def perform
defer_after_callbacks_when_organized

after_perform do
context.after_perform_3a = context.after_perform_3b + 1
context.steps << 'after_perform_3a'
end

after_perform do
context.after_perform_3b = context.after_perform_4a + 1
context.steps << 'after_perform_3b'
end

def perform
context.perform_3 = context.perform_1 + 1
context.steps << 'perform_3'
end
end
end

let!(:test_interactor_4) do
build_interactor('TestInteractor4') do
after_perform do
context.after_perform_4a = context.after_perform_4b + 1
context.steps << 'after_perform_4a'
end

after_perform do
context.after_perform_4b = context.perform_4 + 1
context.steps << 'after_perform_4b'
end

def perform
context.perform_4 = context.perform_3 + 1
context.steps << 'perform_4'
end
end
end
Expand All @@ -72,11 +73,11 @@ def perform
defer_after_callbacks_when_organized

after_perform do
context.after_perform_5a = context.after_perform_5b + 1
context.steps << 'after_perform_5a'
end

after_perform do
context.after_perform_5b = context.after_perform_1a + 1
context.steps << 'after_perform_5b'
end

organize TestInteractor3, TestInteractor4
Expand Down Expand Up @@ -107,19 +108,21 @@ def perform
it { is_expected.to be_a interactor_class.context_class }
it { is_expected.to be_successful }
it { is_expected.to have_attributes(
perform_1: 1,
perform_3: 2,
perform_4: 3,
after_perform_4b: 4,
after_perform_4a: 5,
after_perform_3b: 6,
after_perform_3a: 7,
perform_2: 8,
after_perform_2: 9,
after_perform_1b: 10,
after_perform_1a: 11,
after_perform_5b: 12,
after_perform_5a: 13,
steps: [
'perform_1',
'perform_3',
'perform_4',
'after_perform_4b',
'after_perform_4a',
'after_perform_3b',
'after_perform_3a',
'perform_2',
'after_perform_2',
'after_perform_1b',
'after_perform_1a',
'after_perform_5b',
'after_perform_5a',
]
) }
end
end
Loading