Показать сообщение отдельно
  #9 (permalink)  
Старый 07.01.2015, 21:38
Профессор
Отправить личное сообщение для Яростный Меч Посмотреть профиль Найти все сообщения от Яростный Меч
 
Регистрация: 12.04.2010
Сообщений: 557

Вот набросок очереди команд:
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() {...}
});
Ответить с цитированием