-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import tts from './tts.js' | ||
|
||
const VAULT = 'TTS' | ||
|
||
class TTSConfigVault { | ||
#vault | ||
#ls_outdated | ||
constructor() { | ||
this.#vault = JSON.parse(localStorage.getItem(VAULT)) | ||
if (!this.#vault) { | ||
this.#vault = {} | ||
localStorage.setItem(VAULT, '{}') | ||
} | ||
this.#ls_outdated = false | ||
} | ||
|
||
getVault() { | ||
return this.#vault | ||
} | ||
|
||
#LSVaultListener(event) { | ||
console.log('se ha cambiado en LS') | ||
if (event.key === VAULT) { | ||
if (JSON.stringify(this.#vault) === event.newValue) { | ||
this.#ls_outdated = false | ||
window.removeEventListener('storage', this.#LSVaultListener) | ||
console.log('Memoria y LS son iguales') | ||
} else { | ||
localStorage.setItem(VAULT, JSON.stringify(this.#vault)) | ||
console.log('Memoria y LS son diferentes') | ||
} | ||
this.#ls_outdated = false | ||
} | ||
} | ||
|
||
#lsIsOutdated() { | ||
console.log('se ha cambiado en memoria', this.#vault) | ||
if (!this.#ls_outdated) | ||
window.addEventListener('storage', this.#LSVaultListener) | ||
this.#ls_outdated = true | ||
localStorage.setItem(VAULT, JSON.stringify(this.#vault)) | ||
} | ||
|
||
getConfig(userName) { | ||
if (this.getVault()[userName]) { | ||
return this.getVault()[userName] | ||
} else return null | ||
} | ||
|
||
setConfig(userName, accent, variant) { | ||
if (tts.isAValidVoice(accent)) { | ||
this.#vault[userName] = { | ||
accent, | ||
variant: tts.isAValidVariant(accent, variant) ? variant : 1 | ||
} | ||
this.#lsIsOutdated() | ||
} | ||
} | ||
|
||
resetConfig(userName) { | ||
if (this.#vault[userName]) { | ||
this.#vault[userName] = undefined | ||
this.#ls_outdated = true | ||
} | ||
|
||
this.#lsIsOutdated() | ||
} | ||
} | ||
|
||
const ttsConfigVault = new TTSConfigVault() | ||
|
||
export default ttsConfigVault |