-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (44 loc) · 1.28 KB
/
index.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
'use strict';
const fs = require('fs');
const redux = require('redux');
class ActionStore {
constructor(reducer, history) {
this.history = history || [];
this.reducer = reducer;
this._store = redux.createStore(reducer);
this.createStore = this.createStore.bind(this);
}
static fromJson(path, reducer) {
try {
return new ActionStore(reducer, JSON.parse(fs.readFileSync(path, {encoding: 'utf8'})).history);
} catch (err) {
return new ActionStore(reducer);
}
}
get storeActions() {
return store => next => action => {
this.record(action);
return next(action);
}
}
record(action) {
this.history.push(action);
}
createStore(reducer) {
this.history.forEach(this._store.dispatch.bind(this._store));
return redux.createStore(reducer || this.reducer, this._store.getState());
}
toJson(path) {
fs.writeFileSync(path, JSON.stringify({
history: this.history
}));
}
setPersistence(path) {
process.on('beforeExit', () => this.toJson(path));
process.on('SIGINT', () => {
this.toJson(path);
});
return this;
}
}
module.exports = ActionStore;