Skip to content

Commit 7ebe03a

Browse files
authored
Create index.ts
1 parent 6f813a5 commit 7ebe03a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Context API Dev Tools
2+
const devTools: any = { send: () => {} };
3+
let isInitialized = false;
4+
let firstRender = false;
5+
let oldArgs = {};
6+
7+
export const useContextDevTools = (dispatch: Function) => {
8+
if (!isInitialized) {
9+
devTools.current = (window as any).__REDUX_DEVTOOLS_EXTENSION__.connect();
10+
isInitialized = true;
11+
}
12+
13+
const sendDispatch = (args: any) => {
14+
if(dispatch) {
15+
dispatch(args);
16+
}
17+
oldArgs = args;
18+
};
19+
20+
const sendUpdatedState = (updatedState: any) => {
21+
if (!firstRender) {
22+
devTools.current.init(updatedState);
23+
firstRender = true;
24+
} else {
25+
devTools.current.send(oldArgs, { root: updatedState });
26+
}
27+
};
28+
29+
const disconnectDevTools = () => {
30+
return devTools.current.disconnect();
31+
}
32+
33+
return {
34+
sendDispatch,
35+
sendUpdatedState,
36+
disconnectDevTools,
37+
};
38+
};

0 commit comments

Comments
 (0)