1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-08-01 19:10:22 +02:00

post getting settings to db service

This commit is contained in:
Kushagra Gour
2018-01-08 03:59:07 +05:30
parent bce45f9da2
commit 3f59777976
2 changed files with 30 additions and 17 deletions

View File

@@ -23,13 +23,17 @@
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(key => {
window.localStorage.setItem(key, JSON.stringify(obj[key])); window.localStorage.setItem(key, JSON.stringify(obj[key]));
}); });
/* eslint-disable consistent-return */
setTimeout(() => { setTimeout(() => {
if (cb) { if (cb) {
return cb(); return cb();
} }
}, FAUX_DELAY); }, FAUX_DELAY);
/* eslint-enable consistent-return */
} }
}; };
const dbLocalAlias = chrome && chrome.storage ? chrome.storage.local : local;
const dbSyncAlias = chrome && chrome.storage ? chrome.storage.sync : local;
async function getDb() { async function getDb() {
if (dbPromise) { if (dbPromise) {
@@ -67,19 +71,17 @@
async function getUserLastSeenVersion() { async function getUserLastSeenVersion() {
const d = deferred(); const d = deferred();
if (window.IS_EXTENSION) { // Will be chrome.storage.sync in extension environment,
chrome.storage.sync.get( // otherwise will fallback to localstorage
dbSyncAlias.get(
{ {
lastSeenVersion: '' lastSeenVersion: ''
}, },
function syncGetCallback(result) { result => {
d.resolve(result.lastSeenVersion); d.resolve(result.lastSeenVersion);
} }
); );
} return d.promise;
local.get('lastSeenVersion', result => {
d.resolve(result.lastSeenVersion);
});
// Might consider getting actual value from remote db. // Might consider getting actual value from remote db.
// Not critical right now. // Not critical right now.
} }
@@ -111,12 +113,23 @@
}); });
} }
function getSettings(defaultSettings) {
const d = deferred();
// Will be chrome.storage.sync in extension environment,
// otherwise will fallback to localstorage
dbSyncAlias.get(defaultSettings, result => {
d.resolve(result);
});
return d.promise;
}
window.db = { window.db = {
getDb, getDb,
getUser, getUser,
getUserLastSeenVersion, getUserLastSeenVersion,
setUserLastSeenVersion, setUserLastSeenVersion,
local: chrome && chrome.storage ? chrome.storage.local : local, getSettings,
sync: chrome && chrome.storage ? chrome.storage.sync : local local: dbLocalAlias,
sync: dbSyncAlias
}; };
})(); })();

View File

@@ -1710,7 +1710,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
db.sync.set(obj, function() { db.sync.set(obj, function() {
alertsService.add('Setting saved'); alertsService.add('Setting saved');
}); });
window.db.getDb(remoteDb => { window.db.getDb().then(remoteDb => {
remoteDb remoteDb
.collection('users') .collection('users')
.doc(window.user.uid) .doc(window.user.uid)
@@ -2376,7 +2376,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
); );
// Get synced `preserveLastCode` setting to get back last code (or not). // Get synced `preserveLastCode` setting to get back last code (or not).
db.sync.get(defaultSettings, function syncGetCallback(result) { db.getSettings(defaultSettings).then(result => {
if (result.preserveLastCode && lastCode) { if (result.preserveLastCode && lastCode) {
unsavedEditCount = 0; unsavedEditCount = 0;
if (lastCode.id) { if (lastCode.id) {