2017-03-21 11:15:18 -05:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-02 11:08:55 -05:00
|
|
|
const log = require('../log.js');
|
|
|
|
const logLevels = require('../enums/logLevels.js');
|
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Manages one animation at a time
|
|
|
|
* @param options
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-03-21 11:15:18 -05:00
|
|
|
const AnimationQueue = function(options) {
|
|
|
|
this.options = options;
|
|
|
|
this.queue = [];
|
|
|
|
this.running = false;
|
2017-08-24 02:51:02 -05:00
|
|
|
};
|
2017-03-21 11:15:18 -05:00
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Pushes each animation to a queue
|
|
|
|
* @param object
|
|
|
|
*/
|
2017-03-21 11:15:18 -05:00
|
|
|
AnimationQueue.prototype.push = function(object) {
|
|
|
|
if (this.running) {
|
|
|
|
this.queue.push(object);
|
|
|
|
} else {
|
|
|
|
this.running = true;
|
2017-04-06 12:07:58 -05:00
|
|
|
setTimeout(this.animate.bind(this, object), 0);
|
2017-03-21 11:15:18 -05:00
|
|
|
}
|
2017-08-24 02:51:02 -05:00
|
|
|
};
|
2017-03-21 11:15:18 -05:00
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Animates an animation that is part of the queue
|
|
|
|
* @param object
|
|
|
|
*/
|
2017-03-21 11:15:18 -05:00
|
|
|
AnimationQueue.prototype.animate = function(object) {
|
|
|
|
object.func.apply(null, object.args)
|
|
|
|
.then(function() {
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
// Run next animation
|
|
|
|
this.animate.call(this, this.queue.shift());
|
|
|
|
} else {
|
|
|
|
this.running = false;
|
|
|
|
}
|
|
|
|
}.bind(this))
|
|
|
|
.catch(function(err) {
|
2017-06-02 11:08:55 -05:00
|
|
|
log.send(logLevels.ERROR, 'animationQueue: encountered an error: ' + err +
|
|
|
|
' with stack trace:' + err.stack);
|
2017-03-21 11:15:18 -05:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error('animation queue encountered an error: ' + err +
|
|
|
|
' with stack trace:' + err.stack);
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
})
|
2017-08-24 02:51:02 -05:00
|
|
|
};
|
2017-03-21 11:15:18 -05:00
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Clears the queue
|
|
|
|
*/
|
2017-03-21 11:15:18 -05:00
|
|
|
AnimationQueue.prototype.clear = function() {
|
|
|
|
this.queue = [];
|
2017-08-24 02:51:02 -05:00
|
|
|
};
|
2017-03-21 11:15:18 -05:00
|
|
|
|
|
|
|
module.exports = AnimationQueue;
|