-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateManager.js
47 lines (38 loc) · 997 Bytes
/
StateManager.js
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
'use strict'
var _ = require('lodash');
var state0 = {
speed: 0.0,
direction: true,
light: false
}
function StateManager (state0, commandActions) {
this.state0 = _.cloneDeep(state0);
this.state = _.cloneDeep(state0);
this.commandActions = commandActions;
this.history = [];
}
StateManager.prototype.change = function (command) {
var oldState = this.state;
this.history.push(this.state);
this.state = this.commandActions[command](oldState);
return diff(oldState, this.state);
};
StateManager.prototype.reset = function () {
this.state = _.cloneDeep(this.state0);
};
var diff = function(oldObj, newObj) {
return _(newObj).map(
function(val, key){
if (newObj[key] !== oldObj[key]) {
return key;
}
else {
return 'x';
}
}
)
.filter(
function (val) { return val != 'x'}
).value();
}
module.exports = StateManager;