From d90dfc4a6c5063b059779242b80fd44895480b3e Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Fri, 11 Oct 2019 18:15:32 -0400 Subject: [PATCH] Sequencer converted into a singleton class. --- lib/sequence/index.js | 188 ++++++++++++++++++----------------- lib/sequence/index.js.map | 2 +- src/global.d.ts | 1 + src/sequence/index.ts | 201 +++++++++++++++++++------------------- tsconfig.json | 3 +- 5 files changed, 203 insertions(+), 192 deletions(-) create mode 100644 src/global.d.ts diff --git a/lib/sequence/index.js b/lib/sequence/index.js index b49bf37..34dcd3d 100644 --- a/lib/sequence/index.js +++ b/lib/sequence/index.js @@ -1,103 +1,107 @@ 'use strict'; +Object.defineProperty(exports, "__esModule", { value: true }); const uuid = require('uuid').v4; const log = require('../log')('seq'); /** Object sequence features */ -const sequence = {}; -sequence._state = { - arr: [], - active: false, - paused: false, - frame: false, - delay: false, - count: 0, - stop: null -}; -sequence._loop = { - arr: [], - count: 0, - max: 0 -}; -sequence.start = function (options, cb) { - if (sequence._state.active) { - return false; +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; + }; } - 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; -}; -sequence.setStop = function () { - sequence._state.active = false; -}; -sequence.stop = function () { - 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; -}; -sequence.pause = function () { - sequence._state.paused = true; -}; -sequence.resume = function () { - sequence._state.paused = false; - sequence.step(); -}; -sequence.step = function () { - 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(); - }); + async start(options, cb) { + if (this._state.active) { + return false; } - 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(); - }); + 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 { - return sequence.stop(); + this._loop.max = 0; + } + this._state.stop = cb; + this.step(); + this._state.id = uuid(); + return this._state.id; + } + setStop() { + this._state.active = false; + } + pause() { + this._state.paused = true; + } + resume() { + this._state.paused = false; + this.step(); + } + 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(); + }); + } + 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(); + }); + } + else { + return this.stop(); + } + } + 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 }); } } - 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; +} +module.exports = new Sequence(); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/lib/sequence/index.js.map b/lib/sequence/index.js.map index 7968516..16b62d4 100644 --- a/lib/sequence/index.js.map +++ b/lib/sequence/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sequence/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAA;AAC/B,MAAM,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAA;AAEpC,+BAA+B;AAC/B,MAAM,QAAQ,GAAG,EAAE,CAAC;AAEpB,QAAQ,CAAC,MAAM,GAAG;IACjB,GAAG,EAAG,EAAE;IACR,MAAM,EAAG,KAAK;IACd,MAAM,EAAG,KAAK;IACd,KAAK,EAAE,KAAK;IACZ,KAAK,EAAG,KAAK;IACb,KAAK,EAAG,CAAC;IACT,IAAI,EAAG,IAAI;CACX,CAAA;AAED,QAAQ,CAAC,KAAK,GAAG;IAChB,GAAG,EAAG,EAAE;IACR,KAAK,EAAG,CAAC;IACT,GAAG,EAAG,CAAC;CACP,CAAA;AAED,QAAQ,CAAC,KAAK,GAAG,UAAU,OAAO,EAAE,EAAE;IACrC,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE;QAC3B,OAAO,KAAK,CAAA;KACZ;IAED,QAAQ,CAAC,MAAM,CAAC,MAAM,GAAG,IAAI,CAAA;IAC7B,QAAQ,CAAC,MAAM,CAAC,KA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AK,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE;oBAC7C,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAA;oBACnB,IAAI,CAAC,IAAI,EAAE,CAAA;gBACZ,CAAC,CAAC,CAAA;aACF;iBAAK;gBACL,OAAO,IAAI,CAAC,IAAI,EAAE,CAAA;aAClB;SACD;aAAM,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YAC9B,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,iBAAiB,EAAE,EAAE,IAAI,EAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,EAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;SAC3F;aAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YAC/B,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,EAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;SAC5F;IACF,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG,IAAI,QAAQ,EAAE,CAAC"} \ No newline at end of file diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..044e7e7 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1 @@ +declare module 'delay'; \ No newline at end of file diff --git a/src/sequence/index.ts b/src/sequence/index.ts index f60d86e..8cd331c 100644 --- a/src/sequence/index.ts +++ b/src/sequence/index.ts @@ -2,113 +2,118 @@ const uuid = require('uuid').v4 const log = require('../log')('seq') +import * as delay from 'delay' /** Object sequence features */ -const sequence = {}; - -sequence._state = { - arr : [], - active : false, - paused : false, - frame: false, - delay : false, - count : 0, - stop : null -} - -sequence._loop = { - arr : [], - count : 0, - max : 0 -} - -sequence.start = function (options, cb) { - if (sequence._state.active) { - return false +class Sequence { + private _state : any = { + arr : [], + active : false, + paused : false, + frame: false, + delay : false, + count : 0, + stop : null } - sequence._state.active = true - sequence._state.count = 0 - - if (options.arr) { - sequence._state.arr = options.arr + private _loop : any = { + arr : [], + count : 0, + max : 0 } - if (options.loop) { - sequence._loop.arr = options.loop - sequence._loop.count = 0 + constructor () { + } - 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 -} - -sequence.setStop = function () { - sequence._state.active = false -} - -sequence.stop = function () { - 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 -} - -sequence.pause = function () { - sequence._state.paused = true -} - -sequence.resume = function () { - sequence._state.paused = false - sequence.step() -} - -sequence.step = function () { - 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() + public async start (options : any, cb : Function) { + 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 + } + + public setStop () { + this._state.active = false + } + + public 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 + } + + public pause () { + this._state.paused = true + } + + public resume () { + this._state.paused = false + this.step() + } + + public 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() + }) + } 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() + }) + } else{ + return this.stop() + } + } 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 }) } - } 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 \ No newline at end of file +module.exports = new Sequence(); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 9814b07..bad2738 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,8 @@ "outDir": "./lib/", "rootDir" : "./src/", "paths" : { - "log" : ["./lib/log"] + "log" : ["./lib/log"], + "delay" : [ "./lib/delay"] } }, "exclude" : [