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