@nent/core
Version:
57 lines (56 loc) • 1.54 kB
JavaScript
/*!
* NENT 2022
*/
/* It's a list of audio tracks that can be played, stopped, and destroyed */
export class AudioList {
constructor() {
this.items = [];
}
/**
* It returns true if the length of the items array is equal to 0
* @returns The length of the items array.
*/
isEmpty() {
return this.items.length == 0;
}
/**
* If the length of the items array is greater than 0, return true
* @returns The length of the items array.
*/
hasItems() {
return this.items.length > 0;
}
/**
* Find the first item in the items array that has a trackId property that matches the trackId
* parameter. If no item is found, return null
* @param {string} trackId - The trackId of the track you want to find.
* @returns The trackId of the track that is being searched for.
*/
findTrack(trackId) {
return this.items.find(a => a.trackId == trackId) || null;
}
/**
* It stops all the items in the items array.
*/
stop() {
this.items.forEach(t => {
if (t.playing())
t.stop();
});
}
/**
* It destroys all the items in the array.
*/
destroy() {
this.items.forEach(a => a.destroy());
}
/**
* It filters out the audio tracks that have a discard strategy that matches one of the reasons
* passed in
* @param {DiscardStrategy[]} reasons - DiscardStrategy[]
*/
discard(...reasons) {
const eligibleAudio = (audio) => !reasons.includes(audio.discard);
this.items = this.items.filter(i => eligibleAudio(i)) || null;
}
}