-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Remove fixPolyfills.ts, except when bundling for React Native. #5962
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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); | ||
} | ||
|
||
try { | ||
// If non-extensible objects can't be stored as keys in a Map, make sure we | ||
// do not freeze/seal/etc. an object without first attempting to put it in a | ||
// Map. For example, this gives the React Native Map polyfill a chance to tag | ||
// 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 | ||
testMap.set(frozen, frozen).delete(frozen); | ||
} catch { | ||
const wrap = (method: <T>(obj: T) => T): typeof method => { | ||
return method && (obj => { | ||
try { | ||
// If .set succeeds, also call .delete to avoid leaking memory. | ||
testMap.set(obj, obj).delete(obj); | ||
} finally { | ||
// If .set or .delete fails, the exception will be silently swallowed | ||
// by this return-from-finally statement: | ||
return method.call(Object, obj); | ||
} | ||
}); | ||
}; | ||
Object.freeze = wrap(Object.freeze); | ||
Object.seal = wrap(Object.seal); | ||
Object.preventExtensions = wrap(Object.preventExtensions); | ||
} | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,7 @@ | ||
// 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); | ||
} | ||
|
||
try { | ||
// If non-extensible objects can't be stored as keys in a Map, make sure we | ||
// do not freeze/seal/etc. an object without first attempting to put it in a | ||
// Map. For example, this gives the React Native Map polyfill a chance to tag | ||
// 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 | ||
testMap.set(frozen, frozen).delete(frozen); | ||
} catch { | ||
const wrap = (method: <T>(obj: T) => T): typeof method => { | ||
return method && (obj => { | ||
try { | ||
// If .set succeeds, also call .delete to avoid leaking memory. | ||
testMap.set(obj, obj).delete(obj); | ||
} finally { | ||
// If .set or .delete fails, the exception will be silently swallowed | ||
// by this return-from-finally statement: | ||
return method.call(Object, obj); | ||
} | ||
}); | ||
}; | ||
Object.freeze = wrap(Object.freeze); | ||
Object.seal = wrap(Object.seal); | ||
Object.preventExtensions = wrap(Object.preventExtensions); | ||
} | ||
|
||
export {} | ||
// Most JavaScript environments do not need the workarounds implemented in | ||
// fixPolyfills.native.ts, so importing fixPolyfills.ts merely imports | ||
// this empty module, adding nothing to bundle sizes or execution times. | ||
// When bundling for React Native, we substitute fixPolyfills.native.js | ||
// for fixPolyfills.js (see the "react-native" section of package.json), | ||
// to work around problems with Map and Set polyfills in older versions of | ||
// React Native (which should have been fixed in [email protected]). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These shims are not specific to React Native, but we also do not rely on the return values of
Map.prototype.set
orSet.prototype.add
anywhere in the Apollo Client codebase anymore. I'm leaving them here so folks can easily find and copy the code if they need it.