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

Clear InMemoryCache watches when cache.reset() called #8826

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.4.14 (not yet released)

### Bug Fixes

- Clear `InMemoryCache` `watches` set when `cache.reset()` called. <br/>
[@benjamn](https://github.com/benjamn) in [#8826](https://github.com/apollographql/apollo-client/pull/8826)

## Apollo Client 3.4.13

### Bug Fixes
Expand Down
110 changes: 110 additions & 0 deletions src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,116 @@ describe("ReactiveVar and makeVar", () => {
expect(spy).toBeCalledWith(cache);
});

it("should remove all watchers when cache.reset() called", () => {
const { cache, query, nameVar } = makeCacheAndVar(false);
const unwatchers: Record<string, Array<() => void>> = Object.create(null);
const diffCounts: Record<string, number> = Object.create(null);

function watch(id: string) {
const fns = unwatchers[id] || (unwatchers[id] = []);
fns.push(cache.watch({
query,
optimistic: true,
immediate: true,
callback() {
diffCounts[id] = (diffCounts[id] || 0) + 1;
},
}));
}

watch("a");
watch("b");
watch("c");
watch("a");
watch("d");

expect(cache["watches"].size).toBe(5);
expect(diffCounts).toEqual({
a: 2,
b: 1,
c: 1,
d: 1,
});

unwatchers.a.forEach(unwatch => unwatch());
unwatchers.a.length = 0;
expect(cache["watches"].size).toBe(3);

nameVar("Hugh");
expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
});

cache.reset();
expect(cache["watches"].size).toBe(0);

expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
});

nameVar("Brian");
// No change because cache.reset() called.
expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
});

cache.writeQuery({
query,
data: {
onCall: {
__typename: "Person",
},
},
});

watch("e");
watch("f");

expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
e: 1,
f: 1,
});

nameVar("Trevor");
expect(cache["watches"].size).toBe(2);
expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
e: 2,
f: 2,
});

cache.reset();
expect(cache["watches"].size).toBe(0);

nameVar("Danielle");
expect(diffCounts).toEqual({
a: 2,
b: 2,
c: 2,
d: 2,
e: 2,
f: 2,
});

expect(cache["watches"].size).toBe(0);
});

it("should recall forgotten vars once cache has watches again", () => {
const { cache, nameVar, query } = makeCacheAndVar(false);
const spy = jest.spyOn(nameVar, "forgetCache");
Expand Down
9 changes: 8 additions & 1 deletion src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,15 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {

public reset(): Promise<void> {
this.init();
this.broadcastWatches();
benjamn marked this conversation as resolved.
Show resolved Hide resolved

// Similar to what happens in the unsubscribe function returned by
// cache.watch, applied to all current watches.
this.watches.forEach(watch => this.maybeBroadcastWatch.forget(watch));
this.watches.clear();
forgetCache(this);

canonicalStringify.reset();

return Promise.resolve();
}

Expand Down