Skip to content

Conversation

@sergiodxa
Copy link
Contributor

Resolves #264

This PR adds a mutateMany function, it has the following definition

export type mutateManyInterface<Data = any> = (
  select: (key: keyInterface) => boolean,
  data?: Data | Promise<Data> | mutateCallback<Data>,
  shouldRevalidate?:
    | boolean
    | ((key: keyInterface, currentValue: Data) => boolean)
) => Promise<Data[]>

The function will be called as

mutateMany(
  key => key.startsWith("/api/users"),
  undefined
)

The way this function works is:

  1. It calls select passing for each key in the cache, there you could check if you want to mutate it or not (returned true/false)
  2. It calls mutate against the selected keys and pass data, this data is the same as mutate
  3. It calls shouldRevalidate (if it's a function) passing the selected key to get if it should or not revalidate, it could also be a boolean to pass true/false to all selected keys

@QuietShu if you think this is a good addition to SWR I could document it and add some tests.

This will allow mutateMany updaters to know which key they are updating.
@shuding
Copy link
Member

shuding commented Feb 26, 2020

I don't think we should add it to this lib. The goal is that we want this lib to be extremely simple and performant. IMO things like this can be implemented with existing APIs.

@mxschmitt
Copy link
Contributor

Looks like this is a duplicate of #268

@sergiodxa
Copy link
Contributor Author

@QuietShu 👍 I agree, will close it and if someone wants to implement it could use it as an example implementation

Also if someones one to publish this as a package in npm feel free to do it.

@sergiodxa sergiodxa closed this Feb 26, 2020
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.

Mutate multiple call results

3 participants