Skip to content

Commit

Permalink
replace private dispatch with public Y.dispatchIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
John Lindal committed May 15, 2012
1 parent 1238804 commit bfbcb32
Showing 1 changed file with 63 additions and 50 deletions.
113 changes: 63 additions & 50 deletions src/oop/js/oop.js
Expand Up @@ -13,57 +13,70 @@ var L = Y.Lang,
hasOwn = OP.hasOwnProperty, hasOwn = OP.hasOwnProperty,
toString = OP.toString; toString = OP.toString;


function dispatch(o, f, c, proto, action) { /**
if (o && o[action] && o !== Y) { * Executes the named action for a collection by dispatching the call to
return o[action].call(o, f, c); * either the object, Y.Object, or Y.Array, as appropriate.
} else { *
switch (A.test(o)) { * @method dispatchIterator
case 1: * @param {String} name of the function to call
return A[action](o, f, c); * @param {Array|Object} array or object to iterate
case 2: * @param {Mixed} ... arguments to pass to the function
return A[action](Y.Array(o, 0, true), f, c); * @return {Mixed} whatever the function returns
default: */
return Y.Object[action](o, f, c, proto); Y.dispatchIterator = function(action, o) {
} var args = A(arguments, 1, true);
switch (A.test(o)) {
case 1: // array
return A[action].apply(null, args);
case 2: // array-like
args[0] = A(o, 0, true);
return A[action].apply(null, args);
default: // something else
if (o && o[action] && o !== Y) {
args.shift();
return o[action].apply(o, args);
} else {
return Y.Object[action].apply(null, args);
}
} }
} };


/** /**
Augments the _receiver_ with prototype properties from the _supplier_. The * Augments the _receiver_ with prototype properties from the _supplier_. The
receiver may be a constructor function or an object. The supplier must be a * receiver may be a constructor function or an object. The supplier must be a
constructor function. * constructor function.
*
If the _receiver_ is an object, then the _supplier_ constructor will be called * If the _receiver_ is an object, then the _supplier_ constructor will be called
immediately after _receiver_ is augmented, with _receiver_ as the `this` object. * immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
*
If the _receiver_ is a constructor function, then all prototype methods of * If the _receiver_ is a constructor function, then all prototype methods of
_supplier_ that are copied to _receiver_ will be sequestered, and the * _supplier_ that are copied to _receiver_ will be sequestered, and the
_supplier_ constructor will not be called immediately. The first time any * _supplier_ constructor will not be called immediately. The first time any
sequestered method is called on the _receiver_'s prototype, all sequestered * sequestered method is called on the _receiver_'s prototype, all sequestered
methods will be immediately copied to the _receiver_'s prototype, the * methods will be immediately copied to the _receiver_'s prototype, the
_supplier_'s constructor will be executed, and finally the newly unsequestered * _supplier_'s constructor will be executed, and finally the newly unsequestered
method that was called will be executed. * method that was called will be executed.
*
This sequestering logic sounds like a bunch of complicated voodoo, but it makes * This sequestering logic sounds like a bunch of complicated voodoo, but it makes
it cheap to perform frequent augmentation by ensuring that suppliers' * it cheap to perform frequent augmentation by ensuring that suppliers'
constructors are only called if a supplied method is actually used. If none of * constructors are only called if a supplied method is actually used. If none of
the supplied methods is ever used, then there's no need to take the performance * the supplied methods is ever used, then there's no need to take the performance
hit of calling the _supplier_'s constructor. * hit of calling the _supplier_'s constructor.
*
@method augment * @method augment
@param {Function|Object} receiver Object or function to be augmented. * @param {Function|Object} receiver Object or function to be augmented.
@param {Function} supplier Function that supplies the prototype properties with * @param {Function} supplier Function that supplies the prototype properties with
which to augment the _receiver_. * which to augment the _receiver_.
@param {Boolean} [overwrite=false] If `true`, properties already on the receiver * @param {Boolean} [overwrite=false] If `true`, properties already on the receiver
will be overwritten if found on the supplier's prototype. * will be overwritten if found on the supplier's prototype.
@param {String[]} [whitelist] An array of property names. If specified, * @param {String[]} [whitelist] An array of property names. If specified,
only the whitelisted prototype properties will be applied to the receiver, and * only the whitelisted prototype properties will be applied to the receiver, and
all others will be ignored. * all others will be ignored.
@param {Array|any} [args] Argument or array of arguments to pass to the * @param {Array|any} [args] Argument or array of arguments to pass to the
supplier's constructor when initializing. * supplier's constructor when initializing.
@return {Function} Augmented object. * @return {Function} Augmented object.
@for YUI * @for YUI
**/ */
Y.augment = function (receiver, supplier, overwrite, whitelist, args) { Y.augment = function (receiver, supplier, overwrite, whitelist, args) {
var rProto = receiver.prototype, var rProto = receiver.prototype,
sequester = rProto && supplier, sequester = rProto && supplier,
Expand Down Expand Up @@ -208,7 +221,7 @@ Y.extend = function(r, s, px, sx) {
* @return {YUI} the YUI instance. * @return {YUI} the YUI instance.
*/ */
Y.each = function(o, f, c, proto) { Y.each = function(o, f, c, proto) {
return dispatch(o, f, c, proto, 'each'); return Y.dispatchIterator('each', o, f, c, proto);
}; };


/** /**
Expand All @@ -227,7 +240,7 @@ Y.each = function(o, f, c, proto) {
* false otherwise. * false otherwise.
*/ */
Y.some = function(o, f, c, proto) { Y.some = function(o, f, c, proto) {
return dispatch(o, f, c, proto, 'some'); return Y.dispatchIterator('some', o, f, c, proto);
}; };


/** /**
Expand Down

2 comments on commit bfbcb32

@tilomitra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jafl, I'll look into this and get back to you. It's a pretty old pull request so I'll make sure it's still good to pull in.

@jafl
Copy link
Owner

@jafl jafl commented on bfbcb32 Jan 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.