Remove most useCallback usages
It is unclear whether these are actually significantly improving performance and they add non-trivial complexity to the codebase, especially when under heavy frontend development. Removing most of them for now until it can be shown they are actually worthwhile.
This commit is contained in:
parent
35b62f1e59
commit
f33fa149fc
|
@ -181,8 +181,7 @@ function App(): JSX.Element {
|
||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
|
|
||||||
const handleKeyPress = useCallback(
|
const handleKeyPress = (evt: KeyboardEvent) => {
|
||||||
(evt: KeyboardEvent) => {
|
|
||||||
if (evt.code != 'Space') {
|
if (evt.code != 'Space') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -192,13 +191,10 @@ function App(): JSX.Element {
|
||||||
} else {
|
} else {
|
||||||
handlePause();
|
handlePause();
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
[selection]
|
|
||||||
);
|
|
||||||
|
|
||||||
// handler called when the selection in the overview (zoom setting) is changed.
|
// handler called when the selection in the overview (zoom setting) is changed.
|
||||||
const handleOverviewSelectionChange = useCallback(
|
const handleOverviewSelectionChange = (newViewport: Frames) => {
|
||||||
(newViewport: Frames) => {
|
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -210,13 +206,10 @@ function App(): JSX.Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
setPositionFromFrame(newViewport.start);
|
setPositionFromFrame(newViewport.start);
|
||||||
},
|
};
|
||||||
[mediaSet, audio, video, selection]
|
|
||||||
);
|
|
||||||
|
|
||||||
// handler called when the selection in the main waveform view is changed.
|
// handler called when the selection in the main waveform view is changed.
|
||||||
const handleWaveformSelectionChange = useCallback(
|
const handleWaveformSelectionChange = (newSelection: Frames) => {
|
||||||
(newSelection: Frames) => {
|
|
||||||
setSelection(newSelection);
|
setSelection(newSelection);
|
||||||
|
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
|
@ -229,34 +222,32 @@ function App(): JSX.Element {
|
||||||
(mediaSet.audioFrames / mediaSet.audioSampleRate) * ratio;
|
(mediaSet.audioFrames / mediaSet.audioSampleRate) * ratio;
|
||||||
audio.currentTime = currentTime;
|
audio.currentTime = currentTime;
|
||||||
video.currentTime = currentTime;
|
video.currentTime = currentTime;
|
||||||
},
|
};
|
||||||
[mediaSet, audio, video, selection]
|
|
||||||
);
|
|
||||||
|
|
||||||
const handlePlay = useCallback(() => {
|
const handlePlay = () => {
|
||||||
audio.play();
|
audio.play();
|
||||||
video.play();
|
video.play();
|
||||||
}, [audio, video]);
|
};
|
||||||
|
|
||||||
const handlePause = useCallback(() => {
|
const handlePause = () => {
|
||||||
video.pause();
|
video.pause();
|
||||||
audio.pause();
|
audio.pause();
|
||||||
|
|
||||||
if (selection.start != selection.end) {
|
if (selection.start != selection.end) {
|
||||||
setPositionFromFrame(selection.start);
|
setPositionFromFrame(selection.start);
|
||||||
}
|
}
|
||||||
}, [audio, video, selection]);
|
};
|
||||||
|
|
||||||
const handleClip = useCallback(() => {
|
const handleClip = () => {
|
||||||
if (!window.showSaveFilePicker) {
|
if (!window.showSaveFilePicker) {
|
||||||
downloadClipHTTP();
|
downloadClipHTTP();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadClipFileSystemAccessAPI();
|
downloadClipFileSystemAccessAPI();
|
||||||
}, [mediaSet, selection]);
|
};
|
||||||
|
|
||||||
const downloadClipHTTP = useCallback(() => {
|
const downloadClipHTTP = () => {
|
||||||
(async function () {
|
(async function () {
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -285,9 +276,9 @@ function App(): JSX.Element {
|
||||||
document.body.appendChild(form);
|
document.body.appendChild(form);
|
||||||
form.submit();
|
form.submit();
|
||||||
})();
|
})();
|
||||||
}, [mediaSet, selection]);
|
};
|
||||||
|
|
||||||
const downloadClipFileSystemAccessAPI = useCallback(() => {
|
const downloadClipFileSystemAccessAPI = () => {
|
||||||
(async function () {
|
(async function () {
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -312,9 +303,9 @@ function App(): JSX.Element {
|
||||||
await fileStream.close();
|
await fileStream.close();
|
||||||
console.debug('closed stream');
|
console.debug('closed stream');
|
||||||
})();
|
})();
|
||||||
}, [mediaSet, selection]);
|
};
|
||||||
|
|
||||||
const handleZoomIn = useCallback(() => {
|
const handleZoomIn = () => {
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -325,9 +316,9 @@ function App(): JSX.Element {
|
||||||
...viewport,
|
...viewport,
|
||||||
end: viewport.end - Math.round((viewport.end - viewport.start) / 2),
|
end: viewport.end - Math.round((viewport.end - viewport.start) / 2),
|
||||||
});
|
});
|
||||||
}, [mediaSet, viewport]);
|
};
|
||||||
|
|
||||||
const handleZoomOut = useCallback(() => {
|
const handleZoomOut = () => {
|
||||||
if (mediaSet == null) {
|
if (mediaSet == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -342,7 +333,7 @@ function App(): JSX.Element {
|
||||||
...viewport,
|
...viewport,
|
||||||
end: end,
|
end: end,
|
||||||
});
|
});
|
||||||
}, [mediaSet, viewport]);
|
};
|
||||||
|
|
||||||
const setPositionFromFrame = useCallback(
|
const setPositionFromFrame = useCallback(
|
||||||
(frame: number) => {
|
(frame: number) => {
|
||||||
|
@ -355,7 +346,7 @@ function App(): JSX.Element {
|
||||||
audio.currentTime = currentTime;
|
audio.currentTime = currentTime;
|
||||||
video.currentTime = currentTime;
|
video.currentTime = currentTime;
|
||||||
},
|
},
|
||||||
[mediaSet, audio, video]
|
[mediaSet]
|
||||||
);
|
);
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { useState, useEffect, useRef, useCallback, MouseEvent } from 'react';
|
import { useState, useEffect, useRef, MouseEvent } from 'react';
|
||||||
|
|
||||||
interface Styles {
|
interface Styles {
|
||||||
borderLineWidth: number;
|
borderLineWidth: number;
|
||||||
|
@ -174,8 +174,7 @@ export const HudCanvas: React.FC<Props> = ({
|
||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseDown = useCallback(
|
const handleMouseDown = (evt: MouseEvent<HTMLCanvasElement>) => {
|
||||||
(evt: MouseEvent<HTMLCanvasElement>) => {
|
|
||||||
if (mode != Mode.Normal) {
|
if (mode != Mode.Normal) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -198,12 +197,9 @@ export const HudCanvas: React.FC<Props> = ({
|
||||||
moveOffsetX.current = x;
|
moveOffsetX.current = x;
|
||||||
setNewSelection({ start: x, end: x });
|
setNewSelection({ start: x, end: x });
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
[mode, selection]
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleMouseMove = useCallback(
|
const handleMouseMove = (evt: MouseEvent<HTMLCanvasElement>) => {
|
||||||
(evt: MouseEvent<HTMLCanvasElement>) => {
|
|
||||||
const x = getCanvasX(evt);
|
const x = getCanvasX(evt);
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
@ -271,11 +267,9 @@ export const HudCanvas: React.FC<Props> = ({
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
[mode, selection]
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleMouseUp = useCallback(() => {
|
const handleMouseUp = () => {
|
||||||
if (mode == Mode.Normal) {
|
if (mode == Mode.Normal) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -289,9 +283,9 @@ export const HudCanvas: React.FC<Props> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
onSelectionChange({ ...newSelection });
|
onSelectionChange({ ...newSelection });
|
||||||
}, [mode, newSelection]);
|
};
|
||||||
|
|
||||||
const handleEmptySelectionAction = useCallback(() => {
|
const handleEmptySelectionAction = () => {
|
||||||
switch (emptySelectionAction) {
|
switch (emptySelectionAction) {
|
||||||
case EmptySelectionAction.SelectPrevious:
|
case EmptySelectionAction.SelectPrevious:
|
||||||
setNewSelection({ ...selection });
|
setNewSelection({ ...selection });
|
||||||
|
@ -300,7 +294,7 @@ export const HudCanvas: React.FC<Props> = ({
|
||||||
onSelectionChange({ start: 0, end: 0 });
|
onSelectionChange({ start: 0, end: 0 });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}, [selection]);
|
};
|
||||||
|
|
||||||
const handleMouseLeave = (_evt: MouseEvent<HTMLCanvasElement>) => {
|
const handleMouseLeave = (_evt: MouseEvent<HTMLCanvasElement>) => {
|
||||||
setHoverState(HoverState.Normal);
|
setHoverState(HoverState.Normal);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { MediaSet } from './generated/media_set';
|
import { MediaSet } from './generated/media_set';
|
||||||
import { Frames, VideoPosition } from './App';
|
import { Frames, VideoPosition } from './App';
|
||||||
import { WaveformCanvas } from './WaveformCanvas';
|
import { WaveformCanvas } from './WaveformCanvas';
|
||||||
|
@ -60,15 +60,12 @@ export const Overview: React.FC<Props> = ({
|
||||||
// handlers
|
// handlers
|
||||||
|
|
||||||
// convert selection change from canvas pixels to frames, and trigger callback.
|
// convert selection change from canvas pixels to frames, and trigger callback.
|
||||||
const handleSelectionChange = useCallback(
|
const handleSelectionChange = ({ start, end }: Selection) => {
|
||||||
({ start, end }: Selection) => {
|
|
||||||
onSelectionChange({
|
onSelectionChange({
|
||||||
start: Math.round((start / CanvasLogicalWidth) * mediaSet.audioFrames),
|
start: Math.round((start / CanvasLogicalWidth) * mediaSet.audioFrames),
|
||||||
end: Math.round((end / CanvasLogicalWidth) * mediaSet.audioFrames),
|
end: Math.round((end / CanvasLogicalWidth) * mediaSet.audioFrames),
|
||||||
});
|
});
|
||||||
},
|
};
|
||||||
[mediaSet]
|
|
||||||
);
|
|
||||||
|
|
||||||
// render component
|
// render component
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState, useCallback } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Frames, VideoPosition, newRPC } from './App';
|
import { Frames, VideoPosition, newRPC } from './App';
|
||||||
import { MediaSetServiceClientImpl, MediaSet } from './generated/media_set';
|
import { MediaSetServiceClientImpl, MediaSet } from './generated/media_set';
|
||||||
import { WaveformCanvas } from './WaveformCanvas';
|
import { WaveformCanvas } from './WaveformCanvas';
|
||||||
|
@ -93,12 +93,10 @@ export const Waveform: React.FC<Props> = ({
|
||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
|
|
||||||
const handleSelectionChange = useCallback(
|
const handleSelectionChange = (selection: Selection) => {
|
||||||
(selection: Selection) => {
|
|
||||||
setSelectedPixels(selection);
|
setSelectedPixels(selection);
|
||||||
|
|
||||||
const framesPerPixel =
|
const framesPerPixel = (viewport.end - viewport.start) / CanvasLogicalWidth;
|
||||||
(viewport.end - viewport.start) / CanvasLogicalWidth;
|
|
||||||
const selectedFrames = {
|
const selectedFrames = {
|
||||||
start: Math.round(viewport.start + selection.start * framesPerPixel),
|
start: Math.round(viewport.start + selection.start * framesPerPixel),
|
||||||
end: Math.round(viewport.start + selection.end * framesPerPixel),
|
end: Math.round(viewport.start + selection.end * framesPerPixel),
|
||||||
|
@ -106,20 +104,14 @@ export const Waveform: React.FC<Props> = ({
|
||||||
|
|
||||||
setSelectedFrames(selectedFrames);
|
setSelectedFrames(selectedFrames);
|
||||||
onSelectionChange(selectedFrames);
|
onSelectionChange(selectedFrames);
|
||||||
},
|
};
|
||||||
[viewport, selectedFrames]
|
|
||||||
);
|
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|
||||||
const frameToCanvasX = useCallback(
|
const frameToCanvasX = (frame: number): number => {
|
||||||
(frame: number): number => {
|
const pixelsPerFrame = CanvasLogicalWidth / (viewport.end - viewport.start);
|
||||||
const pixelsPerFrame =
|
|
||||||
CanvasLogicalWidth / (viewport.end - viewport.start);
|
|
||||||
return Math.round((frame - viewport.start) * pixelsPerFrame);
|
return Math.round((frame - viewport.start) * pixelsPerFrame);
|
||||||
},
|
};
|
||||||
[viewport]
|
|
||||||
);
|
|
||||||
|
|
||||||
// render component
|
// render component
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue