Вот набросок очереди команд:
function CommandQueue(limit) {
this._limit = limit || 10;
this._arr = [];
this._pointer = -1;
}
CommandQueue.prototype.undoCount = function() {
return this._pointer + 1;
};
CommandQueue.prototype.redoCount = function() {
return this._arr.length - this._pointer - 1;
};
CommandQueue.prototype.undo = function() {
if (!this.undoCount()) {
return false;
}
this._arr[this._pointer].undo();
this._pointer--;
return true;
};
CommandQueue.prototype.redo = function() {
if (!this.redoCount()) {
return false;
}
this._pointer++;
this._arr[this._pointer].redo();
return true;
};
CommandQueue.prototype.action = function(command) {
this._arr.splice(this._pointer + 1, this.redoCount(), command);
this.redo();
if (this._arr.length > this._limit) {
this._arr.shift();
this._pointer--;
}
};
использование:
var q = new CommandQueue(10);
q.action({
undo: function() {...},
redo: function() {...}
});