-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
84 lines (73 loc) · 2.18 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
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
var { createStore } = require("redux");
var { composeWithDevTools } = require("redux-devtools-extension");
function reduxReducer(state = {}, action) {
return Object.assign({}, state, action.payload);
}
function reducAction(name, data) {
return {
type: name,
payload: data
};
}
function copy(target, source) {
var obj = {};
for (var i in target) obj[i] = target[i];
for (var i in source) obj[i] = source[i];
return obj;
}
function set(path, value, source, target) {
if (path.length) {
target[path[0]] =
1 < path.length ? set(path.slice(1), value, source[path[0]], {}) : value;
return copy(source, target);
}
return value;
}
function get(path, source) {
for (var i = 0; i < path.length; i++) {
source = source[path[i]];
}
return source;
}
module.exports = function devtools(app) {
var composeEnhancers = composeWithDevTools({ action: reducAction });
var store;
return function(state, actions, view, container) {
var appActions;
function wire(path, actions) {
for (var key in actions) {
if (typeof actions[key] === "function") {
(function(key, action) {
actions[key] = function() {
var reducer = action.apply(this, arguments);
return function (slice) {
var data = typeof reducer === "function"
? reducer(slice, get(path, appActions))
: reducer;
if (data && !data.then) {
state = set(path, copy(slice, data), state, {});
store.dispatch(reducAction(key, state));
}
return data;
};
};
})(key, actions[key]);
} else {
wire(path.concat(key), (actions[key] = copy(actions[key])));
}
}
}
wire([], (actions = copy(actions)));
actions.replaceState = function(actualState) {
return function (state) {
return actualState;
}
};
store = createStore(reduxReducer, state, composeEnhancers());
store.subscribe(function() {
appActions.replaceState(store.getState());
});
appActions = app(state, actions, view, container);
return appActions;
};
};