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

Add idGenerator option to createAsyncThunk (#743) #976

Conversation

Shrugsy
Copy link
Collaborator

@Shrugsy Shrugsy commented Apr 2, 2021

Closes #743

This PR adds an optional idGenerator option to the createAsyncThunk options to allow specifying how request IDs are generated rather than using nanoid.

@netlify
Copy link

netlify bot commented Apr 2, 2021

Deploy preview for redux-starter-kit-docs ready!

Built with commit 62d1deb

https://deploy-preview-976--redux-starter-kit-docs.netlify.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 2, 2021

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 62d1deb:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration

@Shrugsy Shrugsy changed the title Add idGenerator option to createAsyncThunk (#743) [DRAFT] Add idGenerator option to createAsyncThunk (#743) Apr 2, 2021
* Make `idGenerator` description consistent with
  other sections of docs for `createAsyncThunk`
* Re-arrange tests for `idGenerator`
@@ -89,6 +89,7 @@ An object with the following optional fields:

- `condition`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
- `dispatchConditionRejection`: if `condition()` returns `false`, the default behavior is that no actions will be dispatched at all. If you still want a "rejected" action to be dispatched when the thunk was canceled, set this flag to `true`.
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](https://www.npmjs.com/package/nanoid).
Copy link
Collaborator

Choose a reason for hiding this comment

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

✋ Let's point this to ./otherExports.mdx#nanoid instead, and maybe also tweak the wording of the nanoid paragraph to say it's the "default" instead of "automatically used"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both points now addressed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I realised I made a sentence for the nanoid description incorrect when I adjusted it, but tweaked it to make sense again this time.

@@ -392,7 +399,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
arg: ThunkArg
): AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> {
return (dispatch, getState, extra) => {
const requestId = nanoid()
const requestId = (options?.idGenerator || nanoid)()
Copy link
Collaborator

Choose a reason for hiding this comment

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

✋ We can use nullish coalescing here instead of ||, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How exactly are you thinking? As in options?.idGenerator?.() || nanoid()?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(options?.idGenerator ?? nanoid)()

just swapping || for ??. same behavior, slightly more semantically correct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry just realised you said nullish coalescing (??). Definitely makes more sense, will do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now addressed

Copy link
Member

@msutkowski msutkowski left a comment

Choose a reason for hiding this comment

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

Looks good, just have a few comments.

@@ -89,6 +89,7 @@ An object with the following optional fields:

- `condition`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
- `dispatchConditionRejection`: if `condition()` returns `false`, the default behavior is that no actions will be dispatched at all. If you still want a "rejected" action to be dispatched when the thunk was canceled, set this flag to `true`.
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](https://www.npmjs.com/package/nanoid).
Copy link
Member

@msutkowski msutkowski Apr 2, 2021

Choose a reason for hiding this comment

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

Suggested change
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](https://www.npmjs.com/package/nanoid).
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx#nanoid).

We don't use all of nanoid, so we should just link back specifically to what we inline and re-export

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, but the link should go to the sibling Markdown file instead of the website directly :)

Copy link
Member

Choose a reason for hiding this comment

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

i'm fired 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now addressed

src/createAsyncThunk.ts Show resolved Hide resolved
@Shrugsy
Copy link
Collaborator Author

Shrugsy commented Apr 2, 2021

I'm struggling to understand why the preact step in the CI build is failing here: https://github.com/reduxjs/redux-toolkit/pull/976/checks?check_run_id=2254585946#step:3:265

  src/createAsyncThunk.test.ts:706:9 - error TS2345: Argument of type '{ idGenerator: jest.Mock<string, []>; }' is not assignable to parameter of type 'AsyncThunkOptions<void, {}>'.  
  Object literal may only specify known properties, and 'idGenerator' does not exist in type 'AsyncThunkOptions<void, {}>'.  

 706       { idGenerator }

As far as I'm aware I added idGenerator to the AsyncThunkOptions definition, including the api:

createAsyncThunk.ts

redux-toolkit.api.md

Does anyone have any insight as to what might be causing this error? Have I missed something somewhere?

@markerikson
Copy link
Collaborator

I think I saw the compressed size check also failing on some TS type changes with one of my other PRs this week. Not sure what's causing that.

* Link to local documentation for nanoid rather than npm package
* Update local docs for `nanoid` to reflect changes to
  `createAsyncThunk` options
* Use nullish coalescing operator when getting requestId generator
@msutkowski
Copy link
Member

msutkowski commented Apr 2, 2021

This line in the cAT docs will also need to be updated:

  • requestId: a unique string ID value that was automatically generated to identify this request sequence`

@markerikson
Copy link
Collaborator

Eh, I think that line's fine. There's still a requestId, and it's still a string, and it's still automatically generated - it's just a question of what the string contents are.

@Shrugsy Shrugsy changed the title [DRAFT] Add idGenerator option to createAsyncThunk (#743) Add idGenerator option to createAsyncThunk (#743) Apr 2, 2021
@Shrugsy
Copy link
Collaborator Author

Shrugsy commented Apr 2, 2021

Regarding the requestId description on the cAT docs: I think it is still accurate as it is. It could be extended to say something like "If an idGenerator function is provided, it uses that to generated the ID instead", but that would just essentially be repeating what is in the 'Options' section shortly after it.

Regarding the Compressed size check failing - Is there anything I can do about that? As far as I can tell, the failure it's hitting just shouldn't be happening. The same test that fails there passes in every other check.

Regarding comment threads, do you folks prefer:

  • for PR creators to advise that they addressed it, and leave it to the 'comment thread starter' to actually confirm it was addressed and press 'resolve conversation'?
    or
  • for PR creators to decide for themselves that it was resolved, and press 'resolve conversation'?

@markerikson
Copy link
Collaborator

Y'know what, this looks good. Let's put it in.

@markerikson markerikson merged commit 309c4bd into reduxjs:master Apr 2, 2021
@Shrugsy Shrugsy deleted the 743-add-idGenerator-option-to-createAsyncThunk branch April 3, 2021 00:07
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.

Possibility of customising function that generates requestId in the action's meta of createAsyncThunk
3 participants