From 2211c569d7cfd1a2c00f8949070c47746400f0e0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 3 Nov 2017 15:33:29 -0400 Subject: [PATCH] settings service to ts --- src/background.html | 1 - src/background.js | 3 +- src/services/settings.service.ts | 61 ++++++++++++++++++++ src/services/settingsService.js | 99 -------------------------------- src/services/syncService.js | 8 +-- 5 files changed, 64 insertions(+), 108 deletions(-) create mode 100644 src/services/settings.service.ts delete mode 100644 src/services/settingsService.js diff --git a/src/background.html b/src/background.html index ad2926ad..a9e418fd 100644 --- a/src/background.html +++ b/src/background.html @@ -5,7 +5,6 @@ - diff --git a/src/background.js b/src/background.js index 044708bc..1a3e45db 100644 --- a/src/background.js +++ b/src/background.js @@ -7,6 +7,7 @@ import EnvironmentService from './services/environment.service'; import i18nService from './services/i18nService.js'; import LockService from './services/lockService.js'; import PasswordGenerationService from './services/passwordGeneration.service'; +import SettingsService from './services/settings.service'; import TokenService from './services/token.service'; import TotpService from './services/totp.service'; import UserService from './services/user.service'; @@ -86,7 +87,7 @@ var bg_isBackground = true, window.bg_apiService = bg_apiService = new ApiService(bg_tokenService, logout); window.bg_environmentService = bg_environmentService = new EnvironmentService(bg_apiService); window.bg_userService = bg_userService = new UserService(bg_tokenService); - window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService, bg_utilsService); + window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService); window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService, bg_utilsService, bg_constantsService); window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_apiService, bg_i18nService, bg_utilsService); diff --git a/src/services/settings.service.ts b/src/services/settings.service.ts new file mode 100644 index 00000000..d429ce4c --- /dev/null +++ b/src/services/settings.service.ts @@ -0,0 +1,61 @@ +import UserService from './user.service'; +import UtilsService from './utils.service'; + +const Keys = { + settingsPrefix: 'settings_', + equivalentDomains: 'equivalentDomains', +}; + +export default class SettingsService { + private settingsCache: any; + + constructor(private userService: UserService) { + } + + clearCache(): void { + this.settingsCache = null; + } + + getEquivalentDomains(): Promise { + return this.getSettingsKey(Keys.equivalentDomains); + } + + async setEquivalentDomains(equivalentDomains: any) { + await this.setSettingsKey(Keys.equivalentDomains, equivalentDomains); + } + + async clear(userId: string): Promise { + await UtilsService.removeFromStorage(Keys.settingsPrefix + userId); + this.settingsCache = null; + } + + // Helpers + + private async getSettings(): Promise { + if (this.settingsCache == null) { + const userId = await this.userService.getUserId(); + this.settingsCache = UtilsService.getObjFromStorage(Keys.settingsPrefix + userId); + } + return this.settingsCache; + } + + private async getSettingsKey(key: string): Promise { + const settings = await this.getSettings(); + if (settings != null && settings[key]) { + return settings[key]; + } + return null; + } + + private async setSettingsKey(key: string, value: any): Promise { + const userId = await this.userService.getUserId(); + let settings = await this.getSettings(); + if (!settings) { + settings = {}; + } + + settings[key] = value; + await UtilsService.saveObjToStorage(Keys.settingsPrefix + userId, settings); + this.settingsCache = settings; + } +} diff --git a/src/services/settingsService.js b/src/services/settingsService.js deleted file mode 100644 index 741adb4a..00000000 --- a/src/services/settingsService.js +++ /dev/null @@ -1,99 +0,0 @@ -function SettingsService(userService, utilsService) { - this.userService = userService; - this.utilsService = utilsService; - this.settingsCache = null; - - initSettingsService(); -} - -function initSettingsService() { - SettingsService.prototype.clearCache = function () { - this.settingsCache = null; - }; - - SettingsService.prototype.getSettings = function (callback) { - if (!callback || typeof callback !== 'function') { - throw 'callback function required'; - } - - var self = this; - - if (self.settingsCache) { - callback(self.settingsCache); - return; - } - - this.userService.getUserId().then(function (userId) { - var key = 'settings_' + userId; - chrome.storage.local.get(key, function (obj) { - self.settingsCache = obj[key]; - callback(self.settingsCache); - }); - }); - }; - - SettingsService.prototype.getEquivalentDomains = function (callback) { - var deferred = Q.defer(); - - getSettingsKey(this, 'equivalentDomains', function (domains) { - deferred.resolve(domains); - }); - - return deferred.promise; - }; - - function getSettingsKey(self, key, callback) { - if (!callback || typeof callback !== 'function') { - throw 'callback function required'; - } - - self.getSettings(function (settings) { - if (settings && settings[key]) { - callback(settings[key]); - return; - } - - callback(null); - }); - } - - SettingsService.prototype.setEquivalentDomains = function (equivalentDomains, callback) { - setSettingsKey(this, 'equivalentDomains', equivalentDomains, callback); - }; - - function setSettingsKey(self, key, value, callback) { - if (!callback || typeof callback !== 'function') { - throw 'callback function required'; - } - - self.userService.getUserId().then(function (userId) { - var settingsKey = 'settings_' + userId; - - self.getSettings(function (settings) { - if (!settings) { - settings = {}; - } - settings[key] = value; - - var obj = {}; - obj[settingsKey] = settings; - - chrome.storage.local.set(obj, function () { - self.settingsCache = settings; - callback(); - }); - }); - }); - } - - SettingsService.prototype.clear = function (userId) { - var self = this; - return self.utilsService.removeFromStorage('settings_' + userId).then(function () { - self.settingsCache = null; - }); - }; - - function handleError(error, deferred) { - deferred.reject(error); - } -} diff --git a/src/services/syncService.js b/src/services/syncService.js index 95f33ba3..4c4172e8 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -147,8 +147,6 @@ function initSyncService() { } function syncSettings(self, userId, response) { - var deferred = Q.defer(); - var eqDomains = []; if (response && response.equivalentDomains) { eqDomains = eqDomains.concat(response.equivalentDomains); @@ -161,11 +159,7 @@ function initSyncService() { } } - self.settingsService.setEquivalentDomains(eqDomains, function () { - deferred.resolve(); - }); - - return deferred.promise; + return self.settingsService.setEquivalentDomains(eqDomains); } SyncService.prototype.getLastSync = function (callback) {