Skip to content

Commit

Permalink
Expose Velocity().promise for use in Promise.all() etc
Browse files Browse the repository at this point in the history
Fixes #868
  • Loading branch information
Rycochet committed May 28, 2018
1 parent 46aa78e commit c90e431
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 38 deletions.
17 changes: 7 additions & 10 deletions src/velocityFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// Typedefs
import {
AnimationCall, AnimationFlags, HTMLorSVGElement, Properties, StrictVelocityOptions,
VelocityElements, VelocityObjectArgs, VelocityOptions, VelocityPromise, VelocityProperty,
VelocityResult,
VelocityElements, VelocityExtended, VelocityObjectArgs, VelocityOptions, VelocityPromise,
VelocityProperty, VelocityResult,
} from "./../velocity.d";

// Project
Expand Down Expand Up @@ -174,22 +174,19 @@ export function Velocity(this: VelocityElements | void, ...argsList: any[]): Vel
// Due to being an async call, they should be back to "normal"
// before the <code>.then()</code> function gets called.
resolver = (result: VelocityResult) => {
if (isVelocityResult(result)) {
const then = result && result.then;
if (isVelocityResult(result) && result.then) {
const then = result.then;

if (then) {
result.then = undefined; // Preserving enumeration etc
}
result.then = undefined; // Preserving enumeration etc
resolve(result);
if (then) {
result.then = then;
}
result.then = then;
} else {
resolve(result);
}
};
});
if (elements) {
defineProperty(elements, "promise", promise);
defineProperty(elements, "then", promise.then.bind(promise));
defineProperty(elements, "catch", promise.catch.bind(promise));
if ((promise as any).finally) {
Expand Down
14 changes: 12 additions & 2 deletions test/src/4_Feature/Feature Promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {defaultOptions, defaultProperties, getNow, getTarget} from "../utilities
import "./_module";

QUnit.test("Promises", (assert) => {
const done = assert.async(9),
const done = assert.async(10),
start = getNow();
let result: VelocityResult;

assert.expect(9);
assert.expect(10);

/**********************
Invalid Arguments
Expand Down Expand Up @@ -97,4 +97,14 @@ QUnit.test("Promises", (assert) => {
})
.then(done);
anim.velocity("stop");

Promise
.all([
Velocity(getTarget(), defaultProperties, defaultOptions).promise,
Velocity(getTarget(), defaultProperties, defaultOptions).promise,
])
.then(() => {
assert.ok(true, "Promise.all fulfilled when all animations have finished.");
})
.then(done);
});
50 changes: 48 additions & 2 deletions test/test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/test.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit c90e431

Please sign in to comment.