frontend: Avoid re-rendering static components during playback

This commit is contained in:
Rob Watson 2021-12-02 20:13:06 +01:00
parent 0cc1fd8272
commit 155e41136c
3 changed files with 52 additions and 34 deletions

View File

@ -6,7 +6,7 @@ import {
GetAudioProgress, GetAudioProgress,
} from './generated/media_set'; } from './generated/media_set';
import { useState, useEffect, useRef } from 'react'; import { useState, useEffect, useRef, useMemo } from 'react';
import { VideoPreview } from './VideoPreview'; import { VideoPreview } from './VideoPreview';
import { Overview, CanvasLogicalWidth } from './Overview'; import { Overview, CanvasLogicalWidth } from './Overview';
import { Waveform } from './Waveform'; import { Waveform } from './Waveform';
@ -192,15 +192,10 @@ function App(): JSX.Element {
const offsetPixels = Math.floor(thumbnailWidth / 2); const offsetPixels = Math.floor(thumbnailWidth / 2);
if (mediaSet == null) { // Avoid re-rendering this component during playback. Needs to be memoized
// TODO: improve // before the mediaSet null check below.
return <></>; const controlBar = useMemo(
} () => (
return (
<>
<div className="App">
<div style={containerStyles}>
<ControlBar <ControlBar
onPlay={() => { onPlay={() => {
audio.play(); audio.play();
@ -211,7 +206,20 @@ function App(): JSX.Element {
audio.pause(); audio.pause();
}} }}
/> />
),
[]
);
if (mediaSet == null) {
// TODO: improve
return <></>;
}
return (
<>
<div className="App">
<div style={containerStyles}>
{controlBar}
<Overview <Overview
peaks={overviewPeaks} peaks={overviewPeaks}
mediaSet={mediaSet} mediaSet={mediaSet}

View File

@ -1,4 +1,4 @@
import { useState, useEffect, useRef, MouseEvent } from 'react'; import { useState, useEffect, useRef, useMemo, MouseEvent } 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';
@ -336,6 +336,8 @@ export const Overview: React.FC<Props> = ({
return ( return (
<> <>
<div style={containerStyles}> <div style={containerStyles}>
{useMemo(
() => (
<WaveformCanvas <WaveformCanvas
peaks={peaks} peaks={peaks}
channels={mediaSet.audioChannels} channels={mediaSet.audioChannels}
@ -346,6 +348,9 @@ export const Overview: React.FC<Props> = ({
zIndex={1} zIndex={1}
alpha={1} alpha={1}
></WaveformCanvas> ></WaveformCanvas>
),
[peaks, mediaSet]
)}
<canvas <canvas
ref={hudCanvasRef} ref={hudCanvasRef}
width={CanvasLogicalWidth} width={CanvasLogicalWidth}

View File

@ -1,4 +1,4 @@
import { useEffect, useState, useRef } from 'react'; import { useEffect, useState, useRef, useMemo } 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';
@ -112,6 +112,8 @@ export const Waveform: React.FC<Props> = ({
return ( return (
<> <>
<div style={containerStyles}> <div style={containerStyles}>
{useMemo(
() => (
<WaveformCanvas <WaveformCanvas
peaks={peaks} peaks={peaks}
channels={mediaSet.audioChannels} channels={mediaSet.audioChannels}
@ -122,6 +124,9 @@ export const Waveform: React.FC<Props> = ({
zIndex={0} zIndex={0}
alpha={1} alpha={1}
></WaveformCanvas> ></WaveformCanvas>
),
[peaks, mediaSet]
)}
<canvas <canvas
width={CanvasLogicalWidth} width={CanvasLogicalWidth}
height={CanvasLogicalHeight} height={CanvasLogicalHeight}