Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking(middleware/devtools): use official devtools extension types #819

Merged

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Feb 22, 2022

Wouldn't this be more maintainable?

@github-actions
Copy link

github-actions bot commented Feb 22, 2022

Size Change: -384 B (-1%)

Total Size: 29.7 kB

Filename Size Change
dist/esm/middleware.js 3.35 kB -62 B (-2%)
dist/middleware.js 3.44 kB -61 B (-2%)
dist/system/middleware.development.js 3.46 kB -61 B (-2%)
dist/system/middleware.production.js 2.4 kB -63 B (-3%)
dist/umd/middleware.development.js 3.55 kB -60 B (-2%)
dist/umd/middleware.production.js 2.38 kB -77 B (-3%)
ℹ️ View Unchanged
Filename Size
dist/context.js 518 B
dist/esm/context.js 448 B
dist/esm/index.js 558 B
dist/esm/shallow.js 272 B
dist/esm/vanilla.js 353 B
dist/index.js 661 B
dist/shallow.js 318 B
dist/system/context.development.js 572 B
dist/system/context.production.js 393 B
dist/system/index.development.js 666 B
dist/system/index.production.js 430 B
dist/system/shallow.development.js 338 B
dist/system/shallow.production.js 253 B
dist/system/vanilla.development.js 421 B
dist/system/vanilla.production.js 263 B
dist/umd/context.development.js 662 B
dist/umd/context.production.js 488 B
dist/umd/index.development.js 802 B
dist/umd/index.production.js 538 B
dist/umd/shallow.development.js 447 B
dist/umd/shallow.production.js 344 B
dist/umd/vanilla.development.js 562 B
dist/umd/vanilla.production.js 364 B
dist/vanilla.js 424 B

compressed-size-action

Copy link
Member Author

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devanshj Hi, can you check this please?

symbol?: boolean
map?: boolean
set?: boolean
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this is breaking. Why did we have options in the first place?

Copy link
Contributor

@devanshj devanshj Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why there was the options thing but this is a good change. We can deprecate the existing shape and support both shapes old and new in 3.x. (Like detect with serialize.options !== undefined or something and have another overload for the new options to add the deprecation jsdoc hint on the old ones)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea. i'll try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extensionConnector =
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ||
(window as any).top.__REDUX_DEVTOOLS_EXTENSION__
extensionConnector = window.__REDUX_DEVTOOLS_EXTENSION__
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we need top. The official '@redux-devtools/extension' doesn't seem to have one.
This is breaking too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably so that the devtools work in codesandbox etc (as __REDUX_DEVTOOLS_EXTENSION__ would only be in the parent window)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, let me experiment. (jotai/valtio doesn't do this, so we want to make three consistent.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://codesandbox.io/s/cool-sun-bnowv5
window.top doesn't work anyway in codesandbox.
image

@devanshj
Copy link
Contributor

(can't comment on an unchanged line)

- let extension = Object.create(extensionConnector.connect(devtoolsOptions))
+ let extension = (Object.create as <T>(t: T) => T)(extensionConnector.connect(devtoolsOptions))

Object.create returns any but connect returns the correct type.

@@ -27,21 +29,19 @@ type StoreSetStateWithAction<S> = S extends { getState: () => infer T }
interface DevtoolsOptions {
Copy link
Contributor

@devanshj devanshj Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if we can use the exported EnhancerOptions type too (We'd also get the jsdoc for free). Something like...

interface DevtoolsOptions extends Pick<EnhancerOptions, "name" | "serialize"> {
  anonymousActionType?: string
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but now TS users have to install some devDependencies (very counter-intuitive), which isn't very handy. Let's keep it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fair. One could write a postinstall script that fetches the .d.ts files from unpkg and writes it to the filesystem but I think that would be going too far.

@@ -1,3 +1,5 @@
import '@redux-devtools/extension'
Copy link
Contributor

@devanshj devanshj Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you'd know this better but just make sure this doesn't add any unused runtime code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the final app code should be fine, but we should actually remove in dist. I'll see how I can do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e9cf15e this should do.

@dai-shi
Copy link
Member Author

dai-shi commented Feb 23, 2022

Object.create returns any but connect returns the correct type.

Thanks. 06123b1

Now, I get type errors...

if (action.type === '__setState') {
setStateFromDevtools(action.state as PartialState<S>)
return
;(extension as any) // FIXME no-any
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could use this type definition: https://github.com/reduxjs/redux-devtools/blob/b82de745928211cd9b7daa7a61b197ad9e11ec36/extension/src/app/api/index.ts#L522-L536

export interface ConnectResponse {
  init: <S, A extends Action<unknown>>(
    state: S,
    liftedData?: LiftedState<S, A, unknown>
  ) => void;
  subscribe: <S, A extends Action<unknown>>(
    listener: (message: ListenerMessage<S, A>) => void
  ) => (() => void) | undefined;
  unsubscribe: () => void;
  send: <S, A extends Action<unknown>>(
    action: A,
    state: LiftedState<S, A, unknown>
  ) => void;
  error: (payload: string) => void;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It's a little weird that ConnectResponse in @redux-devtools/extension only has init and send, one could consider opening an issue to write the actual type definition too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 4, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0efb349:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration
Next.js Configuration

@dai-shi dai-shi added this to the v4.0.0 milestone Mar 4, 2022
@dai-shi dai-shi merged commit f65a25a into main Apr 7, 2022
@dai-shi dai-shi deleted the breaking/middleware/devtools/use-devtools-extension-types branch April 7, 2022 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants