-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
110 lines (110 loc) · 3.23 KB
/
store.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
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
106
107
108
109
110
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const appolo_event_dispatcher_1 = require("appolo-event-dispatcher");
const deepmerge = require("deepmerge");
class Store extends appolo_event_dispatcher_1.EventDispatcher {
constructor(initialState = {}, options = { maxStates: 1 }) {
super();
this.initialState = initialState;
this.options = options;
this._states = [];
this._stateIndex = -1;
this.MaxStates = this.options.maxStates || 1;
this._init();
}
_init() {
if (Array.isArray(this.initialState)) {
this._states = this.initialState;
}
else {
this._states.push(this.initialState);
}
this._stateIndex = this.countIndex;
}
createInitialState() {
return this.initialState;
}
state() {
return this.stateAt(this._stateIndex);
}
states() {
let $self = this, index = -1;
return {
[Symbol.iterator]() {
return this;
},
next() {
index++;
return index == $self.count
? { value: undefined, done: true }
: { value: $self.stateAt(index), done: false };
}
};
}
stateAt(index) {
index = this._checkStateIndex(index);
let state = this._states[index];
if (!state) {
return null;
}
return this._clone(state);
}
_checkStateIndex(index) {
return index <= 0 ? 0 : (index > this.countIndex ? this.countIndex : index);
}
setState(value, options = { arrayMerge: "extend" }) {
let state = this.state;
let mergeOptions = {};
if (options.arrayMerge == "override") {
mergeOptions.arrayMerge = (destinationArray, sourceArray, options) => sourceArray;
}
let newState = state ? deepmerge(state, value, mergeOptions) : value;
if (this._stateIndex != this.countIndex) {
this._states = this._states.slice(0, this._stateIndex + 1);
}
this._states.push(newState);
if (this.count > this.MaxStates) {
this._states.shift();
}
this._stateIndex = this.countIndex;
this.fireEvent("stateChanged", this.state());
}
get count() {
return this._states.length;
}
get currentIndex() {
return this._stateIndex;
}
get countIndex() {
return this.count - 1;
}
_clone(state) {
return JSON.parse(JSON.stringify(state));
}
get prevState() {
return this.stateAt(this._stateIndex - 1);
}
goToPrevState() {
this.goToState(this._stateIndex - 1);
}
get nextState() {
return this.stateAt(this._stateIndex + 1);
}
goToNextState() {
this.goToState(this._stateIndex + 1);
}
goToState(index) {
index = this._checkStateIndex(index);
let oldIndex = this._stateIndex;
this._stateIndex = index;
if (oldIndex != this._stateIndex) {
this.fireEvent("stateChanged", this.state);
}
}
reset() {
this._states = [];
this._init();
}
}
exports.Store = Store;
//# sourceMappingURL=store.js.map