Manually bind methods (#463)

This commit is contained in:
Balthazar Gronon 2018-03-08 11:39:19 -08:00 committed by Xiaoji Chen
parent 7959af17fa
commit f420faab64
7 changed files with 36 additions and 34 deletions

View File

@ -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() {

View File

@ -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) {

View File

@ -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() {

View File

@ -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() {

View File

@ -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
};

View File

@ -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() {

View File

@ -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);
}
}
}
}