SourceManager
API reference for the SourceManager class in Moonlink.js
SourceManager Class
The SourceManager
class is responsible for managing native audio sources like Spotify and Deezer within Moonlink.js. It handles the loading, registration, and matching of these sources, allowing the Manager
to search and load tracks from them directly.
manager.sources
The SourceManager
is accessed through the sources
property of the Manager
instance. You don't need to create it manually.
Properties
Property | Type | Description |
---|---|---|
manager | Manager | Reference to the Manager instance. |
sources | Record<string, ISource> | An object containing all registered native sources, keyed by their name. |
Methods
add
Add Source
Adds a new native source to the SourceManager. This method is primarily used internally during initialization to load built-in sources.
Parameters
Returns & Example
Returns
• void
// Used internally by Moonlink.js to load native sources.
// You typically won't need to call this directly.
get
Get Source
Retrieves a registered native source by its name.
Parameters
Returns & Example
Returns
• ISource | undefined
— The source object if found, otherwise undefined
.
const spotifySource = manager.sources.get('Spotify');
if (spotifySource) {
console.log(`Spotify source found: ${spotifySource.name}`);
}
has
Has Source
Checks if a native source with the given name is registered.
Parameters
Returns & Example
Returns
• boolean
— true
if the source is registered, false
otherwise.
const hasDeezer = manager.sources.has('Deezer');
if (hasDeezer) {
console.log('Deezer source is available.');
}
remove
Remove Source
Removes a registered native source from the SourceManager.
Parameters
Returns & Example
Returns
• void
manager.sources.remove('Spotify');
console.log('Spotify source removed.');
clear
Clear All Sources
Removes all registered native sources from the SourceManager.
Returns & Example
Returns
• void
manager.sources.clear();
console.log('All native sources cleared.');
getAll
Get All Sources
Retrieves an array of all registered native sources.
Returns & Example
Returns
• ISource[]
— An array of all registered source objects.
const allSources = manager.sources.getAll();
console.log('Available sources:', allSources.map(s => s.name));
loadFolder
Load Sources from Folder
Loads native source implementations from the designated sources folder. This method is called automatically during SourceManager
initialization.
Returns & Example
Returns
• Promise<void>
// This method is called internally.
isLinkMatch
Check Link Match
Checks if a given URL or query string matches any registered native source.
Parameters
Returns & Example
Returns
• [boolean, string | null]
— A tuple where the first element indicates if a match was found (true
/false
), and the second element is the name of the matching source (or null
if no match).
const [isMatch, sourceName] = manager.sources.isLinkMatch('https://open.spotify.com/track/123');
if (isMatch) {
console.log(`Link matches source: ${sourceName}`);
}
Usage Example
Accessing and Using Sources
Example of how to access and use native sources through the SourceManager
.
// Access the SourceManager instance
const sourceManager = manager.sources;
// Check if Spotify source is available
if (sourceManager.has('Spotify')) {
const spotify = sourceManager.get('Spotify');
console.log(`Spotify source name: ${spotify.name}`);
// Search for a track on Spotify
const searchResults = await spotify.search('spsearch:Never Gonna Give You Up');
console.log('Spotify search results:', searchResults);
// Load a Spotify track by URL
const trackData = await spotify.load('https://open.spotify.com/track/4PTG3Z6ehFOyPTL5XdNygQ');
console.log('Loaded Spotify track:', trackData);
}
// Get all registered sources
const allAvailableSources = sourceManager.getAll();
console.log('All available native sources:', allAvailableSources.map(s => s.name));