-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client-electron): prefetch audio files
fixes #190
- Loading branch information
1 parent
53782b6
commit c25d23b
Showing
4 changed files
with
65 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export interface SoundPlayerConfig { | ||
soundEnabled: boolean; | ||
errorSoundPath: string; | ||
startSoundPath: string; | ||
} | ||
|
||
interface SoundPlayer { | ||
play(id: string): Promise<void>; | ||
} | ||
|
||
type TrackId = 'start' | 'error'; | ||
|
||
export class BreachProtocolSoundPlayer implements SoundPlayer { | ||
private readonly library = new Map<TrackId, HTMLAudioElement>([ | ||
['start', this.initAudio(this.config.startSoundPath)], | ||
['error', this.initAudio(this.config.errorSoundPath)], | ||
]); | ||
|
||
constructor(private config: SoundPlayerConfig) {} | ||
|
||
update({ startSoundPath, errorSoundPath, soundEnabled }: SoundPlayerConfig) { | ||
if (this.config.startSoundPath !== startSoundPath) { | ||
this.library.set('start', this.initAudio(startSoundPath)); | ||
this.config.startSoundPath = startSoundPath; | ||
} | ||
|
||
if (this.config.errorSoundPath !== errorSoundPath) { | ||
this.library.set('error', this.initAudio(errorSoundPath)); | ||
this.config.errorSoundPath = errorSoundPath; | ||
} | ||
|
||
if (this.config.soundEnabled !== soundEnabled) { | ||
this.config.soundEnabled = soundEnabled; | ||
} | ||
} | ||
|
||
play(id: TrackId) { | ||
if (!this.config.soundEnabled) return; | ||
|
||
const audio = this.library.get(id); | ||
|
||
if (!audio) return; | ||
|
||
return audio.play(); | ||
} | ||
|
||
private initAudio(path: string) { | ||
return path ? new Audio(path) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters