play safe in *Safe() as BG gets defined only in .then

This commit is contained in:
tophf 2017-05-23 22:31:40 +03:00
parent 0440073853
commit 26f84c5852

View File

@ -272,13 +272,13 @@ function sessionStorageHash(name) {
} }
function onBackgroundReady(...dataPassthru) { function onBackgroundReady() {
return BG ? Promise.resolve(...dataPassthru) : new Promise(ping); return BG ? Promise.resolve() : new Promise(ping);
function ping(resolve) { function ping(resolve) {
chrome.runtime.sendMessage({method: 'healthCheck'}, health => { chrome.runtime.sendMessage({method: 'healthCheck'}, health => {
if (health !== undefined) { if (health !== undefined) {
BG = chrome.extension.getBackgroundPage(); BG = chrome.extension.getBackgroundPage();
resolve(...dataPassthru); resolve();
} else { } else {
ping(resolve); ping(resolve);
} }
@ -289,13 +289,14 @@ function onBackgroundReady(...dataPassthru) {
// in case Chrome haven't yet loaded the bg page and displays our page like edit/manage // in case Chrome haven't yet loaded the bg page and displays our page like edit/manage
function getStylesSafe(options) { function getStylesSafe(options) {
return onBackgroundReady(options).then(BG.getStyles); return onBackgroundReady()
.then(() => BG.getStyles(options));
} }
function saveStyleSafe(style) { function saveStyleSafe(style) {
return onBackgroundReady(BG.deepCopy(style)) return onBackgroundReady()
.then(BG.saveStyle) .then(() => BG.saveStyle(BG.deepCopy(style)))
.then(savedStyle => { .then(savedStyle => {
if (style.notify === false) { if (style.notify === false) {
handleUpdate(savedStyle, style); handleUpdate(savedStyle, style);
@ -306,8 +307,8 @@ function saveStyleSafe(style) {
function deleteStyleSafe({id, notify = true} = {}) { function deleteStyleSafe({id, notify = true} = {}) {
return onBackgroundReady({id, notify}) return onBackgroundReady()
.then(BG.deleteStyle) .then(() => BG.deleteStyle({id, notify}))
.then(() => { .then(() => {
if (!notify) { if (!notify) {
handleDelete(id); handleDelete(id);