Skip to content
Browse files

cache compiled callbacks

  • Loading branch information...
1 parent 3aee912 commit ade7d365e434b586a4c1747d917beb986cb35fc7 @tenderlove tenderlove committed May 10, 2013
Showing with 27 additions and 7 deletions.
  1. +27 −7 activesupport/lib/active_support/callbacks.rb
View
34 activesupport/lib/active_support/callbacks.rb
@@ -350,29 +350,46 @@ def initialize(name, config)
:scope => [ :kind ]
}.merge!(config)
@chain = []
+ @callbacks = nil
end
def each(&block); @chain.each(&block); end
def index(o); @chain.index(o); end
- def insert(index, o); @chain.insert(index, o); end
- def delete(o); @chain.delete(o); end
- def clear; @chain.clear; self; end
def empty?; @chain.empty?; end
+ def insert(index, o)
+ @callbacks = nil
+ @chain.insert(index, o)
+ end
+
+ def delete(o)
+ @callbacks = nil
+ @chain.delete(o)
+ end
+
+ def clear
+ @callbacks = nil
+ @chain.clear
+ self
+ end
+
def initialize_copy(other)
- @chain = other.chain.dup
+ @callbacks = nil
+ @chain = other.chain.dup
end
def compile
- callbacks = lambda { |env|
+ return @callbacks if @callbacks
+
+ @callbacks = lambda { |env|
block = env.run_block
env.value = !env.halted && (!block || block.call)
env
}
@chain.reverse_each do |callback|
- callbacks = callback.apply(callbacks)
+ @callbacks = callback.apply(@callbacks)
end
- callbacks
+ @callbacks
end
def append(*callbacks)
@@ -389,16 +406,19 @@ def chain; @chain; end
private
def append_one(callback)
+ @callbacks = nil
remove_duplicates(callback)
@chain.push(callback)
end
def prepend_one(callback)
+ @callbacks = nil
remove_duplicates(callback)
@chain.unshift(callback)
end
def remove_duplicates(callback)
+ @callbacks = nil
@chain.delete_if { |c| callback.duplicates?(c) }
end

0 comments on commit ade7d36

Please sign in to comment.
Something went wrong with that request. Please try again.