Skip to content

Commit

Permalink
Get default duration if none set in arguments Fixes #874
Browse files Browse the repository at this point in the history
  • Loading branch information
Rycochet committed Aug 2, 2020
1 parent 623c914 commit e55cf8b
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 25 deletions.
11 changes: 6 additions & 5 deletions packages/velocity/src/velocityFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ export function Velocity(this: VelocityElements | void, ...argsList: any[]): Vel
}
} else if (!syntacticSugar) {
// Expand any direct options if possible.
const validDuration = validateDuration(args[argumentIndex], true);
let offset = 0;

hasValidDuration = validDuration !== undefined;
if (validDuration !== undefined) {
options.duration = validateDuration(args[argumentIndex], true);
if (options.duration === undefined) {
options.duration = defaults.duration;
} else {
hasValidDuration = true;
offset++;
options.duration = validDuration;
}
if (!isFunction(args[argumentIndex + offset])) {
// Despite coming before Complete, we can't pass a fn easing
Expand All @@ -360,7 +361,7 @@ export function Velocity(this: VelocityElements | void, ...argsList: any[]): Vel
throw new Error("VelocityJS: Cannot reverse a queue:false animation.");
}

if (maybeSequence && !hasValidDuration && maybeSequence.duration) {
if (!hasValidDuration && maybeSequence && maybeSequence.duration) {
options.duration = maybeSequence.duration;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/velocity/test/src/2_Option/Option Delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ QUnit.test("Delay", (assert) => {
duration: defaultOptions.duration,
delay: testDelay,
begin(elements, activeCall) {
assert.close(getNow() - start, (testDelay * 2) + (defaultOptions.duration as number), 32, "Queued delays start after the correct delay.");
assert.close(getNow() - start, (testDelay * 2) + (defaultOptions.duration as number), 32, "Chained delays start after the correct delay.");

done();
},
Expand Down
64 changes: 64 additions & 0 deletions packages/velocity/test/src/2_Option/Option Duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* velocity-animate (C) 2014-2018 Julian Shapiro.
*
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

import "qunit";

import Velocity from "velocity-animate";
import {asyncTests, defaultOptions, defaultProperties, getNow, getTarget} from "../utilities";
import "./_module";

QUnit.test("Duration", (assert) => {
const testDuration = Velocity.defaults.duration as number;

asyncTests(assert, 1, (done) => {
const start = getNow();

Velocity(getTarget(), defaultProperties, {
duration: testDuration,
complete(elements, activeCall) {
const time = getNow() - start;

assert.close(time, testDuration, 32, `Calls run for the correct duration (~${Math.floor(time)}ms / ${testDuration}ms).`);

done();
},
});
});

asyncTests(assert, 1, (done) => {
const start = getNow();

Velocity(getTarget(), {width: ["200px", "500px"]}, {
duration: testDuration,
})
.velocity({width: ["500px", "200px"]}, {
duration: testDuration,
complete(elements, activeCall) {
const time = getNow() - start;

assert.close(getNow() - start, testDuration * 2, 32, `Chained durations run for the correct duration (~${Math.floor(time)}ms / ${testDuration * 2}ms).`);

done();
},
});
});

asyncTests(assert, 1, (done) => {
const start = getNow();

Velocity(getTarget(), {width: ["200px", "500px"]})
.velocity({width: ["500px", "200px"]})
.then(() => {
const time = getNow() - start;

assert.close(getNow() - start, testDuration * 2, 32, `Chained durations with defaults run for the correct duration (~${Math.floor(time)}ms / ${testDuration * 2}ms).`);

done();
});
});

assert.expect(asyncTests());
});
1 change: 1 addition & 0 deletions packages/velocity/test/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import "./2_Option/_module";
import "./2_Option/Option Begin";
import "./2_Option/Option Complete";
import "./2_Option/Option Delay";
import "./2_Option/Option Duration";
import "./2_Option/Option Easing";
import "./2_Option/Option Fps Limit";
import "./2_Option/Option Loop";
Expand Down
39 changes: 38 additions & 1 deletion packages/velocity/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 packages/velocity/test/test.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions packages/velocity/velocity.es5.js

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

2 changes: 1 addition & 1 deletion packages/velocity/velocity.es5.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions packages/velocity/velocity.js

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

2 changes: 1 addition & 1 deletion packages/velocity/velocity.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions packages/velocity/velocity.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,21 +1134,21 @@ j.mobileHA=!0),E||(null!=S.display&&(b.display=S.display,console.error('Deprecat
// TODO: Allow functional options for different options per element
var Y=P(S.begin),X=F(S.complete),K=function(e){if(r(e))return e;null!=e&&console.warn("VelocityJS: Trying to set 'progress' to an invalid value:",e)}(S.progress),ee=D(S.sync);null!=Y&&(j.begin=Y),null!=X&&(j.complete=X),null!=K&&(j.progress=K),null!=ee&&(B=ee)}else if(!m){
// Expand any direct options if possible.
var te=R(p[h],!0),ne=0;if(I=void 0!==te,void 0!==te&&(ne++,j.duration=te),!r(p[h+ne])){
var te=0;if(j.duration=R(p[h],!0),void 0===j.duration?j.duration=v.duration:(I=!0,te++),!r(p[h+te])){
// Despite coming before Complete, we can't pass a fn easing
var re=W(p[h+ne],d(j&&R(j.duration),v.duration),!0);void 0!==re&&(ne++,j.easing=re)}var ie=F(p[h+ne],!0);void 0!==ie&&(j.complete=ie),j.loop=v.loop,j.repeat=j.repeatAgain=v.repeat}if(E&&!1===j.queue)throw new Error("VelocityJS: Cannot reverse a queue:false animation.");M&&!I&&M.duration&&(j.duration=M.duration);
var ne=W(p[h+te],d(j&&R(j.duration),v.duration),!0);void 0!==ne&&(te++,j.easing=ne)}var re=F(p[h+te],!0);void 0!==re&&(j.complete=re),j.loop=v.loop,j.repeat=j.repeatAgain=v.repeat}if(E&&!1===j.queue)throw new Error("VelocityJS: Cannot reverse a queue:false animation.");!I&&M&&M.duration&&(j.duration=M.duration);
// When a set of elements is targeted by a Velocity call, the set is
// broken up and each element has the current Velocity call individually
// queued onto it. In this way, each element's existing queue is
// respected; some elements may already be animating and accordingly
// should not have this current Velocity call triggered immediately
// unless the sync:true option is used.
var oe={options:j,elements:w,_prev:void 0,_next:void 0,_flags:B?32/* SYNC */:0,percentComplete:0,ellapsedTime:0,timeStart:0};x=[];var ae=!0,le=!1,se=void 0;try{for(var ue,ce=w[Symbol.iterator]();!(ae=(ue=ce.next()).done);ae=!0){var fe=ue.value,de=0;if(i(fe)){
var ie={options:j,elements:w,_prev:void 0,_next:void 0,_flags:B?32/* SYNC */:0,percentComplete:0,ellapsedTime:0,timeStart:0};x=[];var oe=!0,ae=!1,le=void 0;try{for(var se,ue=w[Symbol.iterator]();!(oe=(se=ue.next()).done);oe=!0){var ce=se.value,fe=0;if(i(ce)){
// TODO: This needs to check for valid animation targets, not just Elements
if(E){var ve=he(fe).lastAnimationList[j.queue];if(!(b=ve&&ve.tweens)){console.error("VelocityJS: Attempting to reverse an animation on an element with no previous animation:",fe);continue}de|=64/* REVERSE */&~(64/* REVERSE */&ve._flags)}var pe=Object.assign({},oe,{element:fe,_flags:oe._flags|de});j._total++,x.push(pe),M?Yt(pe,M):E?
if(E){var de=he(ce).lastAnimationList[j.queue];if(!(b=de&&de.tweens)){console.error("VelocityJS: Attempting to reverse an animation on an element with no previous animation:",ce);continue}fe|=64/* REVERSE */&~(64/* REVERSE */&de._flags)}var ve=Object.assign({},ie,{element:ce,_flags:ie._flags|fe});j._total++,x.push(ve),M?Yt(ve,M):E?
// In this case we're using the previous animation, so
// it will be expanded correctly when that one runs.
pe.tweens=b:(pe.tweens=Object.create(null),Qe(pe,b)),ke(fe,pe,j.queue)}}}catch(e){le=!0,se=e}finally{try{!ae&&ce.return&&ce.return()}finally{if(le)throw se}}!1===Se.isTicking&&
ve.tweens=b:(ve.tweens=Object.create(null),Qe(ve,b)),ke(ce,ve,j.queue)}}}catch(e){ae=!0,le=e}finally{try{!oe&&ue.return&&ue.return()}finally{if(ae)throw le}}!1===Se.isTicking&&
// If the animation tick isn't running, start it. (Velocity shuts it
// off when there are no active calls to process.)
ft(!1),x&&f(w.velocity,"animations",x)}
Expand Down

0 comments on commit e55cf8b

Please sign in to comment.