Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// The following code patches google.maps API since in `onRemove` they don't check for null
// sometimes this conflicts with the way React handle DOM.
const PATCH_ONREMOVE = Symbol.for('Patch onRemove');
function waitForGoogleThenPatch() {
/* global google */
if (typeof google === 'undefined') {
window.requestAnimationFrame(waitForGoogleThenPatch);
} else {
const Marker = google.maps.Marker;
if (Marker[PATCH_ONREMOVE]) return;
Marker[PATCH_ONREMOVE] = true;
const patch = (onRemove) => {
return function onRemovePatched(...args) {
let temp = document.createElement('div');
if (!this.labelDiv_.parentNode) {
temp.appendChild(this.labelDiv_);
}
if (!this.eventDiv_.parentNode) {
temp.appendChild(this.eventDiv_);
}
if (!this.listeners_) {
this.listeners_ = [];
}
onRemove.call(this, ...args);
temp = null;
};
};
Marker.prototype.setMap = ((setMap) => {
return function setMapPatched(...args) {
if (this.label) {
const proto = Object.getPrototypeOf(this.label);
if (!proto[PATCH_ONREMOVE]) {
proto[PATCH_ONREMOVE] = true;
proto.onRemove = patch(proto.onRemove);
}
}
setMap.call(this, ...args);
};
})(Marker.prototype.setMap);
}
}
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
window.requestAnimationFrame(waitForGoogleThenPatch);
}
/// --- End of patch ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.