« get me outta code hell

undo-manager.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/undo-manager.js
blob: 4a042ad552957d77d8956e93ee933fb2a887779a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class UndoManager {
  constructor() {
    this.actionStack = []
    this.undoneStack = []
  }

  pushAction(action) {
    this.undoneStack = []
    this.actionStack.push(action)
    action.activate()
  }

  undoLastAction() {
    if (this.actionStack.length === 0) {
      return
    }

    const action = this.actionStack.pop()
    this.undoneStack.push(action)

    action.undo()
  }

  redoLastUndoneAction() {
    if (this.undoneStack.length === 0) {
      return
    }

    const action = this.undoneStack.pop()
    this.actionStack.push(action)
    action.activate()
  }

  get safeToPushAction() {
    // Is it safe to push a new action? That is, since pushing a new action
    // clears the undone actions stack, will any undone actions be lost?

    return this.undoStack.length === 0
  }
}

module.exports = UndoManager