Skip to content

Instantly share code, notes, and snippets.

@kentaromiura
Created March 16, 2018 08:17
Show Gist options
  • Save kentaromiura/6bb9d0061c92087792f289dfde4989cd to your computer and use it in GitHub Desktop.
Save kentaromiura/6bb9d0061c92087792f289dfde4989cd to your computer and use it in GitHub Desktop.
// 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