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

clearStore() works differently than resetStore() - clears cache after callbacks #4694

Closed
ds8k opened this issue Apr 11, 2019 · 0 comments
Closed

Comments

@ds8k
Copy link
Contributor

ds8k commented Apr 11, 2019

Intended outcome:
clearStore() should clear the existing cache and then execute the onClearStore callbacks

Actual outcome:
clearStore() runs the onClearStore callbacks and then clears the cache, negating any operations that were performed within said callbacks.

How to reproduce the issue:

const unsubscribe = client.onClearStore(() => {
  client.writeData({ data: { value: 'test' } })
})

client.clearStore()
console.log(client.extract()) // cache is empty

Looking at the code for clearStore shows exactly why this is happening:

public clearStore(): Promise<void | null> {
  const { queryManager } = this;
  return Promise.resolve()
    .then(() => Promise.all(this.clearStoreCallbacks.map(fn => fn()))) // callbacks are fired first
    .then(
      () =>
        queryManager ? queryManager.clearStore() : Promise.resolve(null), // then the store is cleared
    );
}

resetStore works the way I would expect it to:

public resetStore(): Promise<ApolloQueryResult<any>[] | null> {
  return Promise.resolve()
    .then(() => {
      return this.queryManager
        ? this.queryManager.clearStore() // the store is cleared
        : Promise.resolve(null);
    })
    .then(() => Promise.all(this.resetStoreCallbacks.map(fn => fn()))) // then the callbacks are run
    .then(() => {
      return this.queryManager && this.queryManager.reFetchObservableQueries
        ? this.queryManager.reFetchObservableQueries()
        : Promise.resolve(null);
    });
}

Versions

  System:
    OS: macOS 10.14.3
  Binaries:
    Node: 8.13.0 - ~/.nvm/versions/node/v8.13.0/bin/node
    Yarn: 1.15.2 - ~/.yarn/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.13.0/bin/npm
  Browsers:
    Chrome: 73.0.3683.103
    Safari: 12.0.3
  npmPackages:
    apollo-boost: 0.3.1 => 0.3.1 
    apollo-cache-inmemory: 1.3.11 => 1.3.11 
    apollo-cache-persist: 0.1.1 => 0.1.1 
    react-apollo: 2.5.4 => 2.5.4 
@ds8k ds8k changed the title clearStore() works differently than resetStore() clearStore() works differently than resetStore() - clears cache after callbacks Apr 11, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 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

No branches or pull requests

1 participant