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 onResetStore method to client to register callbacks after resetStore #2812

Merged
merged 9 commits into from
Jan 5, 2018

Conversation

peggyrayzis
Copy link
Contributor

Adding this functionality will allow us to register a callback to write defaults to the store after client.resetStore for apollo-link-state, fixing this issue.

@apollo-cla
Copy link

apollo-cla commented Jan 3, 2018

Messages
📖

Please add your name and email to the AUTHORS file (optional)

Generated by 🚫 dangerJS

@jbaxleyiii
Copy link
Contributor

@peggyrayzis awesome! Do we have plans to document this? Would you be able to open a docs PR / issue or want to add it here?

@peggyrayzis
Copy link
Contributor Author

@jbaxleyiii just added docs for client.onResetStore & the new method to the API reference section! client.resetStore wasn't documented either (aside from the authentication recipe), so I added docs for that one too.

@ShockiTV
Copy link
Contributor

ShockiTV commented Jan 4, 2018

This should be part of client init options, as it is no action, just configuration

const client = new ApolloClient({
  cache,
  link: stateLink,
  onResetStore: stateLink.writeDefaults,
});

And it can be used to also do persistor.purge(); for time being :-D

@peggyrayzis
Copy link
Contributor Author

@ShockiTV If it's part of the configuration, you'll only be able to register one callback (or an array if we change it) at initialization. There could be some scenarios where you would want to register a callback dynamically within a component, so I think it makes sense to leave it as a method.

@ShockiTV
Copy link
Contributor

ShockiTV commented Jan 4, 2018

@peggyrayzis It is 1 callback and you can provide your own function to put 20 side effects to it with custom branching so no array needed ofc.
But I suggest adding it as init option too so you can set it right from the start to the right callback. Not after it was instantiated.

@ShockiTV
Copy link
Contributor

ShockiTV commented Jan 4, 2018

Also the push behaviour feels strange when there is no way how to unregister the hooked promise.

Just 1 callback is enough and developer can manage to put 10 effects in async function.
It is more straightforward than proposed way of adding 10 callbacks and than managing horde of if switches to do nothing in each of them when every 1 of them have to be executed each time.

Not mentioning that promise.all behaviour when 1 of them .reject

@jbaxleyiii
Copy link
Contributor

jbaxleyiii commented Jan 5, 2018

@ShockiTV I do think we need to have a way to unregister callbacks, but the benefit of the array vs a single method is the easy of keeping the listeners close to what they would be used for.

For instance, two immediate use cases for this callback are to rewrite to the cache after resetStore (for apollo-link-state) and to force rerender your UI (or portions of it) after a store reset. If you have it as a method with callbacks, then you can do something like this:

import { withApollo } from "react-apollo";

export class Foo extends Component {
  constructor(props) {
    super(props);
    this.unsubscribe = props.client.onResetStore(
      () => this.setState({ reset: false }
    );
    this.state = { reset: false }
  }
  componentDidUnmount(){
    this.unsubscribe()
  }
  render(){
    return this.state.reset ? <div /> : <span />
  }
}

export default withApollo(Foo);

This does show where we need an unsubscribe hook so it can be cleaned up though

@jbaxleyiii jbaxleyiii merged commit f25aa16 into apollographql:master Jan 5, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants