@wwdrew/expo-spotify-sdk
Version:
Expo module wrapping the native Spotify iOS (v5) and Android (v4) SDKs for OAuth authentication and App Remote playback control
87 lines • 3.24 kB
JavaScript
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
import ExpoSpotifySDKModule from "../ExpoSpotifySDKModule";
import { createNativeErrorRethrow } from "../internal/native-errors";
import { UserError } from "./error";
export { UserError } from "./error";
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
const rethrowAsUserError = createNativeErrorRethrow({
ErrorClass: UserError,
unknownCode: "UNKNOWN",
validCodes: new Set([
"NOT_CONNECTED",
"CONNECTION_LOST",
"INVALID_URI",
"OPERATION_NOT_ALLOWED",
"UNKNOWN",
]),
});
const libraryListeners = new Map();
function notifyLibraryState(state) {
const set = libraryListeners.get(state.uri);
if (!set)
return;
set.forEach((listener) => listener(state));
}
/**
* Spotify User namespace. Capabilities and library-state operations.
* Requires `AppRemote.connect()` to be resolved before any call.
*/
export const User = {
/** Returns the current user's capabilities. */
getCapabilities() {
return ExpoSpotifySDKModule.userGetCapabilities().catch(rethrowAsUserError);
},
/** Returns the current library state for a track or album URI. */
getLibraryState(uri) {
return ExpoSpotifySDKModule.userGetLibraryState(uri).catch(rethrowAsUserError);
},
/** Adds a track or album URI to the user's library. */
async addToLibrary(uri) {
const state = (await ExpoSpotifySDKModule.userAddToLibrary(uri).catch(rethrowAsUserError));
notifyLibraryState(state);
},
/** Removes a track or album URI from the user's library. */
async removeFromLibrary(uri) {
const state = (await ExpoSpotifySDKModule.userRemoveFromLibrary(uri).catch(rethrowAsUserError));
notifyLibraryState(state);
},
/**
* Subscribes to user-scoped events.
* Supported event: `"capabilitiesChange"`.
*/
addListener(event, listener) {
return ExpoSpotifySDKModule.addListener("onCapabilitiesChange", listener);
},
/**
* Subscribes to library state changes for a specific URI.
*
* There is no native push stream for per-URI library state updates on all
* platforms, so this listener is updated whenever library mutations are
* performed via this SDK and can be manually seeded by calling
* `User.getLibraryState(uri)` before subscribing.
*/
addLibraryStateListener(uri, listener) {
const key = String(uri);
let set = libraryListeners.get(key);
if (!set) {
set = new Set();
libraryListeners.set(key, set);
}
set.add(listener);
return {
remove() {
const current = libraryListeners.get(key);
if (!current)
return;
current.delete(listener);
if (current.size === 0)
libraryListeners.delete(key);
},
};
},
};
//# sourceMappingURL=index.js.map