This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add integration with redux devtools extension
Currently supports: - Tracking actions and navigation state - Time travel for navigation state It doesn't do anything in production
- Loading branch information
Showing
5 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import * as React from 'react'; | ||
import { NavigationState, NavigationAction, PartialState } from './types'; | ||
|
||
type State = NavigationState | PartialState<NavigationState> | undefined; | ||
|
||
type Options = { | ||
name: string; | ||
reset: (state: NavigationState) => void; | ||
state: State; | ||
}; | ||
|
||
type DevTools = { | ||
init(value: any): void; | ||
send(action: any, value: any): void; | ||
subscribe( | ||
listener: (message: { type: string; [key: string]: any }) => void | ||
): () => void; | ||
}; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface Global { | ||
__REDUX_DEVTOOLS_EXTENSION__: | ||
| { | ||
connect(options: { name: string }): DevTools; | ||
disconnect(): void; | ||
} | ||
| undefined; | ||
} | ||
} | ||
} | ||
|
||
export default function useDevTools({ name, reset, state }: Options) { | ||
const devToolsRef = React.useRef<DevTools>(); | ||
|
||
if ( | ||
process.env.NODE_ENV !== 'production' && | ||
global.__REDUX_DEVTOOLS_EXTENSION__ && | ||
devToolsRef.current === undefined | ||
) { | ||
devToolsRef.current = global.__REDUX_DEVTOOLS_EXTENSION__.connect({ name }); | ||
} | ||
|
||
const devTools = devToolsRef.current; | ||
const lastStateRef = React.useRef<State>(state); | ||
const actions = React.useRef< | ||
Array<{ key: string; action: NavigationAction }> | ||
>([]); | ||
|
||
React.useEffect(() => { | ||
devTools && devTools.init(lastStateRef.current); | ||
}, [devTools]); | ||
|
||
React.useEffect( | ||
() => | ||
devTools && | ||
devTools.subscribe(message => { | ||
if (message.type === 'DISPATCH' && message.state) { | ||
reset(JSON.parse(message.state)); | ||
} | ||
}), | ||
[devTools, reset] | ||
); | ||
|
||
const trackState = React.useCallback( | ||
(state: State) => { | ||
if (!devTools) { | ||
return; | ||
} | ||
|
||
while (actions.current.length > 1) { | ||
devTools.send(actions.current.shift(), lastStateRef.current); | ||
} | ||
|
||
if (actions.current.length) { | ||
devTools.send(actions.current.pop(), state); | ||
} else if (lastStateRef.current !== state) { | ||
devTools.send('@@UNKNOWN', state); | ||
} | ||
|
||
lastStateRef.current = state; | ||
}, | ||
[devTools] | ||
); | ||
|
||
const trackAction = React.useCallback( | ||
(key: string, action: NavigationAction) => { | ||
if (!devTools) { | ||
return; | ||
} | ||
|
||
actions.current.push({ key, action }); | ||
}, | ||
[devTools] | ||
); | ||
|
||
return { | ||
trackAction, | ||
trackState, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters