remove cancelAnimationFrame complexity and rely on MarkedExecution class

pull/21833/head
Germain Souquet 2021-06-28 09:24:35 +01:00
parent 09c22c37ff
commit 652e06a7b1
3 changed files with 18 additions and 41 deletions

View File

@ -34,11 +34,17 @@ interface IState {
@replaceableComponent("views.voice_messages.LiveRecordingClock") @replaceableComponent("views.voice_messages.LiveRecordingClock")
export default class LiveRecordingClock extends React.PureComponent<IProps, IState> { export default class LiveRecordingClock extends React.PureComponent<IProps, IState> {
private seconds = 0; private seconds = 0;
private rafId: number; private scheduledUpdate = new MarkedExecution(
() => this.updateClock(),
() => requestAnimationFrame(() => this.scheduledUpdate.trigger()),
)
state = { constructor(props) {
super(props);
this.state = {
seconds: 0, seconds: 0,
} }
}
componentDidMount() { componentDidMount() {
this.props.recorder.liveData.onUpdate((update: IRecordingUpdate) => { this.props.recorder.liveData.onUpdate((update: IRecordingUpdate) => {
@ -47,24 +53,10 @@ export default class LiveRecordingClock extends React.PureComponent<IProps, ISta
}); });
} }
private scheduledUpdate = new MarkedExecution(
() => this.updateClock(),
() => this.onLiveDataUpdate(),
)
private onLiveDataUpdate() {
if (this.rafId) {
cancelAnimationFrame(this.rafId);
}
this.rafId = requestAnimationFrame(() => this.scheduledUpdate.trigger())
}
private updateClock() { private updateClock() {
this.setState({ this.setState({
seconds: this.seconds, seconds: this.seconds,
}) })
this.rafId = null;
} }
public render() { public render() {

View File

@ -38,11 +38,17 @@ export default class LiveRecordingWaveform extends React.PureComponent<IProps, I
}; };
private waveform: number[] = []; private waveform: number[] = [];
private rafId: number; private scheduledUpdate = new MarkedExecution(
() => this.updateWaveform(),
() => requestAnimationFrame(() => this.scheduledUpdate.trigger()),
)
state = { constructor(props) {
super(props);
this.state = {
waveform: [], waveform: [],
} }
}
componentDidMount() { componentDidMount() {
this.props.recorder.liveData.onUpdate((update: IRecordingUpdate) => { this.props.recorder.liveData.onUpdate((update: IRecordingUpdate) => {
@ -51,24 +57,10 @@ export default class LiveRecordingWaveform extends React.PureComponent<IProps, I
}); });
} }
private scheduledUpdate = new MarkedExecution(
() => this.updateWaveform(),
() => this.onLiveDataUpdate(),
)
private onLiveDataUpdate() {
if (this.rafId) {
cancelAnimationFrame(this.rafId);
}
this.rafId = requestAnimationFrame(() => this.scheduledUpdate.trigger())
}
private updateWaveform() { private updateWaveform() {
this.setState({ this.setState({
waveform: this.waveform, waveform: this.waveform,
}) })
this.rafId = null;
} }
public render() { public render() {

View File

@ -33,13 +33,6 @@ export class MarkedExecution {
constructor(private fn: () => void, private onMarkCallback?: () => void) { constructor(private fn: () => void, private onMarkCallback?: () => void) {
} }
/**
* Getter for the _marked property
*/
public get marked() {
return this._marked;
}
/** /**
* Resets the mark without calling the function. * Resets the mark without calling the function.
*/ */