/
SpotifyRemote.ts
384 lines (341 loc) · 11.2 KB
/
SpotifyRemote.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { EmitterSubscription, NativeEventEmitter, NativeModules, Platform } from 'react-native';
import ContentItem from './ContentItem';
import ContentType from './ContentType';
import CrossfadeState from './CrossfadeState';
import GetChildrenItemsOptions, { DEFAULT_GET_CHILDREN_OPTIONS } from './GetChildrenItemsOptions';
import PlayerContext from './PlayerContext';
import PlayerState from './PlayerState';
import RecommendedContentOptions from './RecommendedContentOptions';
import RepeatMode from './RepeatMode';
import TypedEventEmitter from './TypedEventEmitter';
/**
* Events supported by the [[SpotifyRemoteApi]]
*
* @interface SpotifyRemoteEvents
*/
export interface SpotifyRemoteEvents {
/**
* Fired when the state of the Spotify Player changes
*
* @type {PlayerState}
* @memberof SpotifyRemoteEvents
*/
"playerStateChanged": PlayerState;
/**
* Fires when the context of the Spotify Player changes
*
* @type {PlayerContext}
* @memberof SpotifyRemoteEvents
*/
"playerContextChanged": PlayerContext;
/**
* Fired when the Spotify Remote is disconnected from the Spotify App
*
* @type {void}
* @memberof SpotifyRemoteEvents
*/
"remoteDisconnected": void;
/**
* Fired when the Spotify Remote Connection is established with the Spotify App
*
* @type {void}
* @memberof SpotifyRemoteEvents
*/
"remoteConnected": void;
}
/**
* Interface describes Javascript only extensions to the native api
* @ignore
* @interface SpotifyRemoteApiExtensions
*/
interface SpotifyRemoteApiExtensions {
/**
* @deprecated Please use *resume* and *pause* instead
* @param {boolean} playing
* @returns {Promise<void>}
* @memberof SpotifyJSApi
*/
setPlaying(playing: boolean): Promise<void>;
}
/**
* The Spotify Remote Api allows remote control of Spotify Application
*
* See the example shown for [[SpotifyAuth]]
*
* @export
* @interface SpotifyRemoteApi
*/
export interface SpotifyRemoteApi extends TypedEventEmitter<SpotifyRemoteEvents>, SpotifyRemoteApiExtensions {
/**
* Asynchronous call to get whether or not the Spotify Remote is connected
*
* @returns {Promise<boolean>}
* @memberof SpotifyNativeApi
*/
isConnectedAsync(): Promise<boolean>;
/**
* Connect to Spotify Application via the access token
*
* @param {string} accessToken
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
connect(accessToken: string): Promise<void>;
/**
* Disconnects the App Remote
*
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
disconnect(): Promise<void>;
/**
* Play a track, album, playlist or artist via spotifyUri
* Example: spotify:track:<id>, spotify:album:<id>, spotify:playlist:<id>, spotify:artist:<id>
* @param {string} spotifyUri
* @returns {Promise<void>}
* @memberof SpotifyNativeApi
*/
playUri(spotifyUri: string): Promise<void>;
/**
* Plays a content item
*
* @param {ContentItem} item
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
playItem(item: ContentItem): Promise<void>;
/**
* Plays an item (like a playlist), skipping to a particular track inside
*
* @param {ContentItem} item - item to play (usually a playlist)
* @param {number} skipToTrackIndex - track in playlist to skip to
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
playItemWithIndex(item: ContentItem, skipToTrackIndex: number): Promise<void>;
/**
* Queues the track given by spotifyUri in Spotify
* example: spotify:track:<id>
* @param {string} spotifyUri
* @returns {Promise<void>}
* @memberof SpotifyNativeApi
*/
queueUri(spotifyUri: string): Promise<void>;
/**
* Seeks to a position within a song
*
* @param {number} positionMs - Position in milliseconds
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
seek(positionMs: number): Promise<void>;
/**
* Resumes playing
*
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
resume(): Promise<void>;
/**
* Pauses Playback
*
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
pause(): Promise<void>;
/**
* Skips to next item in context
*
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
skipToNext(): Promise<void>;
/**
* Skips to previous item in context
*
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
skipToPrevious(): Promise<void>;
/**
* Sets shuffling
*
* @param {boolean} shuffling
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
setShuffling(shuffling: boolean): Promise<void>;
/**
* Sets repeat mode of player
*
* @param {RepeatMode} mode
* @returns {Promise<void>}
* @memberof SpotifyRemoteApi
*/
setRepeatMode(mode: RepeatMode): Promise<void>;
/**
* Gets the current state of the player
*
* @returns {Promise<PlayerState>}
* @memberof SpotifyRemoteApi
*/
getPlayerState(): Promise<PlayerState>;
/**
* Retrieves the root content items for a given type. (iOS only)
*
* @param {ContentType} [type]
* @returns {Promise<ContentItem[]>}
* @memberof SpotifyRemoteApi
*/
getRootContentItems(type?: ContentType): Promise<ContentItem[]>;
/**
* Gets the recommended content items for type
*
* @param {RecommendedContentOptions} options
* @returns {Promise<ContentItem[]>}
* @memberof SpotifyRemoteApi
*/
getRecommendedContentItems(options: RecommendedContentOptions): Promise<ContentItem[]>;
/**
* Gets the children of a given item
*
* @param {(Pick<ContentItem, 'uri' | 'id'>)} item
* @param {GetChildrenItemsOptions} [options]
* @returns {Promise<ContentItem[]>}
* @memberof SpotifyRemoteApi
*/
getChildrenOfItem(item: Pick<ContentItem, 'uri' | 'id'>, options?: GetChildrenItemsOptions): Promise<ContentItem[]>;
/**
* Gets a ContentItem from a uri (iOS only)
*
* @param {string} uri
* @returns {Promise<ContentItem>}
* @memberof SpotifyRemoteApi
*/
getContentItemForUri(uri: string): Promise<ContentItem | undefined>;
/**
* Retrieves the current crossfade state of the player.
*
* @returns {Promise<CrossfadeState>}
* @memberof SpotifyRemoteApi
*/
getCrossfadeState(): Promise<CrossfadeState>;
}
const nativeModule = NativeModules.RNSpotifyRemoteAppRemote;
const nativeEventEmitter = new NativeEventEmitter(nativeModule);
const eventListeners: Record<
keyof SpotifyRemoteEvents,
Set<EmitterSubscription>
> = {
playerContextChanged: new Set(),
playerStateChanged: new Set(),
remoteConnected: new Set(),
remoteDisconnected: new Set(),
};
const SpotifyRemote: SpotifyRemoteApi = {
// Native APIs
connect: nativeModule.connect.bind(nativeModule),
disconnect: nativeModule.disconnect.bind(nativeModule),
getChildrenOfItem: nativeModule.getChildrenOfItem.bind(nativeModule),
getContentItemForUri: nativeModule.getContentItemForUri.bind(nativeModule),
getCrossfadeState: nativeModule.getCrossfadeState.bind(nativeModule),
getPlayerState: nativeModule.getPlayerState.bind(nativeModule),
getRecommendedContentItems:
nativeModule.getRecommendedContentItems.bind(nativeModule),
getRootContentItems: nativeModule.getRootContentItems.bind(nativeModule),
isConnectedAsync: nativeModule.isConnectedAsync.bind(nativeModule),
pause: nativeModule.pause.bind(nativeModule),
playItem: nativeModule.playItem.bind(nativeModule),
playItemWithIndex: nativeModule.playItemWithIndex.bind(nativeModule),
playUri: nativeModule.playUri.bind(nativeModule),
queueUri: nativeModule.queueUri.bind(nativeModule),
resume: nativeModule.resume.bind(nativeModule),
seek: nativeModule.seek.bind(nativeModule),
setRepeatMode: nativeModule.setRepeatMode.bind(nativeModule),
setShuffling: nativeModule.setShuffling.bind(nativeModule),
skipToNext: nativeModule.skipToNext.bind(nativeModule),
skipToPrevious: nativeModule.skipToPrevious.bind(nativeModule),
setPlaying(playing: boolean) {
// todo: Will want to likely check the state of playing somewhere?
// Perhaps this can be done in native land so that we don't need to
// worry about it here
return playing ? this.resume() : this.pause();
},
// Listeners
addListener(eventType, listener) {
const sub = nativeEventEmitter.addListener(eventType, listener);
if (this.listenerCount(eventType) === 0) {
nativeModule.eventStartObserving(eventType);
}
eventListeners[eventType].add(sub);
const _remove = sub.remove;
// rewrite sub.remove so we can add stopObserving API
sub.remove = () => {
_remove.call(sub);
eventListeners[eventType].delete(sub);
if (this.listenerCount(eventType) === 0) {
nativeModule.eventStopObserving(eventType);
}
};
return sub;
},
removeListener(eventType, listener) {
eventListeners[eventType].forEach((eventListener) => {
if (eventListener.listener === listener) eventListener.remove();
});
},
removeAllListeners(eventType) {
const eventsToRemove = eventType ? [eventType] : this.eventNames();
for (const eventToRemove of eventsToRemove) {
eventListeners[eventToRemove].forEach((eventListener) => {
eventListener.remove();
});
}
},
emit(eventType, ...args) {
return nativeEventEmitter.emit(eventType, ...args);
},
listenerCount(eventType) {
return eventListeners[eventType].size;
},
on(...args) {
return this.addListener(...args);
},
off(...args) {
return this.removeListener(...args);
},
eventNames() {
return [
'playerContextChanged',
'playerStateChanged',
'remoteConnected',
'remoteDisconnected',
];
},
};
// Augment the android module to warn on unimplemented methods
if (Platform.OS === "android") {
SpotifyRemote.getContentItemForUri = async (uri: string) => {
console.warn("getContentItemForUri is not implemented in Spotify's Android SDK");
return undefined;
}
SpotifyRemote.getRootContentItems = async (type: ContentType) => {
console.warn("getRootContentItems is not implemented in Spotify's Android SDK");
return [];
}
const androidGetItemOfChildren = SpotifyRemote.getChildrenOfItem;
SpotifyRemote.getChildrenOfItem = async (item: ContentItem, options) => {
return androidGetItemOfChildren(item, {
...DEFAULT_GET_CHILDREN_OPTIONS,
...options
});
}
}
// Augment the iOS module to handle differences
if (Platform.OS === "ios") {
const iosGetChildrenOfItem = SpotifyRemote.getChildrenOfItem;
SpotifyRemote.getChildrenOfItem = async (item: ContentItem, options) => {
return iosGetChildrenOfItem(item);
}
}
export default SpotifyRemote;