Skip to content

Commit

Permalink
Latest ECMA
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTCoughlin committed Nov 25, 2024
1 parent 24e23ee commit d13de49
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions app/scripts/background.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
const RADIO_URL = 'https://64.150.176.42:8242/;stream.mp3';

class MediaPlayer {
private audio = new Audio();
#audio = new Audio(); // Private class field for encapsulation

constructor(private url: string) {
chrome.browserAction.setBadgeText({ text: this.getBadgeText() });
chrome.browserAction.onClicked.addListener(() => this.toggle());
this.#updateBadge();
chrome.browserAction.onClicked.addListener(() => this.#toggle());
}

private getBadgeText(): string {
return this.audio.paused ? 'OFF' : 'ON';
}

private updateBadge(): void {
chrome.browserAction.setBadgeText({ text: this.getBadgeText() });
}

toggle(): void {
this.audio.paused ? this.play() : this.stop();
this.updateBadge();
}

private stop(): void {
this.audio.pause();
this.audio.src = '';
}

private play(): void {
this.audio.src = this.url;
this.audio
.play()
.then(() => console.log('Playback started'))
.catch(console.error);
}
#getBadgeText = () => (this.#audio.paused ? 'OFF' : 'ON'); // Arrow function for concise logic

#updateBadge = () =>
chrome.browserAction.setBadgeText({ text: this.#getBadgeText() }); // Inline badge update

#toggle = () => {
this.#audio.paused ? this.#play() : this.#stop();
this.#updateBadge();
};

#stop = () => {
this.#audio.pause();
this.#audio.src = '';
};

#play = async () => {
this.#audio.src = this.url;
try {
await this.#audio.play();
console.log('Playback started'); // Improved logging
} catch (error) {
console.error('Playback failed:', error); // Improved error handling
}
};
}

export default new MediaPlayer(RADIO_URL);

0 comments on commit d13de49

Please sign in to comment.