Skip to content

Commit

Permalink
fix(middleware): revert devtools extension connector with try-catch (p…
Browse files Browse the repository at this point in the history
…mndrs#724)

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

* add a test case

Co-authored-by: Devansh Jethmalani <[email protected]>
  • Loading branch information
dai-shi and devanshj authored Dec 24, 2021
1 parent cb99a36 commit 5b3b963
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
11 changes: 11 additions & 0 deletions tests/devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)({
Expand Down

0 comments on commit 5b3b963

Please sign in to comment.