-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.js
More file actions
105 lines (79 loc) · 2.86 KB
/
Copy pathManager.js
File metadata and controls
105 lines (79 loc) · 2.86 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _rx = require("rx");
var _rx2 = _interopRequireDefault(_rx);
var _Ledger = require("./Ledger");
var _Ledger2 = _interopRequireDefault(_Ledger);
var _Inventory = require("./Inventory");
var _Inventory2 = _interopRequireDefault(_Inventory);
var _utilsClamp = require("./utils/clamp");
var _utilsClamp2 = _interopRequireDefault(_utilsClamp);
var writeData = function writeData(delta, inventory, ledger) {
inventory.set(delta);
ledger.record(delta, inventory.peek());
};
var Manager = function Manager(inventory) {
var _this = this;
_classCallCheck(this, Manager);
if (!inventory) {
throw new Error("The Manager requires an inventory");
return;
}
var ledger = new _Ledger2["default"]();
var privateInventory = new _Inventory2["default"](inventory.peek());
var currentLedgerIndex = -1;
var isTimeTravelling = false;
this.updates = new _rx2["default"].ReplaySubject(1);
var sendUpdate = function sendUpdate() {
return _this.updates.onNext({
currentLedgerIndex: currentLedgerIndex,
isTimeTravelling: isTimeTravelling,
transactions: ledger.peek()
});
};
this.goto = function (index) {
currentLedgerIndex = (0, _utilsClamp2["default"])(index, 0, ledger.peek().length - 1);
isTimeTravelling = true;
inventory.toggleLock(true);
inventory.forceSet(ledger.peek()[currentLedgerIndex].state);
sendUpdate();
};
this.pause = function () {
inventory.toggleLock(true);
isTimeTravelling = true;
sendUpdate();
};
this.resume = function () {
inventory.toggleLock(false);
inventory.set(privateInventory.peek());
isTimeTravelling = false;
currentLedgerIndex = ledger.peek().length - 1;
sendUpdate();
};
this.record = function (delta) {
writeData(delta, privateInventory, ledger);
if (!isTimeTravelling) currentLedgerIndex++;
sendUpdate();
};
this.commit = function () {
isTimeTravelling = false;
inventory.toggleLock(false);
inventory.forceSet(ledger.peek()[currentLedgerIndex].state);
ledger.revertTo(currentLedgerIndex);
sendUpdate();
};
this.rewind = function (amount) {
return _this.goto(currentLedgerIndex - amount);
};
this.fastForward = function (amount) {
return _this.goto(currentLedgerIndex + amount);
};
// prime the ledger
this.record({});
};
exports["default"] = Manager;
module.exports = exports["default"];