https://github.com/lloydzhou/action-stack-manager
action stack manager
https://github.com/lloydzhou/action-stack-manager
history manager redo stack undo
Last synced: 4 months ago
JSON representation
action stack manager
- Host: GitHub
- URL: https://github.com/lloydzhou/action-stack-manager
- Owner: lloydzhou
- License: mit
- Created: 2023-02-03T09:27:52.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-03T10:27:02.000Z (over 2 years ago)
- Last Synced: 2025-02-12T14:09:05.981Z (4 months ago)
- Topics: history, manager, redo, stack, undo
- Language: TypeScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# action-stack-manager
manage redo/undo stack
管理撤销重做调用栈
1. always push (commit and rollback)
2. redo() ==> call commit
2. undo() ==> call rollback## demo
```
const h = new HistoryManager()let inc = 0
// 0 --> 2
await h.push(() => inc = 2, () => inc = 0)
// 2 --> 3
await h.push(() => inc += 1, () => inc -= 1)console.log('HistoryManager', h, inc)
console.log(this)
// 3 --> 2
await h.undo()
console.log('undo', h.redoSize, h.undoSize, inc, 'inc = 2')
// 2 --> 0
await h.undo()
console.log('undo', h.redoSize, h.undoSize, inc, 'inc = 0')
// 0 --> 2
await h.redo()
console.log('redo', h.redoSize, h.undoSize, inc, 'inc = 2')
// 2 --> 3
await h.redo()
console.log('redo', h.redoSize, h.undoSize, inc, 'inc = 3')
// 报错
await h.redo().catch(console.log)
console.log('redo', h.redoSize, h.undoSize, inc, 'inc = 3')
// 3 --> 2
await h.undo()
console.log('undo', h.redoSize, h.undoSize, inc, 'inc = 2')
// 2 --> 4
await h.push(() => inc += 2, () => inc -= 2)
console.log('push', h.redoSize, h.undoSize, inc, 'inc = 4', h)
// 4 --> 2
await h.undo()
console.log('undo', h.redoSize, h.undoSize, inc, 'inc = 2')
// 2 --> 0
await h.undo()
console.log('undo', h.redoSize, h.undoSize, inc, 'inc = 0')
// 0 --> 4
await h.push(() => inc += 4, () => inc -= 4)
console.log('push', h.redoSize, h.undoSize, inc, 'inc = 4', h)
```