| 2 | import { HistoryInterface } from "./interfaces/History"; |
| 3 | |
| 4 | export default class CommandHistory implements HistoryInterface { |
| 5 | private history: Array<CommandInterface> = []; |
| 6 | private undone_history: Array<CommandInterface> = []; |
| 7 | |
| 8 | public undo_posible: Boolean = false; |
| 9 | public redo_posible: Boolean = false; |
| 10 | |
| 11 | public push(command: CommandInterface) { |
| 12 | this.undone_history = []; |
| 13 | this.history.push(command) |
| 14 | this.update_status() |
| 15 | } |
| 16 | |
| 17 | public pop(): CommandInterface { |
| 18 | if (!this.undo_posible) return null; |
| 19 | |
| 20 | const command = this.history.pop() |
| 21 | this.undone_history.push(command) |
| 22 | this.update_status() |
| 23 | return command |
| 24 | } |
| 25 | |
| 26 | public repush(): CommandInterface { |
| 27 | if (!this.redo_posible) return null; |
| 28 | |
| 29 | const command = this.undone_history.pop() |
| 30 | this.history.push(command) |
| 31 | this.update_status() |
| 32 | return command |
| 33 | } |
| 34 | |
| 35 | private update_status() { |
| 36 | this.undo_posible = this.history.length > 0; |
| 37 | this.redo_posible = this.undone_history.length > 0; |
| 38 | } |
| 39 | } |
nothing calls this directly
no outgoing calls
no test coverage detected