-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
entersState.ts
55 lines (51 loc) · 2.15 KB
/
entersState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { type EventEmitter, once } from 'node:events';
import type { VoiceConnection, VoiceConnectionStatus } from '../VoiceConnection';
import type { AudioPlayer, AudioPlayerStatus } from '../audio/AudioPlayer';
import { abortAfter } from './abortAfter';
/**
* Allows a voice connection a specified amount of time to enter a given state, otherwise rejects with an error.
*
* @param target - The voice connection that we want to observe the state change for
* @param status - The status that the voice connection should be in
* @param timeoutOrSignal - The maximum time we are allowing for this to occur, or a signal that will abort the operation
*/
export function entersState(
target: VoiceConnection,
status: VoiceConnectionStatus,
timeoutOrSignal: AbortSignal | number,
): Promise<VoiceConnection>;
/**
* Allows an audio player a specified amount of time to enter a given state, otherwise rejects with an error.
*
* @param target - The audio player that we want to observe the state change for
* @param status - The status that the audio player should be in
* @param timeoutOrSignal - The maximum time we are allowing for this to occur, or a signal that will abort the operation
*/
export function entersState(
target: AudioPlayer,
status: AudioPlayerStatus,
timeoutOrSignal: AbortSignal | number,
): Promise<AudioPlayer>;
/**
* Allows a target a specified amount of time to enter a given state, otherwise rejects with an error.
*
* @param target - The object that we want to observe the state change for
* @param status - The status that the target should be in
* @param timeoutOrSignal - The maximum time we are allowing for this to occur, or a signal that will abort the operation
*/
export async function entersState<Target extends AudioPlayer | VoiceConnection>(
target: Target,
status: AudioPlayerStatus | VoiceConnectionStatus,
timeoutOrSignal: AbortSignal | number,
) {
if (target.state.status !== status) {
const [ac, signal] =
typeof timeoutOrSignal === 'number' ? abortAfter(timeoutOrSignal) : [undefined, timeoutOrSignal];
try {
await once(target as EventEmitter, status, { signal });
} finally {
ac?.abort();
}
}
return target;
}