Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Promises support #143

Merged
merged 2 commits into from
Jul 8, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions jquery.velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Velocity's structure:
loop: false,
delay: false,
mobileHA: true,
promise: true,
/* Set to false to prevent property values from being cached between consecutive Velocity-initiated chain calls. */
_cacheValues: true
},
Expand Down Expand Up @@ -1436,6 +1437,9 @@ Velocity's structure:
CSS.Hooks.register();
CSS.Normalizations.register();

/* Attempt to use ES6 Promises as default. */
Velocity.Promise = Velocity.Promise || window.Promise;

/**********************
Velocity.animate
**********************/
Expand Down Expand Up @@ -1499,6 +1503,23 @@ Velocity's structure:
var elementsLength = (Type.isArray(elements) || Type.isNodeList(elements)) ? elements.length : 1,
elementsIndex = 0;

/**************
Promises
*************/
/* turning this scope into a "deferred" */
var promise, resolver, rejecter;
if (Velocity.Promise && (options.promise || Velocity.defaults.promise)) {
var originalComplete = options.complete;
promise = new Velocity.Promise(function (resolve, reject) {
resolver = resolve;
rejecter = reject;
});
options.complete = function (value) {
originalComplete && originalComplete.call(value, value);
resolver(value);
};
}

/***************************
Argument Overloading
***************************/
Expand Down Expand Up @@ -1598,6 +1619,11 @@ Velocity's structure:
});

/* Since we're stopping, do not proceed with Queueing. */
if (promise) {
/* Just resolve the promise with chained context. */
resolver(getChain());
return promise;
}
return getChain();

default:
Expand Down Expand Up @@ -1647,9 +1673,15 @@ Velocity's structure:
/* Since the animation logic resides within the sequence's own code, abort the remainder of this call.
(The performance overhead up to this point is virtually non-existant.) */
/* Note: The jQuery call chain is kept intact by returning the complete element set. */
return elementsWrapped || elementsOriginal;
return promise || elementsWrapped || elementsOriginal;
} else {
console.log("First argument was not a property map, a known action, or a registered sequence. Aborting.")
var errorMsg = "First argument was not a property map, a known action, or a registered sequence. Aborting.";
console.log(errorMsg)

if (promise) {
rejecter(new Error(errorMsg));
return promise;
}

return getChain();
}
Expand Down Expand Up @@ -2668,7 +2700,7 @@ Velocity's structure:
***************/

/* Return the elements back to the call chain, with wrapped elements taking precedence in case Velocity was called via the $.fn. extension. */
return getChain();
return promise || getChain();
};

/*****************************
Expand Down Expand Up @@ -3018,6 +3050,7 @@ Velocity's structure:
var framework = window.jQuery || window.Zepto;

if (framework) {
Velocity.defaults.promise = false;
/* Assign the object function to Velocity's animate() method. */
framework.fn.velocity = Velocity.animate;

Expand Down Expand Up @@ -3200,4 +3233,4 @@ Velocity's structure:

/* The CSS spec mandates that the translateX/Y/Z transforms are %-relative to the element itself -- not its parent.
Velocity, however, doesn't make this distinction. Thus, converting to or from the % unit with these subproperties
will produce an inaccurate conversion value. The same issue exists with the cx/cy attributes of SVG circles and ellipses. */
will produce an inaccurate conversion value. The same issue exists with the cx/cy attributes of SVG circles and ellipses. */