From 9d34f3d4e6c2399f3fcbe7fc84c5d8110ee8b1de Mon Sep 17 00:00:00 2001 From: Ross Boucher Date: Sun, 18 Jan 2009 14:37:49 -0800 Subject: [PATCH] Remove dead code, use just in time setter replacement. Typo fix. Better way to replace methods, courtesy of Francisco. --- Foundation/CPKeyValueObserving.j | 135 ++++++++++--------------------- 1 file changed, 41 insertions(+), 94 deletions(-) diff --git a/Foundation/CPKeyValueObserving.j b/Foundation/CPKeyValueObserving.j index 5c090b9b9..1e9be5e67 100644 --- a/Foundation/CPKeyValueObserving.j +++ b/Foundation/CPKeyValueObserving.j @@ -24,6 +24,7 @@ @import "CPDictionary.j" @import "CPException.j" @import "CPObject.j" +@import "CPSet.j" @implementation CPObject (KeyValueObserving) @@ -54,7 +55,7 @@ [[KVOProxyMap objectForKey:[self hash]] _removeObserver:anObserver forKeyPath:aPath]; } -- (BOOL)automaticallyNotifiesObserversForKey:(CPString)aKey ++ (BOOL)automaticallyNotifiesObserversForKey:(CPString)aKey { return YES; } @@ -89,7 +90,7 @@ var KVOProxyMap = [CPDictionary dictionary]; Class _nativeClass; CPDictionary _changesForKey; CPDictionary _observersForKey; - CPDictionary _replacementMethods; + CPSet _replacedKeys; } + (id)proxyForObject:(CPObject)anObject @@ -101,10 +102,6 @@ var KVOProxyMap = [CPDictionary dictionary]; proxy = [[self alloc] initWithTarget:anObject]; - //[proxy _replaceSetters]; - - //anObject.isa = proxy.isa; - [proxy _replaceClass]; [KVOProxyMap setObject:proxy forKey:[anObject hash]]; @@ -118,55 +115,13 @@ var KVOProxyMap = [CPDictionary dictionary]; _targetObject = aTarget; _nativeClass = [aTarget class]; - _replacementMethods = [CPDictionary dictionary]; _observersForKey = [CPDictionary dictionary]; _changesForKey = [CPDictionary dictionary]; + _replacedKeys = [CPSet set]; return self; } -- (void)_replaceSetters -{ - var currentClass = [_targetObject class]; - - while (currentClass && currentClass != currentClass.super_class) - { - var methodList = currentClass.method_list, - count = methodList.length; - - for (var i=0; i 2 || !([selector hasPrefix:@"set"] || [selector hasPrefix:@"_set"])) - return nil; - - var keyIndex = selector.indexOf("set") + "set".length, - colonIndex = selector.indexOf(":"); - - return selector.charAt(keyIndex).toLowerCase() + selector.substring(keyIndex+1, colonIndex); -} - -var _kvoMethodForMethod = function _kvoMethodForMethod(theObject, theMethod) -{ - var methodName = theMethod.name, - methodImplementation = theMethod.method_imp, - setterKey = _kvoKeyForSetter(methodName); - - if (setterKey && objj_msgSend(theObject, @selector(automaticallyNotifiesObserversForKey:), setterKey)) - { - var newMethodImp = function(self) - { - [self willChangeValueForKey:setterKey]; - methodImplementation.apply(self, arguments); - [self didChangeValueForKey:setterKey]; - } - - return newMethodImp; + return function(self) + { + [self willChangeValueForKey:theKey]; + theMethod.method_imp.apply(self, arguments); + [self didChangeValueForKey:theKey]; } - - return nil; } @implementation CPArray (KeyValueObserving)