/
devTools.js
309 lines (275 loc) · 8.71 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { stringify, parse } from 'jsan';
import socketCluster from 'socketcluster-client';
import configureStore from './configureStore';
import { defaultSocketOptions } from './constants';
import { getHostForRN } from './utils/reactNative';
import { evalAction, getActionsArray } from 'remotedev-utils';
const ERROR = '@@remotedev/ERROR';
const monitorActions = [ // To be skipped for relaying actions
'@@redux/INIT', 'TOGGLE_ACTION', 'SWEEP', 'IMPORT_STATE', 'SET_ACTIONS_ACTIVE'
];
let instanceId;
let instanceName;
let socketOptions;
let socket;
let channel;
let store = {};
let lastAction;
let filters;
let isExcess;
let isMonitored;
let started;
let startOn;
let stopOn;
let sendOn;
let sendOnError;
let sendTo;
let lastErrorMsg;
let actionCreators;
function isFiltered(action) {
if (!action || !action.action || !action.action.type) return false;
return (
filters.whitelist && !action.action.type.match(filters.whitelist.join('|')) ||
filters.blacklist && action.action.type.match(filters.blacklist.join('|'))
);
}
function filterStagedActions(state) {
if (!filters) return state;
const filteredStagedActionIds = [];
const filteredComputedStates = [];
state.stagedActionIds.forEach((id, idx) => {
if (!isFiltered(state.actionsById[id])) {
filteredStagedActionIds.push(id);
filteredComputedStates.push(state.computedStates[idx]);
}
});
return { ...state,
stagedActionIds: filteredStagedActionIds,
computedStates: filteredComputedStates
};
}
function getLiftedState() {
return filterStagedActions(store.liftedStore.getState());
}
function send() {
if (!instanceId) instanceId = socket && socket.id || Math.random().toString(36).substr(2);
try {
fetch(sendTo, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
type: 'STATE',
id: instanceId,
name: instanceName,
payload: stringify(getLiftedState())
})
}).catch(function (err) {
console.warn(err);
});
} catch (err) {
console.warn(err);
}
}
function relay(type, state, action, nextActionId) {
if (filters && isFiltered(action)) return;
const message = {
type,
id: socket.id,
name: instanceName
};
if (state) message.payload = type === 'ERROR' ? state : stringify(state);
if (type === 'ACTION') {
message.action = stringify(action);
message.isExcess = isExcess;
message.nextActionId = nextActionId;
} else if (action) {
message.action = action;
}
socket.emit(socket.id ? 'log' : 'log-noid', message);
}
function dispatchRemotely(action) {
try {
const result = evalAction(action, actionCreators);
store.dispatch(result);
} catch (e) {
relay('ERROR', e.message);
}
}
function handleMessages(message) {
if (
message.type === 'IMPORT' || message.type === 'SYNC' && socket.id && message.id !== socket.id
) {
store.liftedStore.dispatch({
type: 'IMPORT_STATE', nextLiftedState: parse(message.state)
});
} if (message.type === 'UPDATE' || message.type === 'IMPORT') {
relay('STATE', getLiftedState());
} if (message.type === 'START') {
isMonitored = true;
if (typeof actionCreators === 'function') actionCreators = actionCreators();
relay('STATE', getLiftedState(), actionCreators);
} else if (message.type === 'STOP' || message.type === 'DISCONNECTED') {
isMonitored = false;
relay('STOP');
} else if (message.type === 'ACTION') {
dispatchRemotely(message.action);
} else if (message.type === 'DISPATCH') {
store.liftedStore.dispatch(message.action);
}
}
function async(fn) {
setTimeout(fn, 0);
}
function sendError(errorAction) {
// Prevent flooding
if (errorAction.message && errorAction.message === lastErrorMsg) return;
lastErrorMsg = errorAction.message;
async(() => {
store.dispatch(errorAction);
if (!started) send();
});
}
function catchErrors() {
if (typeof window === 'object' && typeof window.onerror === 'object') {
window.onerror = function (message, url, lineNo, columnNo, error) {
const errorAction = { type: ERROR, message, url, lineNo, columnNo };
if (error && error.stack) errorAction.stack = error.stack;
sendError(errorAction);
return false;
};
} else if (typeof global !== 'undefined' && global.ErrorUtils) {
global.ErrorUtils.setGlobalHandler((error, isFatal) => {
sendError({ type: ERROR, error, isFatal });
});
}
if (typeof console === 'object' && typeof console.error === 'function' && !console.beforeRemotedev) {
console.beforeRemotedev = console.error.bind(console);
console.error = function () {
let errorAction = { type: ERROR };
const error = arguments[0];
errorAction.message = error.message ? error.message : error;
if (error.sourceURL) {
errorAction = {
...errorAction, sourceURL: error.sourceURL, line: error.line, column: error.column
};
}
if (error.stack) errorAction.stack = error.stack;
sendError(errorAction);
console.beforeRemotedev.apply(null, arguments);
};
}
}
function str2array(str) {
return typeof str === 'string' ? [str] : str && str.length;
}
function init(options) {
instanceName = options.name;
if (options.filters) {
filters = options.filters;
}
if (options.port) {
socketOptions = {
port: options.port,
hostname: options.hostname || 'localhost',
secure: options.secure
};
} else socketOptions = defaultSocketOptions;
startOn = str2array(options.startOn);
stopOn = str2array(options.stopOn);
sendOn = str2array(options.sendOn);
sendOnError = options.sendOnError;
if (sendOn || sendOnError) {
sendTo = options.sendTo ||
`${socketOptions.secure ? 'https' : 'http'}://${socketOptions.hostname}:${socketOptions.port}`;
instanceId = options.id;
}
if (sendOnError === 1) catchErrors();
if (options.actionCreators) actionCreators = () => getActionsArray(options.actionCreators);
}
function start() {
if (started) return;
started = true;
socket = socketCluster.connect(socketOptions);
socket.on('error', function (err) {
console.warn(err);
});
socket.emit('login', 'master', (err, channelName) => {
if (err) { console.warn(err); return; }
channel = socket.subscribe(channelName);
channel.watch(handleMessages);
socket.on(channelName, handleMessages);
});
relay('START');
}
function stop() {
started = false;
isMonitored = false;
if (channel) {
channel.unsubscribe();
channel.unwatch();
}
if (socket) {
socket.off();
socket.disconnect();
}
}
function checkForReducerErrors(liftedState = store.liftedStore.getState()) {
if (liftedState.computedStates[liftedState.currentStateIndex].error) {
if (started) relay('STATE', filterStagedActions(liftedState));
else send();
return true;
}
return false;
}
function monitorReducer(state = {}, action) {
lastAction = action.type;
if (!started && sendOnError === 2 && store.liftedStore) async(checkForReducerErrors);
else if (action.action) {
if (startOn && !started && startOn.indexOf(action.action.type) !== -1) async(start);
else if (stopOn && started && stopOn.indexOf(action.action.type) !== -1) async(stop);
else if (sendOn && !started && sendOn.indexOf(action.action.type) !== -1) async(send);
}
return state;
}
function handleChange(state, liftedState, maxAge) {
if (checkForReducerErrors(liftedState)) return;
const nextActionId = liftedState.nextActionId;
const liftedAction = liftedState.actionsById[nextActionId - 1];
const action = liftedAction.action;
if (action.type === '@@INIT') {
relay('INIT', state, { timestamp: Date.now() });
} else if (monitorActions.indexOf(lastAction) === -1) {
if (lastAction === 'JUMP_TO_STATE') return;
relay('ACTION', state, liftedAction, nextActionId);
if (!isExcess && maxAge) isExcess = liftedState.stagedActionIds.length >= maxAge;
} else {
relay('STATE', filterStagedActions(liftedState));
}
}
export default function devTools(options = {}) {
init({
...options,
hostname: getHostForRN(options.hostname)
});
const realtime = typeof options.realtime === 'undefined'
? process.env.NODE_ENV === 'development' : options.realtime;
if (!realtime && !(startOn || sendOn || sendOnError)) return f => f;
const maxAge = options.maxAge || 30;
return (next) => {
return (reducer, initialState) => {
store = configureStore(
next, monitorReducer, { maxAge, shouldCatchErrors: !!sendOnError }
)(reducer, initialState);
if (realtime) start();
store.subscribe(() => {
if (isMonitored) handleChange(store.getState(), store.liftedStore.getState(), maxAge);
});
return store;
};
};
}
devTools.updateStore = (newStore) => {
store = newStore;
};