Skip to content

Commit

Permalink
Fixes the pause flag logic as it was using the global (shared) state,…
Browse files Browse the repository at this point in the history
… use Symbol for the paused flag , and replace function with proper getter sasha240100#21
  • Loading branch information
hirako2000 committed Aug 8, 2018
1 parent 827cc0e commit 8f40dc1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion examples/visual-box.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h5 data-state style="padding-top: 20px;">state: started</h5>

function toggle() {
if (!between) return;
if (between.isPaused()) {
if (between.isPaused) {
between.start();
} else {
between.pause();
Expand Down
20 changes: 11 additions & 9 deletions src/between.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion types/between-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8f40dc1

Please sign in to comment.