// ========================================================================== // Project: SproutCore Costello - Property Observing Library // Copyright: ©2006-2011 Strobe Inc. and contributors. // Portions ©2008-2011 Apple Inc. All rights reserved. // License: Licensed under MIT license (see license.js) // ========================================================================== // ........................................................................ // ObserverSet // /** @namespace This private class is used to store information about observers on a particular key. Note that this object is not observable. You create new instances by calling SC.beget(SC.ObserverSet) ; @private @since SproutCore 1.0 */ SC.ObserverSet = { /** Adds the named target/method observer to the set. The method must be a function, not a string. */ add: function(target, method, context) { var targetGuid = SC.guidFor(target), methodGuid = SC.guidFor(method), targets = this._members, members = this.members, indexes = targets[targetGuid], // get the set of methods index, member; if ( !indexes ) indexes = targets[targetGuid] = { _size: 0 }; index = indexes[methodGuid]; if (index === undefined) { indexes[methodGuid] = members.length; // Increase the size of the indexes for this target so that it can be cleaned up. indexes._size++; member = [target, method, context]; //@if(debug) // If deferred call logging info was specified (i.e., in debug mode when // such logging is enabled), we need to add it to the enqueued target/ // method. member[3] = arguments[3]; //@endif members.push(member); } else { //@if(debug) // If deferred call logging info was specified (i.e., in debug mode when // such logging is enabled), we need to add it to the enqueued target/ // method. var loggingInfo = arguments[3], memberLoggingInfo; if (loggingInfo) { member = members[index]; memberLoggingInfo = member[3]; if (!memberLoggingInfo) { member[3] = [loggingInfo]; } else if (!(memberLoggingInfo instanceof Array)) { member[3] = [memberLoggingInfo, loggingInfo]; } else { memberLoggingInfo.push(loggingInfo); } } //@endif } }, /** removes the named target/method observer from the set. If this is the last method for the named target, then the number of targets will also be reduced. returns YES if the items was removed, NO if it was not found. */ remove: function(target, method) { var targetGuid = SC.guidFor(target), methodGuid = SC.guidFor(method); var indexes = this._members[targetGuid], members = this.members; if( !indexes ) return false; var index = indexes[methodGuid]; if ( index === undefined) return false; if (index !== members.length - 1) { var entry = (members[index] = members[members.length - 1]); this._members[SC.guidFor(entry[0])][SC.guidFor(entry[1])] = index; } // Throw away the last member (it has been moved or is the member we are removing). members.pop(); // Remove the method tracked for the target. delete this._members[targetGuid][methodGuid]; // If there are no more methods tracked on the target, remove the target. if (--this._members[targetGuid]._size === 0) { delete this._members[targetGuid]; } return true; }, /** Invokes the target/method pairs in the receiver. Used by SC.RunLoop Note: does not support context */ invokeMethods: function() { var members = this.members, member; //@if(debug) var shouldLog = SC.LOG_DEFERRED_CALLS, target, method, methodName, loggingInfo, loggingInfos, originatingTarget, originatingMethod, originatingMethodName, originatingStack, j, jLen; //@endif for( var i=0, l=members.length; i