-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devtools.js
180 lines (140 loc) · 4.27 KB
/
devtools.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -----------------------------------------------------------------------------
const logPrefix = '[La Taverne 🐛]';
// -----------------------------------------------------------------------------
const isDebounced = (action = {}) => {
if (action.devtools && action.devtools.debounce) {
return true;
}
if (!action.from) {
return false;
}
return isDebounced(action.from);
};
// -----------------------------------------------------------------------------
const getNesting = (action = {}) => {
if (!action.from) {
return '';
}
return '──' + getNesting(action.from);
};
// -----------------------------------------------------------------------------
const rootType = (action = {}) => {
if (!action.from) {
return action.type;
}
return rootType(action.from);
};
// -----------------------------------------------------------------------------
const orderParentActions = (action = {}) => {
if (!action.from) {
return [action];
}
return [...orderParentActions(action.from), action];
};
// -----------------------------------------------------------------------------
const record = (
devtoolsInstance,
action,
dispatch,
getState,
applyStateFiltering
) => {
let type = action.type;
const nesting = getNesting(action.from);
type = `${action.from ? `└──${nesting}` : ''} ${type}`;
const currentState = getState();
const state = applyStateFiltering(currentState);
devtoolsInstance.send(
{
...action,
type
},
state
);
action.__recorded = true;
};
// -----------------------------------------------------------------------------
const recordDebouncedTree = (
devtoolsInstance,
action,
dispatch,
getState,
applyStateFiltering
) => {
const actions = orderParentActions(action);
actions.forEach(action => {
record(
devtoolsInstance,
{
type: `${action.type} (debounced x ${
devtoolsInstance.debounceCount[rootType(action)]
})`,
from: action.from
},
dispatch,
getState,
applyStateFiltering
);
});
};
// -----------------------------------------------------------------------------
const createDevtools = (options = {}) => taverne => {
const extension = window && window.__REDUX_DEVTOOLS_EXTENSION__;
const {applyStateFiltering = o => o} = options;
if (!extension) {
if (process.env.NODE_ENV === 'development') {
console.error(
'❌ Could not connect Redux devtools. \nTry to install and enable the extension'
);
}
return;
}
const initialState = taverne.getState();
const devtoolsInstance = extension.connect({maxAge: 100});
devtoolsInstance.timeouts = {};
devtoolsInstance.debounceCount = {};
devtoolsInstance.init(initialState);
devtoolsInstance.subscribe(message => {
if (message.type === 'DISPATCH' && message.state) {
taverne.setState(JSON.parse(message.state));
} else {
console.log(`${logPrefix} monitor message not handled:`, message);
}
});
devtoolsInstance.onDispatch = (action, dispatch, getState) => {
if (action.__recorded) {
return;
}
if (isDebounced(action)) {
const actionRootType = rootType(action);
if (devtoolsInstance.debounceCount[actionRootType] === undefined) {
devtoolsInstance.debounceCount[actionRootType] = 0;
}
if (actionRootType === action.type) {
devtoolsInstance.debounceCount[actionRootType]++;
}
if (devtoolsInstance.timeouts[actionRootType]) {
clearTimeout(devtoolsInstance.timeouts[actionRootType]);
}
devtoolsInstance.timeouts[actionRootType] = setTimeout(() => {
if (devtoolsInstance.debounceCount[actionRootType] > 0) {
recordDebouncedTree(
devtoolsInstance,
action,
dispatch,
getState,
applyStateFiltering
);
}
devtoolsInstance.debounceCount[actionRootType] = 0;
}, 250);
} else {
devtoolsInstance.debounceCount[action.type] = 0;
record(devtoolsInstance, action, dispatch, getState, applyStateFiltering);
}
};
console.log(`${logPrefix} Plugged Redux devtools`);
return devtoolsInstance;
};
// -----------------------------------------------------------------------------
export default createDevtools;