Skip to content

Commit e127d42

Browse files
committed
Add new APIs to Webamp NPM module
1 parent 4a0a58b commit e127d42

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

packages/webamp/js/selectors.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export const getOrderedTracks = createSelector(
7676
(tracks, trackOrder) => trackOrder.filter((id) => tracks[id])
7777
);
7878

79+
export const getPlaylistTracks = createSelector(
80+
getTracks,
81+
getTrackOrder,
82+
(tracks, trackOrder) => trackOrder.map((id) => tracks[id]).filter(Boolean)
83+
);
84+
7985
export const getUserTracks = createSelector(
8086
getTracks,
8187
getTrackOrder,
@@ -135,7 +141,7 @@ export const getRunningTimeMessage = createSelector(
135141
)}`
136142
);
137143

138-
// TODO: use slectors to get memoization
144+
// TODO: use selectors to get memoization
139145
export const getCurrentTrackIndex = (state: AppState): number => {
140146
const { playlist } = state;
141147
if (playlist.currentTrack == null) {

packages/webamp/js/webampLazy.tsx

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
PartialState,
1313
Options,
1414
MediaStatus,
15+
PlaylistTrack,
1516
} from "./types";
1617
import getStore from "./store";
1718
import App from "./components/App";
@@ -230,14 +231,14 @@ class Webamp {
230231
}
231232

232233
/**
233-
* Seek backward n seconds in the curent track
234+
* Seek backward n seconds in the current track
234235
*/
235236
seekBackward(seconds: number) {
236237
this.store.dispatch(Actions.seekBackward(seconds));
237238
}
238239

239240
/**
240-
* Seek forward n seconds in the curent track
241+
* Seek forward n seconds in the current track
241242
*/
242243
seekForward(seconds: number) {
243244
this.store.dispatch(Actions.seekForward(seconds));
@@ -250,6 +251,20 @@ class Webamp {
250251
this.store.dispatch(Actions.seekToTime(seconds));
251252
}
252253

254+
/**
255+
* Check if shuffle is enabled
256+
*/
257+
isShuffleEnabled(): boolean {
258+
return Selectors.getShuffle(this.store.getState());
259+
}
260+
261+
/**
262+
* Check if repeat is enabled
263+
*/
264+
isRepeatEnabled(): boolean {
265+
return Selectors.getRepeat(this.store.getState());
266+
}
267+
253268
/**
254269
* Play the next track
255270
*/
@@ -264,6 +279,13 @@ class Webamp {
264279
this.store.dispatch(Actions.previous());
265280
}
266281

282+
/**
283+
* Play a specific track by index
284+
*/
285+
playTrack(index: number): void {
286+
this.store.dispatch(Actions.playTrack(index));
287+
}
288+
267289
/**
268290
* Add an array of `Track`s to the end of the playlist.
269291
*/
@@ -281,6 +303,13 @@ class Webamp {
281303
this.store.dispatch(Actions.loadMediaFiles(tracks, LOAD_STYLE.PLAY));
282304
}
283305

306+
/**
307+
* Get the current playlist in order.
308+
*/
309+
getPlaylistTracks(): PlaylistTrack[] {
310+
return Selectors.getPlaylistTracks(this.store.getState());
311+
}
312+
284313
/**
285314
* Get the current "playing" status.
286315
*/
@@ -325,14 +354,50 @@ class Webamp {
325354
this.store.dispatch(Actions.open());
326355
}
327356

357+
/**
358+
* A callback which will be called whenever a the current track changes.
359+
*
360+
* The callback is passed the current track and the zero-based index of the
361+
* current track's position within the playlist.
362+
*
363+
* Note: This is different from the `onTrackDidChange` callback which is only
364+
* called when a new track first starts loading.
365+
*
366+
* @returns An "unsubscribe" function. Useful if at some point in the future
367+
* you want to stop listening to these events.
368+
*/
369+
onCurrentTrackDidChange(
370+
cb: (currentTrack: PlaylistTrack, trackIndex: number) => void
371+
): () => void {
372+
let previousTrack: PlaylistTrack | null = null;
373+
return this.store.subscribe(() => {
374+
const state = this.store.getState();
375+
const currentTrack = Selectors.getCurrentTrack(state);
376+
if (currentTrack == null || currentTrack === previousTrack) {
377+
return;
378+
}
379+
previousTrack = currentTrack;
380+
const trackIndex = Selectors.getCurrentTrackIndex(state);
381+
cb(currentTrack, trackIndex);
382+
});
383+
}
384+
328385
/**
329386
* A callback which will be called when a new track starts loading.
330387
*
331-
* This can happen on startup when the first track starts buffering, or when a subsequent track starts playing.
332-
* The callback will be called with an object `({url: 'https://example.com/track.mp3'})` containing the URL of the track.
388+
* This can happen on startup when the first track starts buffering, or when a
389+
* subsequent track starts playing. The callback will be called with an
390+
* object `({url: 'https://example.com/track.mp3'})` containing the URL of the
391+
* track.
392+
*
333393
* Note: If the user drags in a track, the URL may be an ObjectURL.
334394
*
335-
* @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
395+
* Note: This is different from the `onCurrentTrackDidChange` callback which
396+
* is called every time a track changes. This callback is only called when a
397+
* new track starts loading.
398+
*
399+
* @returns An "unsubscribe" function. Useful if at some point in the future
400+
* you want to stop listening to these events.
336401
*/
337402
onTrackDidChange(cb: (trackInfo: LoadedURLTrack | null) => void): () => void {
338403
let previousTrackId: number | null = null;

0 commit comments

Comments
 (0)