google-map-react/develop/utils/withSafeInterval.js
Michael Salaverry 3d78dd9cb4 Fix: Update heatmap layer when heatmap positions prop changes (#728)
* improvement: switched from defining API key in multiple places to one place

* now generating random readings

* fix: a change in heatmap positions now updates the map accordingly

* docs: added comment to explain that the developer should use their own key
2019-04-01 12:47:19 -03:00

49 lines
1.0 KiB
JavaScript

import { createElement, Component } from 'react';
const safeTimerFactory = (setFn, clearFn, propName, hocName) =>
Target => {
class SafeTimer extends Component {
constructor(props, context) {
super(props, context);
this.unsubscribers = [];
this[propName] = this[propName].bind(this);
}
componentWillUnmount() {
this.unsubscribers.forEach(unsubscribe => unsubscribe());
this.unsubscribers = [];
}
[propName](...args) {
const id = setFn(...args);
const unsubscriber = () => clearFn(id);
this.unsubscribers.push(unsubscriber);
return unsubscriber;
}
render() {
return createElement(Target, {
...this.props,
[propName]: this[propName],
});
}
}
if (process.env.NODE_ENV !== 'production') {
SafeTimer.displayName = `${hocName}`;
}
return SafeTimer;
};
export default safeTimerFactory(
global.setInterval,
global.clearInterval,
'setSafeInterval',
'withSafeInterval'
);