diff --git a/packages/apollo-cache-inmemory/src/fixPolyfills.ts b/packages/apollo-cache-inmemory/src/fixPolyfills.ts index ef4fb0f8aff..14fa77a2a2d 100644 --- a/packages/apollo-cache-inmemory/src/fixPolyfills.ts +++ b/packages/apollo-cache-inmemory/src/fixPolyfills.ts @@ -1,6 +1,23 @@ -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) { + return set.apply(this, args) || 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) { + return add.apply(this, args) || this; + }; +} + +const frozen = {}; if (typeof Object.freeze === 'function') { Object.freeze(frozen); } @@ -12,13 +29,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: