-
Notifications
You must be signed in to change notification settings - Fork 121
/
redux-devtools.extension.ts
244 lines (215 loc) · 9.37 KB
/
redux-devtools.extension.ts
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
import { ObservableStore } from '@codewithdan/observable-store';
import { ReduxDevtoolsExtensionConnection, ReduxDevtoolsExtensionConfig } from './interfaces';
import { EMPTY, Observable, Subscription } from 'rxjs';
import { ObservableStoreExtension } from './interfaces';
import { AngularDevToolsExtension } from './angular/angular-devtools-extension';
export class ReduxDevToolsExtension extends ObservableStore<any> implements ObservableStoreExtension {
private window = (window as any);
private require = this.window.require;
private devToolsExtensionConnection: ReduxDevtoolsExtensionConnection;
private devtoolsExtension = (window as any)['__REDUX_DEVTOOLS_EXTENSION__'];
private routerPropertyName = '__router';
private devToolsActionName = '__action';
private angularExtension: AngularDevToolsExtension;
private isAngular = this.checkIsAngular();
private isReact = this.checkIsReact();
private routeTriggeredByDevTools = false;
private sub: Subscription;
constructor(private config?: ReduxDevtoolsExtensionConfig) {
super({ trackStateHistory: true, logStateChanges: false });
}
init() {
this.sync();
this.window.addEventListener('DOMContentLoaded', () => {
if (this.isAngular) {
this.angularExtension = new AngularDevToolsExtension();
}
this.hookRouter();
});
if (this.devtoolsExtension) {
if (this.config && this.config.routerPropertyName) {
this.routerPropertyName = this.config.routerPropertyName;
}
this.sub = this.connect();
}
}
private connect(config?: ReduxDevtoolsExtensionConfig) {
return new Observable(subscriber => {
const connection = this.devtoolsExtension.connect(config);
this.devToolsExtensionConnection = connection;
connection.init(config);
connection.subscribe((change: any) => subscriber.next(change));
return connection.unsubscribe;
})
.subscribe((action: any) => this.processDevToolsAction(action));
}
private disconnect() {
if (this.devtoolsExtension) {
this.devtoolsExtension.disconnect();
if (this.sub) {
this.sub.unsubscribe();
}
}
}
private processDevToolsAction(action: any) {
// Called as user interacts with Redux Devtools controls
if (action.type === Actions.DISPATCH) {
switch (action.payload.type) {
case Actions.JUMP_TO_STATE:
case Actions.JUMP_TO_ACTION:
if (action.state) {
const actionState = JSON.parse(action.state);
if (actionState && actionState.__devTools) {
// If we have a route then navigate to it
if (actionState.__devTools.router) {
this.navigateToPath(actionState);
}
this.setStateFromDevTools(actionState, `${actionState.__devTools.action} [${Actions.REDUX_DEVTOOLS_JUMP}]`);
}
}
break;
case Actions.IMPORT_STATE:
this.loadState(action);
break;
}
}
}
private loadState(action: any) {
// clear existing state from devtools
this.disconnect();
this.connect();
if (action.payload) {
const nextLiftedState = action.payload.nextLiftedState;
if (nextLiftedState && nextLiftedState.computedStates) {
nextLiftedState.computedStates.shift();
for (const computedState of nextLiftedState.computedStates) {
if (computedState.state && computedState.state.__devTools) {
this.devToolsExtensionConnection.send(computedState.state.__devTools.action, computedState.state);
}
}
}
}
}
private navigateToPath(actionState: any) {
const path = actionState.__devTools.router.path;
if (window.location.pathname !== path) {
// Ensure route info doesn't make it into the devtool
// since the devtool is actually triggering the route
// rather than an end user interacting with the app.
// It will be set to false in this.hookRouter().
this.routeTriggeredByDevTools = true;
if (this.config && this.config.customRouteNavigator) {
this.config.customRouteNavigator.navigate(path);
return;
}
if (this.isAngular) {
this.angularExtension.navigate(path);
return;
}
if (this.isReact && (this.config && this.config.reactRouterHistory)) {
this.config.reactRouterHistory.push(path);
return;
}
}
}
private setStateFromDevTools(state: any, action: string) {
// #### Run in Angular zone if it's loaded to help with change dectection
if (this.angularExtension) {
this.angularExtension.runInZone(() => this.dispatchDevToolsState(state, action));
return;
}
this.dispatchDevToolsState(state, action);
}
private dispatchDevToolsState(state: any, action: string) {
// Set devtools state for each service but don't dispatch state
// since it will also dispatch global state by default when setState() is called
for (let service of ObservableStore.allStoreServices) {
service.setState(state, action, false);
// dispatch service state but not global state
service.dispatchState(state, false);
}
// dispatch global state changes
this.dispatchState(state);
}
private sync() {
this.globalStateChanged.subscribe(() => {
this.sendStateToDevTool();
});
}
private sendStateToDevTool() {
if (this.stateHistory && this.stateHistory.length) {
const lastItem = this.stateHistory[this.stateHistory.length - 1];
const { action, endState } = lastItem;
if (!action.endsWith(Actions.REDUX_DEVTOOLS_JUMP + ']')) {
// Adding action value here since there's no way to retrieve it when
// it's dispatched from the redux devtools
this.devToolsExtensionConnection.send(action, {
...endState,
__devTools: { ...endState.__devTools, action }
});
}
}
}
private hookRouter() {
try {
const path = window.location.pathname;
this.setState({
__devTools: {
router: { path },
action: Actions.ROUTE_NAVIGATION
}
}, `${Actions.ROUTE_NAVIGATION} [${path}]`);
window.history.pushState = (f => function() {
var ret = f.apply(this, arguments);
window.dispatchEvent(new CustomEvent('pushstate', { detail: window.location.pathname }));
window.dispatchEvent(new CustomEvent('locationchange', { detail: window.location.pathname }));
return ret;
})(window.history.pushState);
window.history.replaceState = (f => function() {
var ret = f.apply(this, arguments);
window.dispatchEvent(new CustomEvent('replacestate', { detail: window.location.pathname }));
window.dispatchEvent(new CustomEvent('locationchange', { detail: window.location.pathname }));
return ret;
})(window.history.replaceState);
window.addEventListener('popstate', () => {
window.dispatchEvent(new CustomEvent('locationchange', { detail: window.location.pathname }));
});
window.addEventListener('locationchange', (e: CustomEvent) => {
if (!this.routeTriggeredByDevTools) {
const path = e.detail;
this.setState({
__devTools: {
router: { path },
action: Actions.ROUTE_NAVIGATION
}
}, `${Actions.ROUTE_NAVIGATION} [${path}]`);
}
else {
this.routeTriggeredByDevTools = false;
}
});
}
catch (e) {
console.log(e);
}
}
private checkIsReact() {
const isReact = (this.window.__REACT_DEVTOOLS_GLOBAL_HOOK__ &&
this.window.__REACT_DEVTOOLS_GLOBAL_HOOK__._renderers &&
this.window.__REACT_DEVTOOLS_GLOBAL_HOOK__._renderers.length) ||
this.window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ || this.window.React ||
(this.window.require && (this.require('react') || this.require('React')));
return isReact;
}
private checkIsAngular() {
return this.window.ng;
}
}
enum Actions {
DISPATCH = 'DISPATCH',
JUMP_TO_STATE = 'JUMP_TO_STATE',
JUMP_TO_ACTION = 'JUMP_TO_ACTION',
REDUX_DEVTOOLS_JUMP = 'REDUX_DEVTOOLS_JUMP',
ROUTE_NAVIGATION = 'ROUTE_NAVIGATION',
IMPORT_STATE = 'IMPORT_STATE'
}