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

fix: devtools to patch api.setState #172

Merged
merged 9 commits into from
Sep 1, 2020
Merged

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Aug 31, 2020

Close #171.

There are a few technical issues:
1. we need to fix types in vanilla.ts and index.ts too.
2. we can't chain middleware that only patches set.

Due to 2), I'm a bit hesitant to this approach.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 31, 2020

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 c981883:

Sandbox Source
React Configuration
React Typescript Configuration
bold-fast-6hsx3 Issue #171

@dai-shi
Copy link
Member Author

dai-shi commented Aug 31, 2020

@TkDodo Here's your example with this change. https://codesandbox.io/s/agitated-stonebraker-bg7bi

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

Okay, this f4c6239 should be much better.
https://codesandbox.io/s/wonderful-lovelace-rjego

@TkDodo
Copy link
Collaborator

TkDodo commented Sep 1, 2020

Thanks! From what I can tell, the code looks good. I understand that we cannot change the interface of set, so there won't be the possibility to give those "actions" a name, but this is an understandable limitation 👍

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

@TkDodo Did you try the sandbox?
It would be nice if you could try and find possible bugs.

we cannot change the interface of set, so there won't be the possibility to give those "actions" a name

By set, do you mean the first argument of the creator function, or api.setState?

@dai-shi dai-shi marked this pull request as ready for review September 1, 2020 07:49
@dai-shi dai-shi changed the title wip: devtools to patch api.setState fix: devtools to patch api.setState Sep 1, 2020
@TkDodo
Copy link
Collaborator

TkDodo commented Sep 1, 2020

Did you try the sandbox?
It would be nice if you could try and find possible bugs.

Yes, I did try the codesandbox and it works fine for me. I made a test case for testing it in combination with the redux middleware: https://codesandbox.io/s/react-forked-xbjg9

From what I can see, the INCREASE action is logged twice (once as action and once as INCREASE). This is the same as the previous behaviour, so this PR didn't introduce this. However, setState is now logged as well (wasn't logged before), so that's good. Is this worth a follow up issue maybe?

By set, do you mean the first argument of the creator function, or api.setState?

I mean api.setState. set in the creator function is patched / typed so that we have a third argument "name", but that's not the case for api.setState

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

Thanks!

From what I can see, the INCREASE action is logged twice

Maybe, I can fix it in this PR.

I mean api.setState. set in the creator function is patched / typed so that we have a third argument "name", but that's not the case for api.setState

Yeah, totally correct. Let's leave it as a limitation because types would be too messy, if we were to support this. (I think it's technically possible.)

If you are adventurous, it might be able to do like this:

const useStore = create(devtools((set, get, api) => {
  api.setState = set
  return {
    // ...
  }
}))

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

https://codesandbox.io/s/react-forked-6czzc
Looks good?

Oh, but this will stop logging if you use set with redux middleware.

@TkDodo
Copy link
Collaborator

TkDodo commented Sep 1, 2020

Yeah, totally correct. Let's leave it as a limitation because types would be too messy, if we were to support this. (I think it's technically possible.)

yes, sounds good.

If you are adventurous, it might be able to do like this:

Yes, I am actually doing that already as a workaround until this PR lands:

let setState: SetState

const useState = create<State>(
    devtools((set) => {
        setState = set
        return { ... }
}))

Oh, but this will stop logging if you use set with redux middleware.

hm, how would you even do that ? In the example, we only pass the reducer to redux, and useState.setState is now correctly logged ... 🤔

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

This 99a9f64 is probably better, even thought it's unlikely that people use set with redux middleware. (This means another middleware between devtools and redux.)

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

@TkDodo Would you do the final test? If it's OK, we release it.

@TkDodo
Copy link
Collaborator

TkDodo commented Sep 1, 2020

using the redux example with 99a9f64 now logs increase button twice, first as setState and then as INCREASE

https://codesandbox.io/s/angry-mestorf-qymx1

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

Oops!

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

Reverted. 😄

https://codesandbox.io/s/react-forked-661ih

@dai-shi dai-shi merged commit 925a90b into master Sep 1, 2020
@dai-shi dai-shi deleted the devtools-to-patch-api-set-state branch September 1, 2020 12:45
@TkDodo
Copy link
Collaborator

TkDodo commented Sep 1, 2020

👍

@dai-shi
Copy link
Member Author

dai-shi commented Sep 1, 2020

Published: https://www.npmjs.com/package/zustand/v/3.0.3

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.

devtools don't always log
2 participants