Skip to content

Commit e3651ec

Browse files
committed
Use weakMap for private parts
1 parent 475ff2f commit e3651ec

File tree

5 files changed

+308
-190
lines changed

5 files changed

+308
-190
lines changed

src/cache/core/cache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
273273

274274
// Make sure we compute the same (===) fragment query document every
275275
// time we receive the same fragment in readFragment.
276-
private getFragmentDoc = wrap(getFragmentQueryDocument, {
276+
getFragmentDoc = wrap(getFragmentQueryDocument, {
277277
max:
278278
cacheSizes["cache.fragmentQueryDocuments"] ||
279279
defaultCacheSizes["cache.fragmentQueryDocuments"],

src/cache/inmemory/__tests__/cache.ts

+36-29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
DocumentNode,
1212
} from "../../../core";
1313
import { Cache } from "../../../cache";
14-
import { InMemoryCache } from "../inMemoryCache";
14+
import { $, InMemoryCache } from "../inMemoryCache";
1515
import { InMemoryCacheConfig } from "../types";
1616

1717
import { StoreReader } from "../readFromStore";
@@ -1498,13 +1498,15 @@ describe("Cache", () => {
14981498
}
14991499
`;
15001500

1501-
const originalReader = cache["storeReader"];
1501+
const privates = $(cache);
1502+
1503+
const originalReader = privates.storeReader;
15021504
expect(originalReader).toBeInstanceOf(StoreReader);
15031505

1504-
const originalWriter = cache["storeWriter"];
1506+
const originalWriter = privates.storeWriter;
15051507
expect(originalWriter).toBeInstanceOf(StoreWriter);
15061508

1507-
const originalMBW = cache["maybeBroadcastWatch"];
1509+
const originalMBW = privates.maybeBroadcastWatch;
15081510
expect(typeof originalMBW).toBe("function");
15091511

15101512
const originalCanon = originalReader.canon;
@@ -1534,12 +1536,12 @@ describe("Cache", () => {
15341536
c: "see",
15351537
});
15361538

1537-
expect(originalReader).not.toBe(cache["storeReader"]);
1538-
expect(originalWriter).not.toBe(cache["storeWriter"]);
1539-
expect(originalMBW).not.toBe(cache["maybeBroadcastWatch"]);
1539+
expect(originalReader).not.toBe(privates.storeReader);
1540+
expect(originalWriter).not.toBe(privates.storeWriter);
1541+
expect(originalMBW).not.toBe(privates.maybeBroadcastWatch);
15401542
// The cache.storeReader.canon is preserved by default, but can be dropped
15411543
// by passing resetResultIdentities:true to cache.gc.
1542-
expect(originalCanon).toBe(cache["storeReader"].canon);
1544+
expect(originalCanon).toBe(privates.storeReader.canon);
15431545
});
15441546
});
15451547

@@ -2122,10 +2124,11 @@ describe("Cache", () => {
21222124
describe("resultCacheMaxSize", () => {
21232125
it("uses default max size on caches if resultCacheMaxSize is not configured", () => {
21242126
const cache = new InMemoryCache();
2125-
expect(cache["maybeBroadcastWatch"].options.max).toBe(
2127+
const privates = $(cache);
2128+
expect(privates.maybeBroadcastWatch.options.max).toBe(
21262129
defaultCacheSizes["inMemoryCache.maybeBroadcastWatch"]
21272130
);
2128-
expect(cache["storeReader"]["executeSelectionSet"].options.max).toBe(
2131+
expect(privates.storeReader["executeSelectionSet"].options.max).toBe(
21292132
defaultCacheSizes["inMemoryCache.executeSelectionSet"]
21302133
);
21312134
expect(cache["getFragmentDoc"].options.max).toBe(
@@ -2136,8 +2139,9 @@ describe("resultCacheMaxSize", () => {
21362139
it("configures max size on caches when resultCacheMaxSize is set", () => {
21372140
const resultCacheMaxSize = 12345;
21382141
const cache = new InMemoryCache({ resultCacheMaxSize });
2139-
expect(cache["maybeBroadcastWatch"].options.max).toBe(resultCacheMaxSize);
2140-
expect(cache["storeReader"]["executeSelectionSet"].options.max).toBe(
2142+
const privates = $(cache);
2143+
expect(privates.maybeBroadcastWatch.options.max).toBe(resultCacheMaxSize);
2144+
expect(privates.storeReader["executeSelectionSet"].options.max).toBe(
21412145
resultCacheMaxSize
21422146
);
21432147
expect(cache["getFragmentDoc"].options.max).toBe(
@@ -2400,7 +2404,7 @@ describe("InMemoryCache#broadcastWatches", function () {
24002404
[canonicalCache, nonCanonicalCache].forEach((cache) => {
24012405
// Hack: delete every watch.lastDiff, so subsequent results will be
24022406
// broadcast, even though they are deeply equal to the previous results.
2403-
cache["watches"].forEach((watch) => {
2407+
$(cache)["watches"].forEach((watch) => {
24042408
delete watch.lastDiff;
24052409
});
24062410
});
@@ -3813,19 +3817,20 @@ describe("ReactiveVar and makeVar", () => {
38133817

38143818
expect(diffs.length).toBe(5);
38153819

3816-
expect(cache["watches"].size).toBe(5);
3820+
const watches = $(cache)["watches"];
3821+
expect(watches.size).toBe(5);
38173822
expect(spy).not.toBeCalled();
38183823

38193824
unwatchers.pop()!();
3820-
expect(cache["watches"].size).toBe(4);
3825+
expect(watches.size).toBe(4);
38213826
expect(spy).not.toBeCalled();
38223827

38233828
unwatchers.shift()!();
3824-
expect(cache["watches"].size).toBe(3);
3829+
expect(watches.size).toBe(3);
38253830
expect(spy).not.toBeCalled();
38263831

38273832
unwatchers.pop()!();
3828-
expect(cache["watches"].size).toBe(2);
3833+
expect(watches.size).toBe(2);
38293834
expect(spy).not.toBeCalled();
38303835

38313836
expect(diffs.length).toBe(5);
@@ -3835,7 +3840,7 @@ describe("ReactiveVar and makeVar", () => {
38353840
expect(unwatchers.length).toBe(3);
38363841
unwatchers.forEach((unwatch) => unwatch());
38373842

3838-
expect(cache["watches"].size).toBe(0);
3843+
expect(watches.size).toBe(0);
38393844
expect(spy).toBeCalledTimes(1);
38403845
expect(spy).toBeCalledWith(cache);
38413846
});
@@ -3865,7 +3870,8 @@ describe("ReactiveVar and makeVar", () => {
38653870
watch("a");
38663871
watch("d");
38673872

3868-
expect(cache["watches"].size).toBe(5);
3873+
const watches = $(cache)["watches"];
3874+
expect(watches.size).toBe(5);
38693875
expect(diffCounts).toEqual({
38703876
a: 2,
38713877
b: 1,
@@ -3875,7 +3881,7 @@ describe("ReactiveVar and makeVar", () => {
38753881

38763882
unwatchers.a.forEach((unwatch) => unwatch());
38773883
unwatchers.a.length = 0;
3878-
expect(cache["watches"].size).toBe(3);
3884+
expect(watches.size).toBe(3);
38793885

38803886
nameVar("Hugh");
38813887
expect(diffCounts).toEqual({
@@ -3886,7 +3892,7 @@ describe("ReactiveVar and makeVar", () => {
38863892
});
38873893

38883894
cache.reset({ discardWatches: true });
3889-
expect(cache["watches"].size).toBe(0);
3895+
expect(watches.size).toBe(0);
38903896

38913897
expect(diffCounts).toEqual({
38923898
a: 2,
@@ -3926,7 +3932,7 @@ describe("ReactiveVar and makeVar", () => {
39263932
});
39273933

39283934
nameVar("Trevor");
3929-
expect(cache["watches"].size).toBe(2);
3935+
expect(watches.size).toBe(2);
39303936
expect(diffCounts).toEqual({
39313937
a: 2,
39323938
b: 2,
@@ -3937,7 +3943,7 @@ describe("ReactiveVar and makeVar", () => {
39373943
});
39383944

39393945
cache.reset({ discardWatches: true });
3940-
expect(cache["watches"].size).toBe(0);
3946+
expect(watches.size).toBe(0);
39413947

39423948
nameVar("Danielle");
39433949
expect(diffCounts).toEqual({
@@ -3949,7 +3955,7 @@ describe("ReactiveVar and makeVar", () => {
39493955
f: 2,
39503956
});
39513957

3952-
expect(cache["watches"].size).toBe(0);
3958+
expect(watches.size).toBe(0);
39533959
});
39543960

39553961
it("should recall forgotten vars once cache has watches again", () => {
@@ -3974,22 +3980,23 @@ describe("ReactiveVar and makeVar", () => {
39743980
expect(diffs.length).toBe(3);
39753981
expect(names()).toEqual(["Ben", "Ben", "Ben"]);
39763982

3977-
expect(cache["watches"].size).toBe(3);
3983+
const watches = $(cache)["watches"];
3984+
expect(watches.size).toBe(3);
39783985
expect(spy).not.toBeCalled();
39793986

39803987
unwatchers.pop()!();
3981-
expect(cache["watches"].size).toBe(2);
3988+
expect(watches.size).toBe(2);
39823989
expect(spy).not.toBeCalled();
39833990

39843991
unwatchers.shift()!();
3985-
expect(cache["watches"].size).toBe(1);
3992+
expect(watches.size).toBe(1);
39863993
expect(spy).not.toBeCalled();
39873994

39883995
nameVar("Hugh");
39893996
expect(names()).toEqual(["Ben", "Ben", "Ben", "Hugh"]);
39903997

39913998
unwatchers.pop()!();
3992-
expect(cache["watches"].size).toBe(0);
3999+
expect(watches.size).toBe(0);
39934000
expect(spy).toBeCalledTimes(1);
39944001
expect(spy).toBeCalledWith(cache);
39954002

@@ -3999,7 +4006,7 @@ describe("ReactiveVar and makeVar", () => {
39994006

40004007
// Call watch(false) to avoid immediate delivery of the "ignored" name.
40014008
unwatchers.push(watch(false));
4002-
expect(cache["watches"].size).toBe(1);
4009+
expect(watches.size).toBe(1);
40034010
expect(names()).toEqual(["Ben", "Ben", "Ben", "Hugh"]);
40044011

40054012
// This is the test that would fail if cache.watch did not call

0 commit comments

Comments
 (0)