Skip to content

Commit b7f4386

Browse files
committed
fix: make the audio player pool class simpler
1 parent 3058bbc commit b7f4386

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

package/src/contexts/audioPlayerContext/AudioPlayerContext.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';
22

3-
import { AudioPlayerPool } from '../../state-store/audio-player-pool';
3+
import { useStateStore } from '../../hooks/useStateStore';
4+
import { AudioPlayerPool, AudioPlayerPoolState } from '../../state-store/audio-player-pool';
45
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
56

67
export type AudioPlayerContextProps = {
@@ -40,3 +41,14 @@ export const WithAudioPlayback = ({
4041
};
4142

4243
export const useAudioPlayerContext = () => useContext(AudioPlayerContext);
44+
45+
const activeAudioPlayerSelector = ({ activeAudioPlayer }: AudioPlayerPoolState) => ({
46+
activeAudioPlayer,
47+
});
48+
49+
export const useActiveAudioPlayer = () => {
50+
const { audioPlayerPool } = useContext(AudioPlayerContext);
51+
const { activeAudioPlayer } =
52+
useStateStore(audioPlayerPool.state, activeAudioPlayerSelector) ?? {};
53+
return activeAudioPlayer;
54+
};

package/src/state-store/audio-player-pool.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import { StateStore } from 'stream-chat';
2+
13
import { AudioPlayer, AudioPlayerOptions } from './audio-player';
24

35
export type AudioPlayerPoolOptions = {
46
allowConcurrentAudioPlayback: boolean;
57
};
68

9+
export type AudioPlayerPoolState = {
10+
activeAudioPlayer: AudioPlayer | null;
11+
};
12+
713
export class AudioPlayerPool {
814
pool: Map<string, AudioPlayer>;
915
allowConcurrentAudioPlayback: boolean;
10-
private currentlyPlayingId: string | null = null;
16+
state: StateStore<AudioPlayerPoolState> = new StateStore<AudioPlayerPoolState>({
17+
activeAudioPlayer: null,
18+
});
1119

1220
constructor({ allowConcurrentAudioPlayback }: AudioPlayerPoolOptions) {
1321
this.pool = new Map<string, AudioPlayer>();
@@ -30,15 +38,24 @@ export class AudioPlayerPool {
3038
return newPlayer;
3139
}
3240

41+
setActivePlayer(activeAudioPlayer: AudioPlayer | null) {
42+
this.state.partialNext({
43+
activeAudioPlayer,
44+
});
45+
}
46+
47+
getActivePlayer() {
48+
return this.state.getLatestValue().activeAudioPlayer;
49+
}
50+
3351
removePlayer(id: string) {
3452
const player = this.pool.get(id);
3553
if (!player) return;
3654
player.onRemove();
3755
this.pool.delete(id);
3856

39-
// Clear tracking if this was the currently playing player
40-
if (!this.allowConcurrentAudioPlayback && this.currentlyPlayingId === id) {
41-
this.currentlyPlayingId = null;
57+
if (this.getActivePlayer()?.id === id) {
58+
this.setActivePlayer(null);
4259
}
4360
}
4461

@@ -52,24 +69,26 @@ export class AudioPlayerPool {
5269
for (const player of this.pool.values()) {
5370
this.removePlayer(player.id);
5471
}
55-
this.currentlyPlayingId = null;
72+
this.setActivePlayer(null);
5673
}
5774

5875
requestPlay(id: string) {
5976
if (this.allowConcurrentAudioPlayback) return;
6077

61-
if (this.currentlyPlayingId && this.currentlyPlayingId !== id) {
62-
const currentPlayer = this.pool.get(this.currentlyPlayingId);
78+
if (this.getActivePlayer()?.id !== id) {
79+
const currentPlayer = this.getActivePlayer();
6380
if (currentPlayer && currentPlayer.isPlaying) {
6481
currentPlayer.pause();
6582
}
6683
}
67-
this.currentlyPlayingId = id;
68-
}
6984

70-
notifyPaused(id: string) {
71-
if (this.currentlyPlayingId === id) {
72-
this.currentlyPlayingId = null;
85+
const activePlayer = this.pool.get(id);
86+
if (activePlayer) {
87+
this.setActivePlayer(activePlayer);
7388
}
7489
}
90+
91+
notifyPaused() {
92+
this.setActivePlayer(null);
93+
}
7594
}

package/src/state-store/audio-player.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class AudioPlayer {
296296
});
297297

298298
if (this._pool) {
299-
this._pool.notifyPaused(this.id);
299+
this._pool.notifyPaused();
300300
}
301301
}
302302

0 commit comments

Comments
 (0)