From f420faab64c08c65a97be57c8f7e1e2bcece6603 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon <6033345+balthazar@users.noreply.github.com> Date: Thu, 8 Mar 2018 11:39:19 -0800 Subject: [PATCH] Manually bind methods (#463) --- src/components/interactive-map.js | 13 +++++++++++-- src/components/navigation-control.js | 9 +++++++-- src/components/popup.js | 8 ++++++-- src/components/static-map.js | 11 +++++++++-- src/index.js | 4 +--- src/overlays/canvas-overlay.js | 4 ++-- src/utils/autobind.js | 21 --------------------- 7 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 src/utils/autobind.js diff --git a/src/components/interactive-map.js b/src/components/interactive-map.js index 210d16cf..1050812f 100644 --- a/src/components/interactive-map.js +++ b/src/components/interactive-map.js @@ -1,6 +1,5 @@ import {PureComponent, createElement} from 'react'; import PropTypes from 'prop-types'; -import autobind from '../utils/autobind'; import StaticMap from './static-map'; import {MAPBOX_LIMITS} from '../utils/map-state'; @@ -151,7 +150,6 @@ export default class InteractiveMap extends PureComponent { constructor(props) { super(props); - autobind(this); // Check for deprecated props deprecateWarn(props); @@ -167,6 +165,17 @@ export default class InteractiveMap extends PureComponent { this._mapControls = props.mapControls || new MapControls(); this._eventManager = new EventManager(null, {rightButton: true}); + + this.getMap = this.getMap.bind(this); + this.queryRenderedFeatures = this.queryRenderedFeatures.bind(this); + this._checkVisibilityConstraints = this._checkVisibilityConstraints.bind(this); + this._getFeatures = this._getFeatures.bind(this); + this._onInteractiveStateChange = this._onInteractiveStateChange.bind(this); + this._getPos = this._getPos.bind(this); + this._onMouseMove = this._onMouseMove.bind(this); + this._onMouseClick = this._onMouseClick.bind(this); + this._eventCanvasLoaded = this._eventCanvasLoaded.bind(this); + this._staticMapLoaded = this._staticMapLoaded.bind(this); } getChildContext() { diff --git a/src/components/navigation-control.js b/src/components/navigation-control.js index 108db9b5..81a4905d 100644 --- a/src/components/navigation-control.js +++ b/src/components/navigation-control.js @@ -1,7 +1,6 @@ import {createElement} from 'react'; import PropTypes from 'prop-types'; import BaseControl from './base-control'; -import autobind from '../utils/autobind'; import MapState from '../utils/map-state'; import TransitionManager from '../utils/transition-manager'; @@ -42,9 +41,15 @@ export default class NavigationControl extends BaseControl { constructor(props) { super(props); - autobind(this); // Check for deprecated props deprecateWarn(props); + + this._updateViewport = this._updateViewport.bind(this); + this._onZoomIn = this._onZoomIn.bind(this); + this._onZoomOut = this._onZoomOut.bind(this); + this._onResetNorth = this._onResetNorth.bind(this); + this._renderCompass = this._renderCompass.bind(this); + this._renderButton = this._renderButton.bind(this); } shouldComponentUpdate(nextProps, nextState, nextContext) { diff --git a/src/components/popup.js b/src/components/popup.js index f3d10e45..168b3642 100644 --- a/src/components/popup.js +++ b/src/components/popup.js @@ -20,7 +20,6 @@ import {createElement} from 'react'; import PropTypes from 'prop-types'; import BaseControl from './base-control'; -import autobind from '../utils/autobind'; import {getDynamicPosition, ANCHOR_POSITION} from '../utils/dynamic-position'; @@ -72,7 +71,12 @@ export default class Popup extends BaseControl { constructor(props) { super(props); - autobind(this); + + this._getPosition = this._getPosition.bind(this); + this._onClose = this._onClose.bind(this); + this._contentLoaded = this._contentLoaded.bind(this); + this._renderTip = this._renderTip.bind(this); + this._renderContent = this._renderContent.bind(this); } componentDidMount() { diff --git a/src/components/static-map.js b/src/components/static-map.js index 7e6db7c3..080e12d1 100644 --- a/src/components/static-map.js +++ b/src/components/static-map.js @@ -19,7 +19,6 @@ // THE SOFTWARE. import {PureComponent, createElement} from 'react'; import PropTypes from 'prop-types'; -import autobind from '../utils/autobind'; import {getInteractiveLayerIds, setDiffStyle} from '../utils/style-utils'; import isImmutableMap from '../utils/is-immutable-map'; @@ -76,7 +75,15 @@ export default class StaticMap extends PureComponent { this.state = { accessTokenInvalid: false }; - autobind(this); + + this.getMap = this.getMap.bind(this); + this.queryRenderedFeatures = this.queryRenderedFeatures.bind(this); + this._updateQueryParams = this._updateQueryParams.bind(this); + this._updateMapSize = this._updateMapSize.bind(this); + this._updateMapStyle = this._updateMapStyle.bind(this); + this._mapboxMapLoaded = this._mapboxMapLoaded.bind(this); + this._mapboxMapError = this._mapboxMapError.bind(this); + this._renderNoTokenWarning = this._renderNoTokenWarning.bind(this); } getChildContext() { diff --git a/src/index.js b/src/index.js index 6eed7204..19e808c3 100644 --- a/src/index.js +++ b/src/index.js @@ -45,9 +45,7 @@ export { // Experimental Features (May change in minor version bumps, use at your own risk) import MapControls from './utils/map-controls'; -import autobind from './utils/autobind'; export const experimental = { - MapControls, - autobind + MapControls }; diff --git a/src/overlays/canvas-overlay.js b/src/overlays/canvas-overlay.js index d7af9d5c..f8e7aac1 100644 --- a/src/overlays/canvas-overlay.js +++ b/src/overlays/canvas-overlay.js @@ -22,7 +22,6 @@ import {createElement} from 'react'; import PropTypes from 'prop-types'; import BaseControl from '../components/base-control'; import {window} from '../utils/globals'; -import autobind from '../utils/autobind'; const propTypes = Object.assign({}, BaseControl.propTypes, { redraw: PropTypes.func.isRequired @@ -38,7 +37,8 @@ const defaultProps = { export default class CanvasOverlay extends BaseControl { constructor(props) { super(props); - autobind(this); + this._redraw = this._redraw.bind(this); + this._canvasLoaded = this._canvasLoaded.bind(this); } componentDidMount() { diff --git a/src/utils/autobind.js b/src/utils/autobind.js deleted file mode 100644 index b81a4bcc..00000000 --- a/src/utils/autobind.js +++ /dev/null @@ -1,21 +0,0 @@ -const PREDEFINED = [ - 'constructor', 'render', 'componentWillMount', 'componentDidMount', - 'componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', - 'componentDidUpdate', 'componentWillUnmount' -]; - -/** - * Binds the "this" argument of all functions on a class instance to the instance - * @param {Object} obj - class instance (typically a react component) - */ -export default function autobind(obj) { - const proto = Object.getPrototypeOf(obj); - const propNames = Object.getOwnPropertyNames(proto); - for (const key of propNames) { - if (typeof obj[key] === 'function') { - if (!PREDEFINED.find(name => key === name)) { - obj[key] = obj[key].bind(obj); - } - } - } -}