Star ✨ on GitHub

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

Properties

PropertyTypeDescription
managerManagerReference to the Manager instance.
sourcesRecord<string, ISource>An object containing all registered native sources, keyed by their name.

Methods

add

addmethod

Add Source

Adds a new native source to the SourceManager. This method is primarily used internally during initialization to load built-in sources.

Parameters
sourcerequiredISource
The source object to add.

Returns & Example

Returnsvoid

// Used internally by Moonlink.js to load native sources.
// You typically won't need to call this directly.

get

getmethod

Get Source

Retrieves a registered native source by its name.

Parameters
namerequiredstring
The name of the source to retrieve.

Returns & Example

ReturnsISource | undefined — The source object if found, otherwise undefined.

const spotifySource = manager.sources.get('Spotify');
if (spotifySource) {
  console.log(`Spotify source found: ${spotifySource.name}`);
}

has

hasmethod

Has Source

Checks if a native source with the given name is registered.

Parameters
namerequiredstring
The name of the source to check.

Returns & Example

Returnsbooleantrue if the source is registered, false otherwise.

const hasDeezer = manager.sources.has('Deezer');
if (hasDeezer) {
  console.log('Deezer source is available.');
}

remove

removemethod

Remove Source

Removes a registered native source from the SourceManager.

Parameters
namerequiredstring
The name of the source to remove.

Returns & Example

Returnsvoid

manager.sources.remove('Spotify');
console.log('Spotify source removed.');

clear

clearmethod

Clear All Sources

Removes all registered native sources from the SourceManager.

Returns & Example

Returnsvoid

manager.sources.clear();
console.log('All native sources cleared.');

getAll

getAllmethod

Get All Sources

Retrieves an array of all registered native sources.

Returns & Example

ReturnsISource[] — An array of all registered source objects.

const allSources = manager.sources.getAll();
console.log('Available sources:', allSources.map(s => s.name));

loadFolder

loadFoldermethod

Load Sources from Folder

Loads native source implementations from the designated sources folder. This method is called automatically during SourceManager initialization.

Returns & Example

ReturnsPromise<void>

// This method is called internally.

isLinkMatch

isLinkMatchmethod

Check Link Match

Checks if a given URL or query string matches any registered native source.

Parameters
urlrequiredstring
The URL or query string to check.
_unusedSourceParamstring
Internal parameter, typically not used directly.

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));