Skip to content

Commit 7a6fb5e

Browse files
committed
Bind methods in the class body instead of ctor
1 parent 3cc5817 commit 7a6fb5e

File tree

16 files changed

+76
-167
lines changed

16 files changed

+76
-167
lines changed

src/components/SearchTracePage/SearchDropdownInput.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default class SearchDropdownInput extends Component {
3737
this.state = {
3838
currentItems: props.items.slice(0, props.maxResults),
3939
};
40-
this.onSearch = this.onSearch.bind(this);
4140
}
4241
componentWillReceiveProps(nextProps) {
4342
if (this.props.items.map(i => i.text).join(',') !== nextProps.items.map(i => i.text).join(',')) {
@@ -46,12 +45,12 @@ export default class SearchDropdownInput extends Component {
4645
});
4746
}
4847
}
49-
onSearch(_, searchText) {
48+
onSearch = (_, searchText) => {
5049
const { items, maxResults } = this.props;
5150
const regexStr = regexpEscape(searchText);
5251
const regex = new RegExp(regexStr, 'i');
5352
return items.filter(v => regex.test(v.text)).slice(0, maxResults);
54-
}
53+
};
5554
render() {
5655
const { input: { value, onChange } } = this.props;
5756
const { currentItems } = this.state;

src/components/TracePage/ScrollManager.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export type Accessors = {
4646

4747
interface Scroller {
4848
scrollTo: number => void,
49-
scrollBy: number => void,
49+
scrollBy: (number, ?boolean) => void,
5050
}
5151

5252
/**
@@ -100,12 +100,6 @@ export default class ScrollManager {
100100
this._trace = trace;
101101
this._scroller = scroller;
102102
this._accessors = undefined;
103-
104-
this.scrollToNextVisibleSpan = this.scrollToNextVisibleSpan.bind(this);
105-
this.scrollToPrevVisibleSpan = this.scrollToPrevVisibleSpan.bind(this);
106-
this.scrollPageDown = this.scrollPageDown.bind(this);
107-
this.scrollPageUp = this.scrollPageUp.bind(this);
108-
this.setAccessors = this.setAccessors.bind(this);
109103
}
110104

111105
_scrollPast(rowIndex: number, direction: 1 | -1) {
@@ -205,15 +199,15 @@ export default class ScrollManager {
205199
* `setAccessors` is bound in the ctor, so it can be passed as a prop to
206200
* children components.
207201
*/
208-
setAccessors = function setAccessors(accessors: Accessors) {
202+
setAccessors = (accessors: Accessors) => {
209203
this._accessors = accessors;
210204
};
211205

212206
/**
213207
* Scrolls around one page down (0.95x). It is bounds in the ctor, so it can
214208
* be used as a keyboard shortcut handler.
215209
*/
216-
scrollPageDown = function scrollPageDown() {
210+
scrollPageDown = () => {
217211
if (!this._scroller || !this._accessors) {
218212
return;
219213
}
@@ -224,7 +218,7 @@ export default class ScrollManager {
224218
* Scrolls around one page up (0.95x). It is bounds in the ctor, so it can
225219
* be used as a keyboard shortcut handler.
226220
*/
227-
scrollPageUp = function scrollPageUp() {
221+
scrollPageUp = () => {
228222
if (!this._scroller || !this._accessors) {
229223
return;
230224
}
@@ -236,7 +230,7 @@ export default class ScrollManager {
236230
* text filter, if there is one. It is bounds in the ctor, so it can
237231
* be used as a keyboard shortcut handler.
238232
*/
239-
scrollToNextVisibleSpan = function scrollToNextVisibleSpan() {
233+
scrollToNextVisibleSpan = () => {
240234
this._scrollToVisibleSpan(1);
241235
};
242236

@@ -245,7 +239,7 @@ export default class ScrollManager {
245239
* text filter, if there is one. It is bounds in the ctor, so it can
246240
* be used as a keyboard shortcut handler.
247241
*/
248-
scrollToPrevVisibleSpan = function scrollToPrevVisibleSpan() {
242+
scrollToPrevVisibleSpan = () => {
249243
this._scrollToVisibleSpan(-1);
250244
};
251245

src/components/TracePage/SpanGraph/CanvasSpanGraph.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default class CanvasSpanGraph extends React.PureComponent<CanvasSpanGraph
4141
constructor(props: CanvasSpanGraphProps) {
4242
super(props);
4343
this._canvasElm = undefined;
44-
this._setCanvasRef = this._setCanvasRef.bind(this);
4544
}
4645

4746
componentDidMount() {
@@ -52,7 +51,7 @@ export default class CanvasSpanGraph extends React.PureComponent<CanvasSpanGraph
5251
this._draw();
5352
}
5453

55-
_setCanvasRef = function _setCanvasRef(elm: React.Node) {
54+
_setCanvasRef = (elm: ?HTMLCanvasElement) => {
5655
this._canvasElm = elm;
5756
};
5857

src/components/TracePage/SpanGraph/ViewingLayer.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ export default class ViewingLayer extends React.PureComponent<ViewingLayerProps,
110110

111111
constructor(props: ViewingLayerProps) {
112112
super(props);
113-
this._setRoot = this._setRoot.bind(this);
114-
this._getDraggingBounds = this._getDraggingBounds.bind(this);
115-
this._handleReframeMouseMove = this._handleReframeMouseMove.bind(this);
116-
this._handleReframeMouseLeave = this._handleReframeMouseLeave.bind(this);
117-
this._handleReframeDragUpdate = this._handleReframeDragUpdate.bind(this);
118-
this._handleReframeDragEnd = this._handleReframeDragEnd.bind(this);
119-
this._handleScrubberEnterLeave = this._handleScrubberEnterLeave.bind(this);
120-
this._handleScrubberDragUpdate = this._handleScrubberDragUpdate.bind(this);
121-
this._handleScrubberDragEnd = this._handleScrubberDragEnd.bind(this);
122113

123114
this._draggerReframe = new DraggableManager({
124115
getBounds: this._getDraggingBounds,
@@ -162,11 +153,11 @@ export default class ViewingLayer extends React.PureComponent<ViewingLayerProps,
162153
this._draggerStart.dispose();
163154
}
164155

165-
_setRoot = function _setRoot(elm: ?Element) {
156+
_setRoot = (elm: ?Element) => {
166157
this._root = elm;
167158
};
168159

169-
_getDraggingBounds = function _getDraggingBounds(tag: ?string): DraggableBounds {
160+
_getDraggingBounds = (tag: ?string): DraggableBounds => {
170161
if (!this._root) {
171162
throw new Error('invalid state');
172163
}
@@ -182,60 +173,56 @@ export default class ViewingLayer extends React.PureComponent<ViewingLayerProps,
182173
return { clientXLeft, maxValue, minValue, width };
183174
};
184175

185-
_handleReframeMouseMove = function _handleReframeMouseMove({ value }: DraggingUpdate) {
176+
_handleReframeMouseMove = ({ value }: DraggingUpdate) => {
186177
this.props.updateNextViewRangeTime({ cursor: value });
187178
};
188179

189-
_handleReframeMouseLeave = function _handleReframeMouseLeave() {
180+
_handleReframeMouseLeave = () => {
190181
this.props.updateNextViewRangeTime({ cursor: null });
191182
};
192183

193-
_handleReframeDragUpdate = function _handleReframeDragUpdate({ value }: DraggingUpdate) {
184+
_handleReframeDragUpdate = ({ value }: DraggingUpdate) => {
194185
const shift = value;
195186
const { time } = this.props.viewRange;
196187
const anchor = time.reframe ? time.reframe.anchor : shift;
197188
const update = { reframe: { anchor, shift } };
198189
this.props.updateNextViewRangeTime(update);
199190
};
200191

201-
_handleReframeDragEnd = function _handleReframeDragEnd({ manager, value }: DraggingUpdate) {
192+
_handleReframeDragEnd = ({ manager, value }: DraggingUpdate) => {
202193
const { time } = this.props.viewRange;
203194
const anchor = time.reframe ? time.reframe.anchor : value;
204195
const [start, end] = value < anchor ? [value, anchor] : [anchor, value];
205196
manager.resetBounds();
206197
this.props.updateViewRangeTime(start, end);
207198
};
208199

209-
_handleScrubberEnterLeave = function _handleScrubberEnterLeave({ type }: DraggingUpdate) {
200+
_handleScrubberEnterLeave = ({ type }: DraggingUpdate) => {
210201
const preventCursorLine = type === updateTypes.MOUSE_ENTER;
211202
this.setState({ preventCursorLine });
212203
};
213204

214-
_handleScrubberDragUpdate = function _handleScrubberDragUpdate({
215-
event,
216-
tag,
217-
type,
218-
value,
219-
}: DraggingUpdate) {
205+
_handleScrubberDragUpdate = ({ event, tag, type, value }: DraggingUpdate) => {
220206
if (type === updateTypes.DRAG_START) {
221207
event.stopPropagation();
222208
}
223-
const update = {};
224209
if (tag === dragTypes.SHIFT_START) {
225-
update.shiftStart = value;
210+
this.props.updateNextViewRangeTime({ shiftStart: value });
226211
} else if (tag === dragTypes.SHIFT_END) {
227-
update.shiftEnd = value;
212+
this.props.updateNextViewRangeTime({ shiftEnd: value });
228213
}
229-
this.props.updateNextViewRangeTime(update);
230214
};
231215

232-
_handleScrubberDragEnd = function _handleScrubberDragEnd({ manager, tag, value }: DraggingUpdate) {
216+
_handleScrubberDragEnd = ({ manager, tag, value }: DraggingUpdate) => {
233217
const [viewStart, viewEnd] = this.props.viewRange.time.current;
234218
let update: [number, number];
235219
if (tag === dragTypes.SHIFT_START) {
236220
update = [value, viewEnd];
237221
} else if (tag === dragTypes.SHIFT_END) {
238222
update = [viewStart, value];
223+
} else {
224+
// to satisfy flow
225+
throw new Error('bad state');
239226
}
240227
manager.resetBounds();
241228
this.setState({ preventCursorLine: false });

src/components/TracePage/TraceTimelineViewer/ListView/index.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,6 @@ export default class ListView extends React.Component<ListViewProps> {
178178
constructor(props: ListViewProps) {
179179
super(props);
180180

181-
this.getViewHeight = this.getViewHeight.bind(this);
182-
this.getBottomVisibleIndex = this.getBottomVisibleIndex.bind(this);
183-
this.getTopVisibleIndex = this.getTopVisibleIndex.bind(this);
184-
this.getRowPosition = this.getRowPosition.bind(this);
185-
this._getHeight = this._getHeight.bind(this);
186-
this._scanItemHeights = this._scanItemHeights.bind(this);
187-
this._onScroll = this._onScroll.bind(this);
188-
this._positionList = this._positionList.bind(this);
189-
this._initWrapper = this._initWrapper.bind(this);
190-
this._initItemHolder = this._initItemHolder.bind(this);
191-
192181
this._yPositions = new Positions(200);
193182
// _knownHeights is (item-key -> observed height) of list items
194183
this._knownHeights = new Map();
@@ -232,34 +221,29 @@ export default class ListView extends React.Component<ListViewProps> {
232221
}
233222
}
234223

235-
getViewHeight = function getViewHeight(): number {
236-
return this._viewHeight;
237-
};
224+
getViewHeight = () => this._viewHeight;
238225

239226
/**
240227
* Get the index of the item at the bottom of the current view.
241228
*/
242-
getBottomVisibleIndex = function getBottomVisibleIndex(): number {
229+
getBottomVisibleIndex = (): number => {
243230
const bottomY = this._scrollTop + this._viewHeight;
244-
return this._yPositions.findFloorIndex(bottomY);
231+
return this._yPositions.findFloorIndex(bottomY, this._getHeight);
245232
};
246233

247234
/**
248235
* Get the index of the item at the top of the current view.
249236
*/
250-
getTopVisibleIndex = function getTopVisibleIndex(): number {
251-
return this._yPositions.findFloorIndex(this._scrollTop, this._getHeight);
252-
};
237+
getTopVisibleIndex = (): number => this._yPositions.findFloorIndex(this._scrollTop, this._getHeight);
253238

254-
getRowPosition = function getRowPosition(index: number): { height: number, y: number } {
255-
return this._yPositions.getRowPosition(index, this._getHeight);
256-
};
239+
getRowPosition = (index: number): { height: number, y: number } =>
240+
this._yPositions.getRowPosition(index, this._getHeight);
257241

258242
/**
259243
* Scroll event listener that schedules a remeasuring of which items should be
260244
* rendered.
261245
*/
262-
_onScroll = function _onScroll() {
246+
_onScroll = () => {
263247
if (!this._isScrolledOrResized) {
264248
this._isScrolledOrResized = true;
265249
window.requestAnimationFrame(this._positionList);
@@ -309,7 +293,7 @@ export default class ListView extends React.Component<ListViewProps> {
309293
* Checked to see if the currently rendered items are sufficient, if not,
310294
* force an update to trigger more items to be rendered.
311295
*/
312-
_positionList = function _positionList() {
296+
_positionList = () => {
313297
this._isScrolledOrResized = false;
314298
if (!this._wrapperElm) {
315299
return;
@@ -329,14 +313,14 @@ export default class ListView extends React.Component<ListViewProps> {
329313
}
330314
};
331315

332-
_initWrapper = function _initWrapper(elm: HTMLElement) {
316+
_initWrapper = (elm: HTMLElement) => {
333317
this._wrapperElm = elm;
334318
if (!this.props.windowScroller) {
335319
this._viewHeight = elm && elm.clientHeight;
336320
}
337321
};
338322

339-
_initItemHolder = function _initItemHolder(elm: HTMLElement) {
323+
_initItemHolder = (elm: HTMLElement) => {
340324
this._itemHolderElm = elm;
341325
this._scanItemHeights();
342326
};
@@ -346,7 +330,7 @@ export default class ListView extends React.Component<ListViewProps> {
346330
* item-key (which is on a data-* attribute). If any new or adjusted heights
347331
* are found, re-measure the current known y-positions (via .yPositions).
348332
*/
349-
_scanItemHeights = function _scanItemHeights() {
333+
_scanItemHeights = () => {
350334
const getIndexFromKey = this.props.getIndexFromKey;
351335
if (!this._itemHolderElm) {
352336
return;
@@ -399,7 +383,7 @@ export default class ListView extends React.Component<ListViewProps> {
399383
* Get the height of the element at index `i`; first check the known heigths,
400384
* fallbck to `.props.itemHeightGetter(...)`.
401385
*/
402-
_getHeight = function _getHeight(i: number) {
386+
_getHeight = (i: number) => {
403387
const key = this.props.getKeyFromIndex(i);
404388
const known = this._knownHeights.get(key);
405389
// known !== known iff known is NaN

src/components/TracePage/TraceTimelineViewer/SpanBarRow.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,11 @@ export default class SpanBarRow extends React.PureComponent<SpanBarRowProps> {
7373
rpc: null,
7474
};
7575

76-
constructor(props: SpanBarRowProps) {
77-
super(props);
78-
this._detailToggle = this._detailToggle.bind(this);
79-
this._childrenToggle = this._childrenToggle.bind(this);
80-
}
81-
82-
_detailToggle = function _detailToggle() {
76+
_detailToggle = () => {
8377
this.props.onDetailToggled(this.props.spanID);
8478
};
8579

86-
_childrenToggle = function _childrenToggle() {
80+
_childrenToggle = () => {
8781
this.props.onChildrenToggled(this.props.spanID);
8882
};
8983

src/components/TracePage/TraceTimelineViewer/SpanDetailRow.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ type SpanDetailRowProps = {
4747
export default class SpanDetailRow extends React.PureComponent<SpanDetailRowProps> {
4848
props: SpanDetailRowProps;
4949

50-
constructor(props: SpanDetailRowProps) {
51-
super(props);
52-
this._detailToggle = this._detailToggle.bind(this);
53-
}
54-
55-
_detailToggle = function _detailToggle() {
50+
_detailToggle = () => {
5651
this.props.onDetailToggled(this.props.span.spanID);
5752
};
5853

0 commit comments

Comments
 (0)