Skip to content

Commit 234061c

Browse files
authored
Use useTypedEventEmitterState for broadcasts (matrix-org#9947)
1 parent 9dbc5f3 commit 234061c

File tree

7 files changed

+67
-49
lines changed

7 files changed

+67
-49
lines changed

src/hooks/useEventEmitter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export function useEventEmitter(emitter: EventEmitter | undefined, eventName: st
6363

6464
type Mapper<T> = (...args: any[]) => T;
6565

66+
/**
67+
* {@link useEventEmitterState}
68+
*/
6669
export function useTypedEventEmitterState<T, Events extends string, Arguments extends ListenerMap<Events>>(
6770
emitter: TypedEventEmitter<Events, Arguments>,
6871
eventName: Events,
@@ -71,6 +74,16 @@ export function useTypedEventEmitterState<T, Events extends string, Arguments ex
7174
return useEventEmitterState<T>(emitter, eventName, fn);
7275
}
7376

77+
/**
78+
* Creates a state, that can be updated by events.
79+
*
80+
* @param emitter The emitter sending the event
81+
* @param eventName Event name to listen for
82+
* @param fn The callback function, that should return the state value.
83+
* It should have the signature of the event callback, except that all parameters are optional.
84+
* If the params are not set, a default value for the state should be returned.
85+
* @returns State
86+
*/
7487
export function useEventEmitterState<T>(
7588
emitter: EventEmitter | undefined,
7689
eventName: string | symbol,

src/voice-broadcast/hooks/useCurrentVoiceBroadcastPlayback.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { useState } from "react";
18-
19-
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
17+
import { useTypedEventEmitterState } from "../../hooks/useEventEmitter";
2018
import { VoiceBroadcastPlayback } from "../models/VoiceBroadcastPlayback";
2119
import {
2220
VoiceBroadcastPlaybacksStore,
@@ -28,15 +26,11 @@ export const useCurrentVoiceBroadcastPlayback = (
2826
): {
2927
currentVoiceBroadcastPlayback: VoiceBroadcastPlayback | null;
3028
} => {
31-
const [currentVoiceBroadcastPlayback, setVoiceBroadcastPlayback] = useState(
32-
voiceBroadcastPlaybackStore.getCurrent(),
33-
);
34-
35-
useTypedEventEmitter(
29+
const currentVoiceBroadcastPlayback = useTypedEventEmitterState(
3630
voiceBroadcastPlaybackStore,
3731
VoiceBroadcastPlaybacksStoreEvent.CurrentChanged,
38-
(playback: VoiceBroadcastPlayback) => {
39-
setVoiceBroadcastPlayback(playback);
32+
(playback?: VoiceBroadcastPlayback) => {
33+
return playback ?? voiceBroadcastPlaybackStore.getCurrent();
4034
},
4135
);
4236

src/voice-broadcast/hooks/useCurrentVoiceBroadcastPreRecording.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { useState } from "react";
18-
19-
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
17+
import { useTypedEventEmitterState } from "../../hooks/useEventEmitter";
2018
import { VoiceBroadcastPreRecordingStore } from "../stores/VoiceBroadcastPreRecordingStore";
2119
import { VoiceBroadcastPreRecording } from "../models/VoiceBroadcastPreRecording";
2220

@@ -25,12 +23,14 @@ export const useCurrentVoiceBroadcastPreRecording = (
2523
): {
2624
currentVoiceBroadcastPreRecording: VoiceBroadcastPreRecording | null;
2725
} => {
28-
const [currentVoiceBroadcastPreRecording, setCurrentVoiceBroadcastPreRecording] = useState(
29-
voiceBroadcastPreRecordingStore.getCurrent(),
26+
const currentVoiceBroadcastPreRecording = useTypedEventEmitterState(
27+
voiceBroadcastPreRecordingStore,
28+
"changed",
29+
(preRecording?: VoiceBroadcastPreRecording) => {
30+
return preRecording ?? voiceBroadcastPreRecordingStore.getCurrent();
31+
},
3032
);
3133

32-
useTypedEventEmitter(voiceBroadcastPreRecordingStore, "changed", setCurrentVoiceBroadcastPreRecording);
33-
3434
return {
3535
currentVoiceBroadcastPreRecording,
3636
};

src/voice-broadcast/hooks/useCurrentVoiceBroadcastRecording.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { useState } from "react";
18-
1917
import { VoiceBroadcastRecording, VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStoreEvent } from "..";
20-
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
18+
import { useTypedEventEmitterState } from "../../hooks/useEventEmitter";
2119

2220
export const useCurrentVoiceBroadcastRecording = (
2321
voiceBroadcastRecordingsStore: VoiceBroadcastRecordingsStore,
2422
): {
2523
currentVoiceBroadcastRecording: VoiceBroadcastRecording;
2624
} => {
27-
const [currentVoiceBroadcastRecording, setCurrentVoiceBroadcastRecording] = useState(
28-
voiceBroadcastRecordingsStore.getCurrent(),
29-
);
30-
31-
useTypedEventEmitter(
25+
const currentVoiceBroadcastRecording = useTypedEventEmitterState(
3226
voiceBroadcastRecordingsStore,
3327
VoiceBroadcastRecordingsStoreEvent.CurrentChanged,
34-
setCurrentVoiceBroadcastRecording,
28+
(recording?: VoiceBroadcastRecording) => {
29+
return recording ?? voiceBroadcastRecordingsStore.getCurrent();
30+
},
3531
);
3632

3733
return {

src/voice-broadcast/hooks/useVoiceBroadcastPlayback.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { useState } from "react";
1817
import { Room } from "matrix-js-sdk/src/models/room";
1918
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
2019

21-
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
20+
import { useTypedEventEmitterState } from "../../hooks/useEventEmitter";
2221
import { MatrixClientPeg } from "../../MatrixClientPeg";
2322
import {
2423
VoiceBroadcastLiveness,
2524
VoiceBroadcastPlayback,
2625
VoiceBroadcastPlaybackEvent,
2726
VoiceBroadcastPlaybackState,
27+
VoiceBroadcastPlaybackTimes,
2828
} from "..";
2929

3030
export const useVoiceBroadcastPlayback = (
@@ -52,24 +52,35 @@ export const useVoiceBroadcastPlayback = (
5252
playback.toggle();
5353
};
5454

55-
const [playbackState, setPlaybackState] = useState(playback.getState());
56-
useTypedEventEmitter(
55+
const playbackState = useTypedEventEmitterState(
5756
playback,
5857
VoiceBroadcastPlaybackEvent.StateChanged,
59-
(state: VoiceBroadcastPlaybackState, _playback: VoiceBroadcastPlayback) => {
60-
setPlaybackState(state);
58+
(state?: VoiceBroadcastPlaybackState) => {
59+
return state ?? playback.getState();
6160
},
6261
);
6362

64-
const [times, setTimes] = useState({
65-
duration: playback.durationSeconds,
66-
position: playback.timeSeconds,
67-
timeLeft: playback.timeLeftSeconds,
68-
});
69-
useTypedEventEmitter(playback, VoiceBroadcastPlaybackEvent.TimesChanged, (t) => setTimes(t));
63+
const times = useTypedEventEmitterState(
64+
playback,
65+
VoiceBroadcastPlaybackEvent.TimesChanged,
66+
(t?: VoiceBroadcastPlaybackTimes) => {
67+
return (
68+
t ?? {
69+
duration: playback.durationSeconds,
70+
position: playback.timeSeconds,
71+
timeLeft: playback.timeLeftSeconds,
72+
}
73+
);
74+
},
75+
);
7076

71-
const [liveness, setLiveness] = useState(playback.getLiveness());
72-
useTypedEventEmitter(playback, VoiceBroadcastPlaybackEvent.LivenessChanged, (l) => setLiveness(l));
77+
const liveness = useTypedEventEmitterState(
78+
playback,
79+
VoiceBroadcastPlaybackEvent.LivenessChanged,
80+
(l?: VoiceBroadcastLiveness) => {
81+
return l ?? playback.getLiveness();
82+
},
83+
);
7384

7485
return {
7586
times,

src/voice-broadcast/hooks/useVoiceBroadcastRecording.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import { Room } from "matrix-js-sdk/src/models/room";
1818
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
19-
import React, { useState } from "react";
19+
import React from "react";
2020

2121
import {
2222
VoiceBroadcastInfoState,
@@ -25,7 +25,7 @@ import {
2525
VoiceBroadcastRecordingState,
2626
} from "..";
2727
import QuestionDialog from "../../components/views/dialogs/QuestionDialog";
28-
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
28+
import { useTypedEventEmitterState } from "../../hooks/useEventEmitter";
2929
import { _t } from "../../languageHandler";
3030
import { MatrixClientPeg } from "../../MatrixClientPeg";
3131
import Modal from "../../Modal";
@@ -74,17 +74,21 @@ export const useVoiceBroadcastRecording = (
7474
}
7575
};
7676

77-
const [recordingState, setRecordingState] = useState(recording.getState());
78-
useTypedEventEmitter(
77+
const recordingState = useTypedEventEmitterState(
7978
recording,
8079
VoiceBroadcastRecordingEvent.StateChanged,
81-
(state: VoiceBroadcastInfoState, _recording: VoiceBroadcastRecording) => {
82-
setRecordingState(state);
80+
(state?: VoiceBroadcastRecordingState) => {
81+
return state ?? recording.getState();
8382
},
8483
);
8584

86-
const [timeLeft, setTimeLeft] = useState(recording.getTimeLeft());
87-
useTypedEventEmitter(recording, VoiceBroadcastRecordingEvent.TimeLeftChanged, setTimeLeft);
85+
const timeLeft = useTypedEventEmitterState(
86+
recording,
87+
VoiceBroadcastRecordingEvent.TimeLeftChanged,
88+
(t?: number) => {
89+
return t ?? recording.getTimeLeft();
90+
},
91+
);
8892

8993
const live = (
9094
[VoiceBroadcastInfoState.Started, VoiceBroadcastInfoState.Resumed] as VoiceBroadcastRecordingState[]

src/voice-broadcast/models/VoiceBroadcastPlayback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export enum VoiceBroadcastPlaybackEvent {
5757
InfoStateChanged = "info_state_changed",
5858
}
5959

60-
type VoiceBroadcastPlaybackTimes = {
60+
export type VoiceBroadcastPlaybackTimes = {
6161
duration: number;
6262
position: number;
6363
timeLeft: number;

0 commit comments

Comments
 (0)