Skip to content

Commit

Permalink
Run callbacks for children within fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
adviti-mishra committed Jul 3, 2024
1 parent 69485bf commit 0b103f7
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def run_callbacks(kind, with_children: true, skip_if: nil, &block)
# @api private
def _mongoid_run_child_callbacks(kind, children: nil, &block)
if Mongoid::Config.around_callbacks_for_embeds
_mongoid_run_child_callbacks_with_around(kind, children: children, &block)
_mongoid_run_child_callbacks_with_around_fibers(kind, children: children, &block)
else
_mongoid_run_child_callbacks_without_around(kind, children: children, &block)
end
Expand Down Expand Up @@ -186,6 +186,28 @@ def _mongoid_run_child_callbacks_with_around(kind, children: nil, &block)
end
end


def _mongoid_run_child_callbacks_with_around_fibers(kind, children: nil, &block)
children = (children || cascadable_children(kind))
with_children = !Mongoid::Config.prevent_multiple_calls_of_embedded_callbacks

return block&.call if children.empty?

fibers = children.map do |child|
Fiber.new do
child.run_callbacks(child_callback_type(kind, child), with_children: with_children) do
Fiber.yield
end
end
end

fibers.each(&:resume)

block&.call

fibers.reverse.each(&:resume)
end

# Execute the callbacks of given kind for embedded documents without
# around callbacks.
#
Expand Down

0 comments on commit 0b103f7

Please sign in to comment.