fix(middleware): revert devtools extension connector with try-catch (#724)

* fix(middleware): revert devtools extension connector with try-catch

* add a test case

Co-authored-by: Devansh Jethmalani <devanshj@users.noreply.github.com>
This commit is contained in:
Daishi Kato 2021-12-24 16:09:46 +09:00 committed by GitHub
parent cb99a36c59
commit 5b3b963181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -87,16 +87,20 @@ export const devtools =
? { name: options }
: options
if (typeof window === 'undefined') {
return fn(set, get, api)
let extensionConnector
try {
extensionConnector =
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ||
(window as any).top.__REDUX_DEVTOOLS_EXTENSION__
} catch {
// ignored
}
const extensionConnector =
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ||
(window as any).top.__REDUX_DEVTOOLS_EXTENSION__
if (!extensionConnector) {
if (process.env.NODE_ENV === 'development') {
if (
process.env.NODE_ENV === 'development' &&
typeof window !== 'undefined'
) {
console.warn(
'[zustand devtools middleware] Please install/enable Redux devtools extension'
)

View File

@ -483,6 +483,17 @@ it('works in non-browser env', () => {
global.window = originalWindow
})
it('works in react native env', () => {
const originalWindow = global.window
global.window = {} as any
expect(() => {
create(devtools(() => ({ count: 0 })))
}).not.toThrow()
global.window = originalWindow
})
it('preserves isRecording after setting from devtools', () => {
const api = create(devtools(() => ({ count: 0 })))
;(extensionSubscriber as (message: any) => void)({