Skip to content

Commit 199cb5e

Browse files
committed
Add memoization hooks
1 parent 804336e commit 199cb5e

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

src/commons/sideContent/content/SideContentCseMachine.tsx

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { bindActionCreators } from '@reduxjs/toolkit';
1414
import classNames from 'classnames';
1515
import { Chapter } from 'js-slang/dist/types';
1616
import { debounce } from 'lodash';
17-
import React, { useCallback, useEffect, useState } from 'react';
17+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
1818
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux';
1919
import HotKeys from 'src/commons/hotkeys/HotKeys';
2020
import { Output } from 'src/commons/repl/Repl';
@@ -159,45 +159,54 @@ const SideContentCseMachineBase: React.FC<Props> = props => {
159159
}
160160
}, [props.needCseUpdate, isJava]);
161161

162-
const sliderRelease = (newValue: number) => {
163-
setLastStep(newValue === props.stepsTotal);
164-
props.handleEditorEval();
165-
};
162+
const sliderRelease = useCallback(
163+
(newValue: number) => {
164+
setLastStep(newValue === props.stepsTotal);
165+
props.handleEditorEval();
166+
},
167+
[props]
168+
);
166169

167-
const sliderShift = (newValue: number) => {
168-
props.handleStepUpdate(newValue);
169-
setValue(newValue);
170-
};
170+
const sliderShift = useCallback(
171+
(newValue: number) => {
172+
props.handleStepUpdate(newValue);
173+
setValue(newValue);
174+
},
175+
[props]
176+
);
171177

172-
const stepPrevious = () => {
178+
const stepPrevious = useCallback(() => {
173179
if (value !== 0) {
174180
sliderShift(value - 1);
175181
sliderRelease(value - 1);
176182
}
177-
};
183+
}, [value, sliderShift, sliderRelease]);
178184

179-
const stepNext = () => {
185+
const stepNext = useCallback(() => {
180186
const lastStepValue = props.stepsTotal;
181187
if (value !== lastStepValue) {
182188
sliderShift(value + 1);
183189
sliderRelease(value + 1);
184190
CseAnimation.enableAnimations();
185191
}
186-
};
192+
}, [value, props.stepsTotal, sliderShift, sliderRelease]);
187193

188-
const stepFirst = () => {
194+
const stepFirst = useCallback(() => {
189195
// Move to the first step
190196
sliderShift(0);
191197
sliderRelease(0);
192-
};
198+
}, [sliderShift, sliderRelease]);
193199

194-
const stepLast = (lastStepValue: number) => {
195-
// Move to the last step
196-
sliderShift(lastStepValue);
197-
sliderRelease(lastStepValue);
198-
};
200+
const stepLast = useCallback(
201+
(lastStepValue: number) => {
202+
// Move to the last step
203+
sliderShift(lastStepValue);
204+
sliderRelease(lastStepValue);
205+
},
206+
[sliderShift, sliderRelease]
207+
);
199208

200-
const stepNextBreakpoint = () => {
209+
const stepNextBreakpoint = useCallback(() => {
201210
for (const step of props.breakpointSteps) {
202211
if (step > value) {
203212
sliderShift(step);
@@ -207,9 +216,9 @@ const SideContentCseMachineBase: React.FC<Props> = props => {
207216
}
208217
sliderShift(props.stepsTotal);
209218
sliderRelease(props.stepsTotal);
210-
};
219+
}, [props.breakpointSteps, props.stepsTotal, value, sliderShift, sliderRelease]);
211220

212-
const stepPrevBreakpoint = () => {
221+
const stepPrevBreakpoint = useCallback(() => {
213222
for (let i = props.breakpointSteps.length - 1; i >= 0; i--) {
214223
const step = props.breakpointSteps[i];
215224
if (step < value) {
@@ -220,9 +229,9 @@ const SideContentCseMachineBase: React.FC<Props> = props => {
220229
}
221230
sliderShift(0);
222231
sliderRelease(0);
223-
};
232+
}, [props.breakpointSteps, value, sliderShift, sliderRelease]);
224233

225-
const stepNextChangepoint = () => {
234+
const stepNextChangepoint = useCallback(() => {
226235
for (const step of props.changepointSteps) {
227236
if (step > value) {
228237
sliderShift(step);
@@ -232,9 +241,9 @@ const SideContentCseMachineBase: React.FC<Props> = props => {
232241
}
233242
sliderShift(props.stepsTotal);
234243
sliderRelease(props.stepsTotal);
235-
};
244+
}, [props.changepointSteps, props.stepsTotal, value, sliderShift, sliderRelease]);
236245

237-
const stepPrevChangepoint = () => {
246+
const stepPrevChangepoint = useCallback(() => {
238247
for (let i = props.changepointSteps.length - 1; i >= 0; i--) {
239248
const step = props.changepointSteps[i];
240249
if (step < value) {
@@ -245,29 +254,36 @@ const SideContentCseMachineBase: React.FC<Props> = props => {
245254
}
246255
sliderShift(0);
247256
sliderRelease(0);
248-
};
257+
}, [props.changepointSteps, value, sliderShift, sliderRelease]);
249258

250-
const zoomStage = (isZoomIn: boolean, multiplier: number) => {
251-
if (isJava()) {
252-
JavaCseMachine.zoomStage(isZoomIn, multiplier);
253-
} else {
254-
Layout.zoomStage(isZoomIn, multiplier);
255-
}
256-
};
259+
const zoomStage = useCallback(
260+
(isZoomIn: boolean, multiplier: number) => {
261+
if (isJava()) {
262+
JavaCseMachine.zoomStage(isZoomIn, multiplier);
263+
} else {
264+
Layout.zoomStage(isZoomIn, multiplier);
265+
}
266+
},
267+
[isJava]
268+
);
257269

258-
const hotkeyBindings: HotkeyItem[] = visualization
259-
? [
260-
['a', stepFirst],
261-
['f', stepNext],
262-
['b', stepPrevious],
263-
['e', () => stepLast(props.stepsTotal)]
264-
]
265-
: [
266-
['a', () => {}],
267-
['f', () => {}],
268-
['b', () => {}],
269-
['e', () => {}]
270-
];
270+
const hotkeyBindings: HotkeyItem[] = useMemo(
271+
() =>
272+
visualization
273+
? [
274+
['a', stepFirst],
275+
['f', stepNext],
276+
['b', stepPrevious],
277+
['e', () => stepLast(props.stepsTotal)]
278+
]
279+
: [
280+
['a', () => {}],
281+
['f', () => {}],
282+
['b', () => {}],
283+
['e', () => {}]
284+
],
285+
[visualization, stepFirst, stepNext, stepPrevious, props.stepsTotal, stepLast]
286+
);
271287

272288
return (
273289
<HotKeys

0 commit comments

Comments
 (0)