lib/glimmer/data_binding/observable_array.rb in glimmer-2.4.1 vs lib/glimmer/data_binding/observable_array.rb in glimmer-2.5.0

- old
+ new

@@ -48,23 +48,23 @@ def add_observer(observer, *args) options = args.last.is_a?(Hash) ? args.pop : {} element_properties = args element_properties = element_properties.flatten.compact.uniq return observer if has_observer?(observer) && has_observer_element_properties?(observer, element_properties) - property_observer_list << observer + property_observer_list[observer] = options observer_element_properties[observer] = element_properties_for(observer) + Concurrent::Set.new(element_properties) if !options.empty? && options[:recursive].is_a?(Integer) options = options.clone options[:recursive] = options[:recursive] - 1 end each { |element| add_element_observer(element, observer, options) } observer end - def add_element_observers(element, options = {}) - property_observer_list.each do |observer| - add_element_observer(element, observer, options) + def add_element_observers(element, general_options = {}) + property_observer_list.each do |observer, options| + add_element_observer(element, observer, options.merge(general_options)) end end def add_element_observer(element, observer, options = {}) element_properties_for(observer).each do |property| @@ -77,11 +77,11 @@ def ensure_array_object_observer(object, options) return unless object&.is_a?(Array) array_object_observer = array_object_observer_for(object) array_observer_registration = array_object_observer.observe(object, options) - property_observer_list.each do |observer| + property_observer_list.each do |observer, options| my_registration = observer.registration_for(self) observer.add_dependent(my_registration => array_observer_registration) end end @@ -107,38 +107,38 @@ end observer end def remove_element_observers(element) - property_observer_list.each do |observer| + property_observer_list.each do |observer, options| remove_element_observer(element, observer) end end def remove_element_observer(element, observer) element_properties_for(observer).each do |property| observer.unobserve(element, property) end if element.is_a?(ObservableArray) array_object_observer_for(element).unobserve(element) - element.property_observer_list.select {|o| o.respond_to?(:observable_array) && o.observable_array == self}.each do |o| - o.deregister_all_observables + element.property_observer_list.select {|obs, opt| obs.respond_to?(:observable_array) && obs.observable_array == self}.each do |o| + o.deregister_all_observables if o.respond_to?(:deregister_all_observables) @array_object_observers.reject! {|k, v| v == o} end end end def has_observer?(observer) - property_observer_list.include?(observer) + property_observer_list.keys.include?(observer) end def has_observer_element_properties?(observer, element_properties) element_properties_for(observer).to_a.include_all?(*element_properties) end def property_observer_list - @property_observer_list ||= Concurrent::Set.new + @property_observer_list ||= Concurrent::Hash.new end def observer_element_properties @observer_element_properties ||= Concurrent::Hash.new end @@ -146,11 +146,11 @@ def element_properties_for(observer) observer_element_properties[observer] ||= Concurrent::Set.new end def notify_observers - property_observer_list.to_a.each { |o| o.call(self) } + property_observer_list.to_a.each { |obs, opt| obs.call(self) } end def <<(element) super(element).tap do add_element_observers(element) @@ -370,10 +370,10 @@ end end def unregister_dependent_observers(old_value) return unless old_value.is_a?(ObservableModel) || old_value.is_a?(ObservableArray) - property_observer_list.each { |observer| observer.unregister_dependents_with_observable(observer.registration_for(self), old_value) } + property_observer_list.each { |observer, options| observer.unregister_dependents_with_observable(observer.registration_for(self), old_value) } end alias deregister_dependent_observers unregister_dependent_observers end end end