lib/motion/observation.rb in rm-extensions-0.3.0 vs lib/motion/observation.rb in rm-extensions-0.3.1

- old
+ new

@@ -102,13 +102,10 @@ end super end def cleanup - if ::RMExtensions.debug? - p "cleanup ObservationProxy(#{@desc})" - end unobserve_all off_all true end @@ -152,32 +149,33 @@ def add_observer_block(target, key_path, block) return if target.nil? || key_path.nil? || block.nil? key_path = key_path.to_s @targets[target] ||= {} @targets[target][key_path] ||= [] + block.weak! @targets[target][key_path].addObject block end # NSKeyValueObserving Protocol def observeValueForKeyPath(key_path, ofObject:target, change:change, context:context) - m_desc = nil - if ::RMExtensions.debug? - m_desc = "~~> ObservationProxy(#{@desc})#observeValueForKeyPath(#{key_path}, ofObject:#{target.inspect.split(" ").first}>, ...)" - p "called", m_desc - end + # m_desc = nil + # if ::RMExtensions.debug? + # m_desc = "~~> ObservationProxy(#{@desc})#observeValueForKeyPath(#{key_path}, ofObject:#{target.inspect.split(" ").first}>, ...)" + # p "called", m_desc + # end action = proc do next if @did_dealloc next if target.nil? key_paths = @targets[target] next if key_paths.nil? blocks = key_paths[key_path] next if blocks.nil? blocks = [] + blocks # get a new array that can be popped - if ::RMExtensions.debug? - p "blocks.size", blocks.size, m_desc - end + # if ::RMExtensions.debug? + # p "blocks.size", blocks.size, m_desc + # end while blk = blocks.pop res = ObservationResponse.new res.context = @weak_object res.value = change[NSKeyValueChangeNewKey] res.old_value = change[NSKeyValueChangeOldKey] @@ -187,18 +185,18 @@ res.kind = change[NSKeyValueChangeKindKey] blk.call(res) end end if NSThread.currentThread.isMainThread - if ::RMExtensions.debug? - p "inline execution", m_desc - end + # if ::RMExtensions.debug? + # p "inline execution", m_desc + # end action.call else - if ::RMExtensions.debug? - p "dispatch execution", m_desc - end + # if ::RMExtensions.debug? + # p "dispatch execution", m_desc + # end rmext_on_main_q(&action) end end def on(event, inContext:context, withBlock:block) @@ -211,10 +209,11 @@ end unless context_event_blocks = context_events.objectForKey(event) context_event_blocks = [] context_events.setObject(context_event_blocks, forKey:event) end + block.weak! context_event_blocks.addObject block end def off(event, inContext:context, withBlock:block) return if event.nil? || block.nil? @@ -229,15 +228,15 @@ def off_all @events.removeAllObjects end def trigger(event, value) - m_desc = nil - if ::RMExtensions.debug? - m_desc = "~~> ObservationProxy(#{@desc})#trigger(#{event}, #{value.inspect.split(" ").first }>)" - p "called", m_desc - end + # m_desc = nil + # if ::RMExtensions.debug? + # m_desc = "~~> ObservationProxy(#{@desc})#trigger(#{event}, #{value.inspect.split(" ").first }>)" + # p "called", m_desc + # end rmext_on_main_q do next if @did_dealloc next if event.nil? event = event.to_s keyEnumerator = @events.keyEnumerator @@ -247,12 +246,12 @@ end while context = contexts.pop if context_events = @events.objectForKey(context) if event_blocks = context_events[event] blocks = [] + event_blocks - if ::RMExtensions.debug? - p "blocks.size", blocks.size, m_desc - end + # if ::RMExtensions.debug? + # p "blocks.size", blocks.size, m_desc + # end while blk = blocks.pop res = EventResponse.new res.context = context res.value = value res.target = @weak_object