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(types)(middleware/devtools): fix action type in devtools's setState #1183

Merged
merged 12 commits into from
Aug 18, 2022
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ const createBearSlice = (set, get) => ({
})
```

You can also log action's type along with its payload:

```jsx
const createBearSlice = (set, get) => ({
addFishes: (count) =>
set(
(prev) => ({ fishes: prev.fishes + count }),
false,
{ type: 'bear/addFishes', payload: count }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's good to have the payload key something other than "payload" to imply "payload" is not a special key, it can be anything...

Suggested change
{ type: 'bear/addFishes', payload: count }
{ type: 'bear/addFishes', count }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's Redux-style logging and the payload is a special key for action in Redux Devtools. IMO it would be great to keep it like that while using Redux Devtools.
image
Here is a link to the conversation on the pmndrs DC: https://discord.com/channels/740090768164651008/740093228904218657/1006824679111135343

Copy link
Contributor

@devanshj devanshj Aug 11, 2022

Choose a reason for hiding this comment

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

In what way is it special? Like how does redux devtools treat payload key different from count key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main goal of this PR is logging the action's payload as the payload field, not any field name the user wants. To be consistent with the Redux convention (https://github.com/redux-utilities/flux-standard-action).
Ok, I am getting now that you want to abstract from it, right? The question is whether we want to have it consistent or to have whatever field's name the user wants but I think we should leave this decision for Zustand's owner.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay so as I guessed redux devtools doesn't treat payload key in any special way.

Users allowed to choose the convention they want to follow, the one you've linked is just one of the many styles out there, it's not required. The only requirement and constraint of a redux action is that it must have a type property.

I understand that you want to impose an convention for your logger, but the code being edited here affects everyone, and we can't impose a non-required convention for everyone.

I think we should leave this decision for Zustand's owner

Sure, just giving my two cents.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to impose some of my conventions. I assumed that is a well-known standard and it would be nice to be consistent with it, but not. And it's okay.

I will add [key: string]: unknown; but you mention:

in general it's good to stay away from index signatures

Could you elaborate? Is there the other way to achieve that?

Copy link
Contributor

@devanshj devanshj Aug 12, 2022

Choose a reason for hiding this comment

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

Ah I missed this comment, it's mostly resolved now but I'll reply for sake of completeness...

I don't want to impose some of my conventions. I assumed that is a well-known standard and it would be nice to be consistent with it, but not. And it's okay.

I never said it was "your" convention, I'm well aware about FSA, but not everyone uses it. My point was simply this: Redux does not require users to follow FSA so we should also not.

Could you elaborate? Is there the other way to achieve that?

Now I did here #1183 (comment)

),
})
```

If an action type is not provided, it is defaulted to "anonymous". You can customize this default value by providing an `anonymousActionType` parameter:

```jsx
Expand Down
5 changes: 4 additions & 1 deletion src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ type StoreDevtools<S> = S extends {
}
? {
setState(
...a: [...a: TakeTwo<A>, actionType?: string | { type: unknown }]
...a: [
...a: TakeTwo<A>,
actionType?: string | { type: unknown; payload?: unknown }
]
): Sr
lucasrabiec marked this conversation as resolved.
Show resolved Hide resolved
}
: never
Expand Down
39 changes: 29 additions & 10 deletions tests/devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,45 @@ describe('If there is no extension installed...', () => {
expect(console.warn).not.toBeCalled()
})
})

describe('When state changes...', () => {
it("sends { type: setStateName || 'anonymous` } as the action with current state", () => {
const api = create(
devtools(() => ({ count: 0, foo: 'bar' }), {
name: 'testOptionsName',
enabled: true,
})
)
api.setState({ count: 10 }, false, 'testSetStateName')
it("sends { type: 'testSetStateName' } as the action with current state", () => {
const { setState } = createTestState()
setState({ count: 10 }, false, 'testSetStateName')
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'testSetStateName' },
{ count: 10, foo: 'bar' }
)
api.setState({ count: 5, foo: 'baz' }, true)
})

it("sends { type: 'testSetStateName', payload: 15 } as the action with current state", () => {
const { setState } = createTestState()
setState({ count: 15 }, false, {
type: 'testSetStateName',
payload: 15,
})
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'testSetStateName', payload: 15 },
{ count: 15, foo: 'bar' }
)
})

it("sends { type: 'anonymous' } as the action with current state", () => {
const { setState } = createTestState()
setState({ count: 5, foo: 'baz' }, true)
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'anonymous' },
{ count: 5, foo: 'baz' }
)
})

function createTestState() {
return create(
devtools(() => ({ count: 0, foo: 'bar' }), {
name: 'testOptionsName',
enabled: true,
})
)
}
lucasrabiec marked this conversation as resolved.
Show resolved Hide resolved
})

describe('when it receives an message of type...', () => {
Expand Down