2019-10-11 21:53:41 +00:00
|
|
|
'use strict';
|
2019-10-11 22:15:32 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2019-10-11 21:53:41 +00:00
|
|
|
const uuid = require('uuid').v4;
|
|
|
|
const log = require('../log')('seq');
|
2019-10-11 22:24:41 +00:00
|
|
|
require("../delay");
|
2017-12-20 23:40:51 +00:00
|
|
|
/** Object sequence features */
|
2019-10-11 22:15:32 +00:00
|
|
|
class Sequence {
|
|
|
|
constructor() {
|
|
|
|
this._state = {
|
|
|
|
arr: [],
|
|
|
|
active: false,
|
|
|
|
paused: false,
|
|
|
|
frame: false,
|
|
|
|
delay: false,
|
|
|
|
count: 0,
|
|
|
|
stop: null
|
|
|
|
};
|
|
|
|
this._loop = {
|
|
|
|
arr: [],
|
|
|
|
count: 0,
|
|
|
|
max: 0
|
|
|
|
};
|
|
|
|
this.stop = function () {
|
|
|
|
this._state.active = false;
|
|
|
|
this._state.count = 0;
|
|
|
|
this._state.arr = [];
|
|
|
|
this._loop.count = 0;
|
|
|
|
this._loop.max = 0;
|
|
|
|
this._loop.arr = [];
|
|
|
|
if (this._state.stop)
|
|
|
|
this._state.stop();
|
|
|
|
this._state.stop = null;
|
|
|
|
};
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:24:41 +00:00
|
|
|
start(options, cb) {
|
2019-10-11 22:15:32 +00:00
|
|
|
if (this._state.active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._state.active = true;
|
|
|
|
this._state.count = 0;
|
|
|
|
if (options.arr) {
|
|
|
|
this._state.arr = options.arr;
|
|
|
|
}
|
|
|
|
if (options.loop) {
|
|
|
|
this._loop.arr = options.loop;
|
|
|
|
this._loop.count = 0;
|
|
|
|
}
|
|
|
|
if (options.maxLoop) {
|
|
|
|
this._loop.max = options.maxLoop;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._loop.max = 0;
|
|
|
|
}
|
|
|
|
this._state.stop = cb;
|
|
|
|
this.step();
|
|
|
|
this._state.id = uuid();
|
|
|
|
return this._state.id;
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
setStop() {
|
|
|
|
this._state.active = false;
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
pause() {
|
|
|
|
this._state.paused = true;
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
resume() {
|
|
|
|
this._state.paused = false;
|
|
|
|
this.step();
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
step() {
|
|
|
|
if (this._state.active && !this._state.paused) {
|
|
|
|
if (this._state.arr.length > 0) {
|
|
|
|
if (this._state.count > this._state.arr.length - 1) {
|
|
|
|
return this.stop();
|
|
|
|
}
|
|
|
|
log.info('step', { count: this._state.count, id: this._state.id });
|
|
|
|
return this._state.arr[this._state.count](() => {
|
|
|
|
this._state.count++;
|
|
|
|
this.step();
|
|
|
|
});
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
else if (this._loop.arr.length > 0) {
|
|
|
|
if (this._state.count > this._loop.arr.length - 1) {
|
|
|
|
this._state.count = 0;
|
|
|
|
this._loop.count++;
|
|
|
|
}
|
|
|
|
if (this._loop.max > 0 && this._loop.count > this._loop.max) {
|
|
|
|
return this.stop();
|
|
|
|
}
|
|
|
|
log.info('step', { count: this._state.count, id: this._state.id });
|
|
|
|
return this._loop.arr[this._state.count](() => {
|
|
|
|
this._state.count++;
|
|
|
|
this.step();
|
|
|
|
});
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
else {
|
|
|
|
return this.stop();
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
else if (this._state.paused) {
|
|
|
|
log.info('step', 'Sequence paused', { loop: this._loop.count, count: this._state.count });
|
|
|
|
}
|
|
|
|
else if (!this._state.active) {
|
|
|
|
log.info('step', 'Sequence stopped', { loop: this._loop.count, count: this._state.count });
|
2019-10-11 21:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 22:15:32 +00:00
|
|
|
}
|
|
|
|
module.exports = new Sequence();
|
2019-10-11 21:53:41 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|