2017-11-23 01:26:10 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
const log = require('../log')('seq')
|
|
|
|
|
|
|
|
/** Object sequence features */
|
|
|
|
const sequence = {};
|
|
|
|
|
|
|
|
sequence._state = {
|
|
|
|
arr : [],
|
2017-11-23 01:26:10 +00:00
|
|
|
active : false,
|
2017-12-20 23:40:51 +00:00
|
|
|
paused : false,
|
2017-11-23 01:26:10 +00:00
|
|
|
frame: false,
|
2017-12-20 23:40:51 +00:00
|
|
|
delay : false,
|
|
|
|
count : 0,
|
|
|
|
stop : null
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence._loop = {
|
|
|
|
arr : [],
|
|
|
|
count : 0,
|
|
|
|
max : 0
|
2017-11-23 01:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sequence.start = function (options, cb) {
|
2017-12-20 23:40:51 +00:00
|
|
|
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
|
|
|
|
}
|
2017-12-21 02:24:51 +00:00
|
|
|
sequence._state.stop = cb
|
2017-12-20 23:40:51 +00:00
|
|
|
sequence.step()
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:49:26 +00:00
|
|
|
sequence.setStop = function (cb) {
|
2017-12-21 02:24:51 +00:00
|
|
|
sequence._state.active = false
|
2017-12-21 02:49:26 +00:00
|
|
|
sequence._state.input = cb
|
2017-12-21 02:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sequence.stop = function () {
|
2017-12-20 23:40:51 +00:00
|
|
|
sequence._state.active = false
|
|
|
|
sequence._state.count = 0
|
2017-12-21 02:26:59 +00:00
|
|
|
sequence._state.arr = []
|
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
sequence._loop.count = 0
|
2017-12-21 02:26:59 +00:00
|
|
|
sequence._loop.max = 0
|
|
|
|
sequence._loop.arr = []
|
|
|
|
|
2017-12-21 02:24:51 +00:00
|
|
|
if (sequence._state.stop) sequence._state.stop()
|
2017-12-21 02:49:26 +00:00
|
|
|
if (sequence._state.input) sequence._state.input()
|
2017-12-21 02:24:51 +00:00
|
|
|
sequence._state.stop = null
|
2017-12-21 02:49:26 +00:00
|
|
|
sequence._state.input = null
|
2017-12-20 23:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sequence.pause = function () {
|
|
|
|
sequence._state.paused = true
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence.resume = function () {
|
|
|
|
sequence._state.paused = false
|
|
|
|
sequence.step()
|
2017-11-23 01:26:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
sequence.step = function () {
|
|
|
|
if (sequence._state.active && !sequence._state.paused) {
|
2017-12-21 02:24:51 +00:00
|
|
|
if (sequence._state.arr.length > 0) {
|
|
|
|
if (sequence._state.count > sequence._state.arr.length) {
|
|
|
|
return sequence.stop()
|
|
|
|
}
|
2017-12-21 02:49:26 +00:00
|
|
|
log.info('step')
|
2017-12-20 23:40:51 +00:00
|
|
|
return sequence._state.arr[sequence._state.count](() => {
|
|
|
|
sequence._state.count++
|
|
|
|
sequence.step()
|
|
|
|
})
|
2017-12-21 02:24:51 +00:00
|
|
|
} else if (sequence._loop.arr.length > 0) {
|
2017-12-20 23:40:51 +00:00
|
|
|
if (sequence._state.count > sequence._loop.arr.length) {
|
|
|
|
sequence._state.count = 0
|
|
|
|
sequence._loop.count++
|
|
|
|
}
|
2017-12-21 02:49:26 +00:00
|
|
|
if (sequence._loop.max > 0 && sequence._loop.count > sequence._loop.max) {
|
2017-12-20 23:40:51 +00:00
|
|
|
return sequence.stop()
|
|
|
|
}
|
2017-12-21 02:49:26 +00:00
|
|
|
log.info()
|
2017-12-20 23:40:51 +00:00
|
|
|
return sequence._loop.arr[sequence._state.count](() => {
|
|
|
|
sequence._state.count++
|
2017-12-21 01:53:21 +00:00
|
|
|
sequence.step()
|
2017-12-20 23:40:51 +00:00
|
|
|
})
|
|
|
|
} else{
|
|
|
|
return sequence.stop()
|
|
|
|
}
|
|
|
|
} else if (sequence._state.paused) {
|
2017-12-21 02:49:26 +00:00
|
|
|
log.info('step', 'Sequence paused', { loop : sequence._loop.count, count : sequence._state.count })
|
2017-12-20 23:40:51 +00:00
|
|
|
} else if (!sequence._state.active) {
|
2017-12-21 02:49:26 +00:00
|
|
|
log.info('step', 'Sequence stopped', { loop : sequence._loop.count, count : sequence._state.count })
|
2017-12-20 23:40:51 +00:00
|
|
|
}
|
2017-11-23 01:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = sequence
|