bitwarden-estensione-browser/src/services/settings.service.ts

63 lines
1.9 KiB
TypeScript
Raw Normal View History

import { SettingsService as SettingsServiceAbstraction } from '../abstractions/settings.service';
import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';
2018-01-10 04:47:58 +01:00
const Keys = {
settingsPrefix: 'settings_',
equivalentDomains: 'equivalentDomains',
};
2018-02-08 18:24:37 +01:00
export class SettingsService implements SettingsServiceAbstraction {
2018-01-10 04:47:58 +01:00
private settingsCache: any;
constructor(private userService: UserService, private storageService: StorageService) {
}
clearCache(): void {
this.settingsCache = null;
}
getEquivalentDomains(): Promise<any> {
return this.getSettingsKey(Keys.equivalentDomains);
}
async setEquivalentDomains(equivalentDomains: string[][]): Promise<void> {
await this.setSettingsKey(Keys.equivalentDomains, equivalentDomains);
}
async clear(userId: string): Promise<void> {
await this.storageService.remove(Keys.settingsPrefix + userId);
2019-04-11 22:32:07 +02:00
this.clearCache();
2018-01-10 04:47:58 +01:00
}
// Helpers
private async getSettings(): Promise<any> {
if (this.settingsCache == null) {
const userId = await this.userService.getUserId();
this.settingsCache = this.storageService.get(Keys.settingsPrefix + userId);
}
return this.settingsCache;
}
private async getSettingsKey(key: string): Promise<any> {
const settings = await this.getSettings();
if (settings != null && settings[key]) {
return settings[key];
}
return null;
}
private async setSettingsKey(key: string, value: any): Promise<void> {
const userId = await this.userService.getUserId();
let settings = await this.getSettings();
if (!settings) {
settings = {};
}
settings[key] = value;
await this.storageService.save(Keys.settingsPrefix + userId, settings);
this.settingsCache = settings;
}
}