UNPKG

remember-js

Version:

A simple program used to remember and redo program operations

87 lines (82 loc) 2.53 kB
'use strict'; var Storage = require('./Storage'); var storageId = '#REMEMBER_JS_REMEMBER_QUEUE'; function Remember(options) { this.options = options || {}; this.inSequence = options.inSequence || false; this.rememberQueue = new Storage(options.storageId || storageId, options.storage); this.onConsumingComplete = options.onConsumingComplete; } Remember.prototype = { registerAction: function registerAction(name, callback) { this.actions = this.actions || {}; this.actions[name] = callback; }, set: function set(options) { Remember.call(this, Object.assign(this.options, options)); }, _remember: function _remember(data) { this.rememberQueue.push(data); }, _next: function _next() { this.rememberQueue.shift(); if (typeof this.consumeCount === 'number') { this.consumeCount++; } this._consumeInSequence(); }, do: function _do(name, a, b, c, d, e, f) { var args = argumentsToArray(arguments); if (this.inSequence) { this._remember(args); this._consumeInSequence(); } else { this._doAction.apply(this, args); } }, _doAction: function _doAction(name, a, b, c, d, e, f) { var callback = this.actions[name], args = argumentsToArray(arguments); callback(this._remember.bind(this, args), a, b, c, d, e, f); }, _doActionInSequence: function _doActionInSequence(name, a, b, c, d, e, f) { var callback = this.actions[name]; callback(this._next.bind(this), a, b, c, d, e, f); }, _consumeInSequence: function _consumeInSequence() { if (this.rememberQueue.getLength()) { this._doActionInSequence.apply(this, this.rememberQueue.peek()); } else if (typeof this.consumeCount === 'number') { if (this.consumeCount && typeof this.onConsumingComplete === 'function') { this.onConsumingComplete(this.consumeCount); } this.consumeCount = undefined; } }, _consume: function _consume() { var queueLength = this.rememberQueue.getLength(), i = 0; for (; i < queueLength; i++) { this._doAction.apply(this.rememberQueue.shift()); } }, consume: function consume() { if (this.inSequence) { this.consumeCount = 0; this._consumeInSequence(); } else { this._consume(); } }, clear: function clear() { this.rememberQueue.clear(); } }; module.exports = Remember; function argumentsToArray(args) { var i = args.length, argsArr = []; while (i--) { argsArr.unshift(args[i]); }return argsArr; }