@nent/core
Version:
72 lines (71 loc) • 2.41 kB
JavaScript
/*!
* NENT 2022
*/
import { debounce } from '../../../services/common';
import { EventEmitter } from '../../../services/common/emitter';
import { DATA_EVENTS, } from '../../../services/data/interfaces';
/* It listens to the audio listener and emits a `changed` event when the audio listener changes */
export class AudioDataProvider {
/**
* A constructor function that takes in an audioListener as a parameter. It then creates a new
* EventEmitter and assigns it to the changed property. It then creates a change function that is
* debounced and emits a changed event. It then subscribes to the audioListener's changed event and
* calls the change function.
* @param {AudioActionListener} audioListener - AudioActionListener - this is the service that
* listens for changes to the audio data.
*/
constructor(audioListener) {
this.audioListener = audioListener;
this.changed = new EventEmitter();
const change = debounce(1000, () => {
this.changed.emit(DATA_EVENTS.DataChanged, {
provider: 'audio',
});
}, true);
this.listenerSubscription = this.audioListener.changed.on('changed', () => {
change();
});
}
async get(key) {
switch (key) {
// Global
case 'hasAudio':
return this.audioListener.hasAudio().toString();
case 'isPlaying':
return this.audioListener.isPlaying().toString();
// Music files
case 'loadedMusic':
return this.audioListener.music
? JSON.stringify(this.audioListener.music.loader.items)
: null;
case 'queuedMusic':
return this.audioListener.music
? JSON.stringify(this.audioListener.music.queue.items)
: null;
case 'currentMusic':
return this.audioListener.music.active
? JSON.stringify(this.audioListener.music.active)
: null;
// Sound files
case 'loadedSounds':
return this.audioListener.sound.loader.items
? JSON.stringify(this.audioListener.sound.loader.items)
: null;
case 'currentSound':
return this.audioListener.sound.active
? JSON.stringify(this.audioListener.sound.active)
: null;
default:
return null;
}
}
async set(_key, _value) {
// do nothing
}
/**
* It destroys the listener subscription.
*/
destroy() {
this.listenerSubscription();
}
}