Skip to content

Commit

Permalink
breaking(middleware/devtools): use official devtools extension types (#…
Browse files Browse the repository at this point in the history
…819)

* breaking(middleware/devtools): use official devtools extension types

* type object.create

* avoid emitting @redux-devtools/extension

* fix type with any

* refactor

* lock date-fns version

* refactor
  • Loading branch information
dai-shi authored Apr 7, 2022
1 parent a34649d commit f65a25a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/plugin-transform-typescript": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"@redux-devtools/extension": "^3.2.2",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-replace": "^4.0.0",
Expand Down Expand Up @@ -171,6 +172,7 @@
"prettier": "^2.6.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"redux": "^5.0.0-alpha.0",
"rollup": "^2.70.1",
"rollup-plugin-esbuild": "^4.9.1",
"rollup-plugin-terser": "^7.0.2",
Expand Down
55 changes: 26 additions & 29 deletions src/middleware/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {} from '@redux-devtools/extension'
import { GetState, PartialState, SetState, State, StoreApi } from '../vanilla'

declare module '../vanilla' {
Expand All @@ -7,6 +8,13 @@ declare module '../vanilla' {
}
}

// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
type Message = {
type: string
payload?: any
state?: any
}

type Write<T extends object, U extends object> = Omit<T, keyof U> & U
type Cast<T, U> = T extends U ? T : U

Expand Down Expand Up @@ -39,22 +47,6 @@ interface DevtoolsOptions {
symbol?: boolean
map?: boolean
set?: boolean
/**
* @deprecated serialize.options is deprecated, just use serialize
*/
options:
| boolean
| {
date?: boolean
regex?: boolean
undefined?: boolean
nan?: boolean
infinity?: boolean
error?: boolean
symbol?: boolean
map?: boolean
set?: boolean
}
}
}

Expand Down Expand Up @@ -184,21 +176,14 @@ export function devtools<
}
const devtoolsOptions =
options === undefined
? { name: undefined, anonymousActionType: undefined }
? {}
: typeof options === 'string'
? { name: options }
: options
if (typeof (devtoolsOptions as any)?.serialize?.options !== 'undefined') {
console.warn(
'[zustand devtools middleware]: `serialize.options` is deprecated, just use `serialize`'
)
}

let extensionConnector
let extensionConnector: typeof window['__REDUX_DEVTOOLS_EXTENSION__']
try {
extensionConnector =
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ||
(window as any).top.__REDUX_DEVTOOLS_EXTENSION__
extensionConnector = window.__REDUX_DEVTOOLS_EXTENSION__
} catch {
// ignored
}
Expand All @@ -212,7 +197,9 @@ export function devtools<
return fn(set, get, api)
}

let extension = Object.create(extensionConnector.connect(devtoolsOptions))
let extension = (Object.create as <T>(t: T) => T)(
extensionConnector.connect(devtoolsOptions)
)
// We're using `Object.defineProperty` to set `prefix`, so if extensionConnector.connect
// returns the same reference we'd get cannot redefine property prefix error
// hence we `Object.create` to make a new reference
Expand Down Expand Up @@ -314,7 +301,14 @@ export function devtools<
}
}

extension.subscribe((message: any) => {
;(
extension as unknown as {
// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
subscribe: (
listener: (message: Message) => void
) => (() => void) | undefined
}
).subscribe((message) => {
switch (message.type) {
case 'ACTION':
if (typeof message.payload !== 'string') {
Expand Down Expand Up @@ -364,7 +358,10 @@ export function devtools<
nextLiftedState.computedStates.slice(-1)[0]?.state
if (!lastComputedState) return
setStateFromDevtools(lastComputedState)
extension.send(null, nextLiftedState)
extension.send(
null as any, // FIXME no-any
nextLiftedState
)
return
}

Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,13 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@redux-devtools/extension@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@redux-devtools/extension/-/extension-3.2.2.tgz#2d6da4df2c4d32a0aac54d824e46f52b1fd9fc4d"
integrity sha512-fKA2TWNzJF7wXSDwBemwcagBFudaejXCzH5hRszN3Z6B7XEJtEmGD77AjV0wliZpIZjA/fs3U7CejFMQ+ipS7A==
dependencies:
"@babel/runtime" "^7.17.0"

"@rollup/plugin-babel@^5.3.1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz#04bc0608f4aa4b2e4b1aebf284344d0f68fda283"
Expand Down Expand Up @@ -4379,6 +4386,13 @@ rechoir@^0.6.2:
dependencies:
resolve "^1.1.6"

redux@^5.0.0-alpha.0:
version "5.0.0-alpha.0"
resolved "https://registry.yarnpkg.com/redux/-/redux-5.0.0-alpha.0.tgz#a787df7b92a69af70900c84586fc2dc89ca97ab5"
integrity sha512-9NQVVttmTiwECalBRd6sKWW4e8u6ekR1rlfsHy0ZOU95kcDpKp4enL8xbNZkKgT7GhnNmlfNZp1HWlHs+XZaww==
dependencies:
"@babel/runtime" "^7.9.2"

regenerate-unicode-properties@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-10.0.1.tgz#7f442732aa7934a3740c779bb9b3340dccc1fb56"
Expand Down

0 comments on commit f65a25a

Please sign in to comment.