From 39778af3b39c2d5ac485d8fb3478035b511e4cc4 Mon Sep 17 00:00:00 2001 From: Florent Cailhol Date: Tue, 16 Oct 2018 09:26:19 +0200 Subject: [PATCH 1/3] Fix apollo-cache-inmemory in IE11 In IE11, Map.prototype.set is not spec-compliant and returns undefined. This commit removes the chained calls on Map to avoid the TypeError. --- packages/apollo-cache-inmemory/src/optimism.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/apollo-cache-inmemory/src/optimism.ts b/packages/apollo-cache-inmemory/src/optimism.ts index ae81d4581e1..15ee2b53a7a 100644 --- a/packages/apollo-cache-inmemory/src/optimism.ts +++ b/packages/apollo-cache-inmemory/src/optimism.ts @@ -1,7 +1,7 @@ declare function require(id: string): any; export type OptimisticWrapperFunction< - T = (...args: any[]) => any, + T = (...args: any[]) => any > = T & { // The .dirty(...) method of an optimistic function takes exactly the same // parameter types as the original function. @@ -14,7 +14,9 @@ export type OptimisticWrapOptions = { makeCacheKey?(...args: any[]): any; }; -const { wrap }: { +const { + wrap, +}: { wrap( originalFunction: T, options?: OptimisticWrapOptions, @@ -40,7 +42,11 @@ export class CacheKeyNode { } getOrCreate(value: any): CacheKeyNode { - const map = this.children || (this.children = new Map); - return map.get(value) || map.set(value, new CacheKeyNode()).get(value); + const map = this.children || (this.children = new Map()); + let node = map.get(value); + if (!node) { + map.set(value, node = new CacheKeyNode()); + } + return node; } } From 61262b3a94ecc21dfc9535e071a573b066eb5193 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 24 Oct 2018 09:04:34 -0400 Subject: [PATCH 2/3] Polyfill correct Map#set and Set#add return behavior. Implementing my own suggestion: https://github.com/apollographql/apollo-client/pull/4012#pullrequestreview-165182791 This change would be sufficient to fix #4024, though #4012 helps as well. --- .../apollo-cache-inmemory/src/fixPolyfills.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/apollo-cache-inmemory/src/fixPolyfills.ts b/packages/apollo-cache-inmemory/src/fixPolyfills.ts index ef4fb0f8aff..df4fb8a5d8b 100644 --- a/packages/apollo-cache-inmemory/src/fixPolyfills.ts +++ b/packages/apollo-cache-inmemory/src/fixPolyfills.ts @@ -1,6 +1,25 @@ -const frozen = {}; -const frozenTestMap = new Map(); +// Make sure Map.prototype.set returns the Map instance, per spec. +// https://github.com/apollographql/apollo-client/issues/4024 +const testMap = new Map(); +if (testMap.set(1, 2) !== testMap) { + const { set } = testMap; + Map.prototype.set = function (...args) { + set.apply(this, args); + return this; + }; +} +// Make sure Set.prototype.add returns the Set instance, per spec. +const testSet = new Set(); +if (testSet.add(3) !== testSet) { + const { add } = testSet; + Set.prototype.add = function (...args) { + add.apply(this, args); + return this; + }; +} + +const frozen = {}; if (typeof Object.freeze === 'function') { Object.freeze(frozen); } @@ -12,13 +31,13 @@ try { // objects before they become non-extensible: // https://github.com/facebook/react-native/blob/98a6f19d7c/Libraries/vendor/core/Map.js#L44-L50 // https://github.com/apollographql/react-apollo/issues/2442#issuecomment-426489517 - frozenTestMap.set(frozen, frozen).delete(frozen); + testMap.set(frozen, frozen).delete(frozen); } catch { const wrap = (method: (obj: T) => T): typeof method => { return method && (obj => { try { // If .set succeeds, also call .delete to avoid leaking memory. - frozenTestMap.set(obj, obj).delete(obj); + testMap.set(obj, obj).delete(obj); } finally { // If .set or .delete fails, the exception will be silently swallowed // by this return-from-finally statement: From f74e82dc51acd71e52e0c9951d63b71c0ee65e0c Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 24 Oct 2018 09:56:45 -0400 Subject: [PATCH 3/3] Bump apollo-cache-inmemory maxSize to make tests pass. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 375b035de28..63ee40be10b 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ { "name": "apollo-cache-inmemory", "path": "./packages/apollo-cache-inmemory/lib/bundle.min.js", - "maxSize": "6.75 kB" + "maxSize": "7 kB" }, { "name": "apollo-client",