lib/motion/observation.rb in rm-extensions-0.1.10 vs lib/motion/observation.rb in rm-extensions-0.2.0
- old
+ new
@@ -81,23 +81,21 @@
class ObservationProxy
COLLECTION_OPERATIONS = [ NSKeyValueChangeInsertion, NSKeyValueChangeRemoval, NSKeyValueChangeReplacement ]
DEFAULT_OPTIONS = NSKeyValueObservingOptionNew
def initialize(obj)
- obj.rmext_on_dealloc do |x|
- cleanup
- end
@desc = obj.inspect
@events = {}
@targets = {}
if ::RMExtensions.debug?
p "created ObservationProxy for #{@desc}"
end
end
def dealloc
@did_dealloc = true
+ cleanup
if ::RMExtensions.debug?
p "dealloc ObservationProxy for #{@desc}"
end
super
end
@@ -125,11 +123,11 @@
def remove_observer_block(target, key_path)
return if target.nil? || key_path.nil?
key_paths = @targets[target]
if !key_paths.nil? && key_paths.has_key?(key_path.to_s)
- key_paths.delete(key_path.to_s)
+ key_paths.removeObject(key_path.to_s)
end
end
def unobserve_all
keys = @targets.keys.clone
@@ -140,22 +138,22 @@
while paths.size > 0
key_path = paths.pop
target.removeObserver(self, forKeyPath:key_path)
end
end
- @targets.clear
+ @targets.removeAllObjects
end
def registered?(target, key_path)
!target.nil? && !@targets[target].nil? && @targets[target].has_key?(key_path.to_s)
end
def add_observer_block(target, key_path, &block)
return if target.nil? || key_path.nil? || block.nil?
@targets[target] ||= {}
- @targets[target][key_path.to_s] ||= []
- @targets[target][key_path.to_s] << block
+ @targets[target][key_path.to_s] ||= NSHashTable.weakObjectsHashTable
+ @targets[target][key_path.to_s].addObject block
end
# NSKeyValueObserving Protocol
def observeValueForKeyPath(key_path, ofObject:target, change:change, context:context)
@@ -177,20 +175,20 @@
end
def on(event, &block)
return if event.nil? || block.nil?
@events[event.to_s] ||= []
- @events[event.to_s] << block
+ @events[event.to_s].addObject block
end
def off(event, &block)
return if event.nil? || block.nil? || !@events.key?(event.to_s)
- @events[event.to_s].delete_if { |b| b == block }
+ @events[event.to_s].removeObject block
nil
end
def off_all
- @events.clear
+ @events.removeAllObjects
end
def trigger(event, *args)
return if event.nil? || !@events.key?(event.to_s)
@events[event.to_s].each do |block|