lib/acfs/global.rb in acfs-1.3.1 vs lib/acfs/global.rb in acfs-1.3.2
- old
+ new
@@ -71,20 +71,28 @@
raise ArgumentError.new 'Given resource is not an Acfs resource ' \
"delegator but a: #{resource.class.name}"
end
return false if block.nil?
- if resource.loaded?
+ if resource.nil? || resource.loaded?
block.call resource
else
resource.__callbacks__ << block
end
end
def on(*resources)
+ # If all resources have already been loaded, we run the callback immediately.
+ if resources.all? {|res| res.nil? || res.loaded? }
+ yield(*resources)
+ return
+ end
+
+ # Otherwise, we add a callback to *each* resource with a guard that ensures
+ # that only the very last resource being loaded executes the callback.
resources.each do |resource|
add_callback resource do |_|
- yield(*resources) unless resources.any? {|res| !res.loaded? }
+ yield(*resources) if resources.all? {|res| res.nil? || res.loaded? }
end
end
end
end
end