From 8f40dc1437744b571af86d9afbe86fc8f9e075ba Mon Sep 17 00:00:00 2001 From: hirako2000 Date: Wed, 8 Aug 2018 17:06:27 +0700 Subject: [PATCH] Fixes the pause flag logic as it was using the global (shared) state, use Symbol for the paused flag , and replace function with proper getter #21 --- README.md | 6 +++--- examples/visual-box.html | 2 +- src/between.js | 20 +++++++++++--------- types/between-tests.ts | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7ce9744..29dd65a 100644 --- a/README.md +++ b/README.md @@ -85,15 +85,15 @@ new Between( [Number|Object|Array] to ) -// Chainable Methods +// Methods .time([Number] duration) // Set duration .loop([String] mode, [?Number] repeatTimes) // Set loop mode, if "repeatTimes" is falsy, treats as "endless" .easing([Function] easing) // Set easing function .on([String] eventName, [Function] callback) // Add event listener .pause() // Pauses -// Util Methods - .isPaused() // returns true if paused +// Getters + .isPaused // returns true if paused ``` diff --git a/examples/visual-box.html b/examples/visual-box.html index e9706b4..05bb51f 100644 --- a/examples/visual-box.html +++ b/examples/visual-box.html @@ -57,7 +57,7 @@
state: started
function toggle() { if (!between) return; - if (between.isPaused()) { + if (between.isPaused) { between.start(); } else { between.pause(); diff --git a/src/between.js b/src/between.js index 6d41855..7fa0b16 100644 --- a/src/between.js +++ b/src/between.js @@ -9,16 +9,16 @@ const _betweens = []; const SYMBOL_TYPE = Symbol('type'); const SYMBOL_START_TIME = Symbol('start_time'); const SYMBOL_COMPLETED = Symbol('completed'); +const SYMBOL_PAUSED = Symbol('paused'); const _requestAnimationFrame = ( // polyfill requestAnimationFrame || raf ); -let _prevTime = Date.now(), _time, _delta, _paused; +let _prevTime = Date.now(), _time, _delta; (function _update() { _requestAnimationFrame(_update); - if (_paused) return; _time = Date.now(); _delta = _time - _prevTime; @@ -74,7 +74,8 @@ export default class Between extends Events { ), [SYMBOL_COMPLETED]: false, [SYMBOL_TYPE]: type, - [SYMBOL_START_TIME]: Date.now() + [SYMBOL_START_TIME]: Date.now(), + [SYMBOL_PAUSED]: false }); switch (this[SYMBOL_TYPE]) { @@ -130,20 +131,18 @@ export default class Between extends Events { } pause() { - _paused = true; + this[SYMBOL_PAUSED] = true; this.emit('pause', this.value, this, _delta); return this; } - isPaused() { - return _paused; + get isPaused() { + return this[SYMBOL_PAUSED]; } start() { + this[SYMBOL_PAUSED] = false; this.emit('start', this.value, this, _delta); - _paused = false; - _delta = 0; - _prevTime = Date.now(); return this; } @@ -206,6 +205,9 @@ export default class Between extends Events { const {_updateValue} = this; return (delta, time) => { + if (this[SYMBOL_PAUSED]) + return; + if (this.localTime === 0) this.emit('start', this.value, this); diff --git a/types/between-tests.ts b/types/between-tests.ts index 0a74dd4..f5b6508 100644 --- a/types/between-tests.ts +++ b/types/between-tests.ts @@ -22,7 +22,7 @@ between.on('complete', (value) => { value }); // Test pause control between.pause(); between.start(); -between.isPaused(); +between.isPaused; // Test chained between.time(1000)