Skip to content

Commit c50a2c7

Browse files
committed
feat: add event listeners
1 parent 70d254f commit c50a2c7

File tree

2 files changed

+248
-116
lines changed

2 files changed

+248
-116
lines changed

src/spotify-types.ts

Lines changed: 89 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,45 @@
22
* @todo https://developer.spotify.com/documentation/web-playback-sdk/reference/
33
*/
44

5-
/** @hidden */
6-
type WebPlaybackMethod<T = void> = () => Promise<T>;
5+
/**
6+
* Generic method type.
7+
*/
8+
export type SpotifyWebPlaybackMethod<T = void, R = void> = (args: T) => R;
79

810
/** @hidden */
9-
export type WebPlaybackCallback = (token: string) => void;
11+
export type SpotifyOAuthCallback = SpotifyWebPlaybackMethod<string, Promise<void>>;
1012

11-
/** @hidden */
12-
export type WebPlaybackStatuses = 'ready' | 'not_ready';
13+
/**
14+
* Status events.
15+
*/
16+
export type SpotifyWebPlaybackStatusType = 'ready' | 'not_ready';
1317

14-
/** @hidden */
15-
export type WebPlaybackStates = 'player_state_changed';
18+
/**
19+
* State change event.
20+
*/
21+
export type SpotifyWebPlaybackStateType = 'player_state_changed';
1622

17-
/** @hidden */
18-
export type WebPlaybackErrors =
23+
/**
24+
* Error events.
25+
*/
26+
export type SpotifyWebPlaybackErrorType =
1927
| 'initialization_error'
2028
| 'authentication_error'
2129
| 'account_error'
2230
| 'playback_error';
2331

2432
/** @hidden */
25-
export interface WebPlaybackError {
26-
message: WebPlaybackErrors;
33+
export interface SpotifyWebPlaybackError {
34+
message: SpotifyWebPlaybackErrorType;
2735
}
2836

2937
/** @hidden */
30-
export interface WebPlaybackReady {
38+
export interface SpotifyWebPlaybackReady {
3139
device_id: string;
3240
}
3341

3442
/** @hidden */
35-
export interface WebPlaybackState {
43+
export interface SpotifyWebPlaybackState {
3644
bitrate: number;
3745
context: {
3846
metadata: Record<string, unknown>;
@@ -53,36 +61,36 @@ export interface WebPlaybackState {
5361
shuffle: boolean;
5462
timestamp: number;
5563
track_window: {
56-
current_track: WebPlaybackTrack;
57-
next_tracks: WebPlaybackTrack[];
58-
previous_tracks: WebPlaybackTrack[];
64+
current_track: SpotifyWebPlaybackTrack;
65+
next_tracks: SpotifyWebPlaybackTrack[];
66+
previous_tracks: SpotifyWebPlaybackTrack[];
5967
};
6068
}
6169

6270
/** @hidden */
63-
export interface WebPlaybackAlbum {
64-
images: WebPlaybackImage[];
65-
name: string;
66-
uri: string;
67-
}
68-
69-
/** @hidden */
70-
export interface WebPlaybackArtist {
71+
export interface SpotifyWebPlaybackArtist {
7172
name: string;
7273
uri: string;
7374
}
7475

7576
/** @hidden */
76-
export interface WebPlaybackImage {
77+
export interface SpotifyWebPlaybackImage {
7778
height: number;
7879
url: string;
7980
width: number;
8081
}
8182

8283
/** @hidden */
83-
export interface WebPlaybackTrack {
84-
album: WebPlaybackAlbum;
85-
artists: WebPlaybackArtist[];
84+
export interface SpotifyWebPlaybackAlbum {
85+
images: SpotifyWebPlaybackImage[];
86+
name: string;
87+
uri: string;
88+
}
89+
90+
/** @hidden */
91+
export interface SpotifyWebPlaybackTrack {
92+
album: SpotifyWebPlaybackAlbum;
93+
artists: SpotifyWebPlaybackArtist[];
8694
duration_ms: number;
8795
id: string;
8896
is_playable: boolean;
@@ -97,35 +105,70 @@ export interface WebPlaybackTrack {
97105
uri: string;
98106
}
99107

108+
/**
109+
* Event callbacks.
110+
*/
111+
export type SpotifyListenerType = 'error' | 'state' | 'ready';
112+
113+
/**
114+
* An event listener for when an error occurs.
115+
*/
116+
export type SpotifyErrorListener = SpotifyWebPlaybackMethod<SpotifyWebPlaybackErrorType>;
117+
118+
/**
119+
* An event listener for when the playback state changes.
120+
*/
121+
export type SpotifyStateListener = SpotifyWebPlaybackMethod<SpotifyWebPlaybackState | null>;
122+
123+
/**
124+
* An event listener for when the player status changes.
125+
*/
126+
export type SpotifyStatusListener = SpotifyWebPlaybackMethod<SpotifyWebPlaybackStatusType>;
127+
128+
/**
129+
* Event listeners.
130+
*/
131+
export type SpotifyListener = SpotifyErrorListener | SpotifyStateListener | SpotifyStatusListener;
132+
133+
134+
/** @hidden */
135+
export type SpotifyErrorCallback = SpotifyWebPlaybackMethod<SpotifyWebPlaybackError>;
136+
137+
/** @hidden */
138+
export type SpotifyStateCallback = SpotifyWebPlaybackMethod<SpotifyWebPlaybackState | null>;
139+
140+
/** @hidden */
141+
export type SpotifyStatusCallback = SpotifyWebPlaybackMethod<SpotifyWebPlaybackReady>;
142+
100143
/** @hidden */
101-
export interface WebPlaybackPlayer {
144+
export interface SpotifyWebPlaybackPlayer {
102145
_options: {
103-
getOAuthToken: WebPlaybackCallback;
146+
getOAuthToken: SpotifyOAuthCallback;
104147
name: string;
105148
id: string;
106149
volume: number;
107150
};
108151
addListener: {
109-
(event: WebPlaybackErrors, callback: (d: WebPlaybackError) => void): boolean;
110-
(event: WebPlaybackStates, callback: (d: WebPlaybackState | null) => void): boolean;
111-
(event: WebPlaybackStatuses, callback: (d: WebPlaybackReady) => void): boolean;
152+
(event: SpotifyWebPlaybackErrorType, callback: SpotifyErrorCallback): boolean;
153+
(event: SpotifyWebPlaybackStateType, callback: SpotifyStateCallback): boolean;
154+
(event: SpotifyWebPlaybackStatusType, callback: SpotifyStatusCallback): boolean;
112155
};
113-
connect: WebPlaybackMethod<boolean>;
114-
disconnect: () => void;
115-
getCurrentState: () => Promise<WebPlaybackState | null>;
116-
getVolume: WebPlaybackMethod<number>;
117-
pause: WebPlaybackMethod;
118-
nextTrack: WebPlaybackMethod;
119-
previousTrack: WebPlaybackMethod;
156+
connect: SpotifyWebPlaybackMethod<void, boolean>;
157+
disconnect: SpotifyWebPlaybackMethod;
158+
getCurrentState: SpotifyWebPlaybackMethod<void, Promise<SpotifyWebPlaybackState | null>>;
159+
getVolume: SpotifyWebPlaybackMethod<void, number>;
160+
pause: SpotifyWebPlaybackMethod;
161+
nextTrack: SpotifyWebPlaybackMethod;
162+
previousTrack: SpotifyWebPlaybackMethod;
120163
removeListener: (
121-
event: WebPlaybackErrors | WebPlaybackStates | WebPlaybackStatuses,
122-
callback?: () => void,
164+
event: SpotifyWebPlaybackErrorType | SpotifyWebPlaybackStateType | SpotifyWebPlaybackStatusType,
165+
callback?: SpotifyWebPlaybackMethod,
123166
) => boolean;
124-
resume: WebPlaybackMethod;
167+
resume: SpotifyWebPlaybackMethod;
125168
seek: (positionMS: number) => Promise<void>;
126-
setName: (n: string) => Promise<void>;
127-
setVolume: (n: number) => Promise<void>;
128-
togglePlay: WebPlaybackMethod;
169+
setName: (name: string) => Promise<void>;
170+
setVolume: (volume: number) => Promise<void>;
171+
togglePlay: SpotifyWebPlaybackMethod;
129172
}
130173

131174
/**

0 commit comments

Comments
 (0)