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

[RFC] feat: Use a status enum to label the current stage of a SWR hook #1215

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

icyJoseph
Copy link
Contributor

@icyJoseph icyJoseph commented Jun 15, 2021

Hi!

I've been using this library, and I've collected some feedback from colleagues or people I show this hook to.

One persistent subject is that, often developers want to derive the status of the hook, that is, is it loading? is it validating? is there a difference between them at all? is there any data, etc. Refer to: #563

I understand that we should open up discussions for these subjects first, but in this case it seemed best to talk over the code, because of the change itself and because I am short on bandwidth as of now.

I'll create a discussion thread and link both. Hopefully it doesn't create confusion. #1217

At the core of this PR, we derive a new key called status which can be loading, stale, error or validating.

In addition, I noticed that isValidating doesn't really settle on error cases. This might introduce breaking changes for users.

I included some tests which helpfully help understanding the subject matter of the feature.

Last but not least, more documentation is needed.

  • Documentation
  • Analyze possible breaking changes
  • More units tests
  • Infinite SWR

@codesandbox-ci
Copy link

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

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@huozhi
Copy link
Member

huozhi commented Jun 16, 2021

As #141 (comment) said, composing data, error and isValidating will result in 8 cases that make status enums hard to remeber for users. IMO it's better to keep automic for returned values and let users compose them flexibly on top of swr.

@huozhi huozhi self-requested a review June 18, 2021 04:32
@shuding
Copy link
Member

shuding commented Jun 18, 2021

Also worth noting, #1160 might help this. We can build a status middleware to attach a status enum to the SWR return object.

@koba04 koba04 mentioned this pull request Jun 19, 2021
@pacocoursey pacocoursey removed their request for review July 9, 2021 18:18
@phaux
Copy link

phaux commented Aug 21, 2022

It would be even better if the response was a tagged union:

type SWRResponse<Data, Error> =
  | {
    status: "loading"
    isValidating: boolean
    ...
  }
  | {
    status: "error"
    error: Error
    isValidating: boolean
    ...
  }
  | {
    status: "validating"
    data: Data
    isValidating: true
    ...
  }
  | {
    status: "stale"
    data: Data
    isValidating: false
    ...
  }

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

4 participants