Skip to content

Commit

Permalink
https://github.com/AdguardTeam/AdguardBrowserExtension/issues/681
Browse files Browse the repository at this point in the history
  • Loading branch information
atropnikov committed May 12, 2017
1 parent 4edfd8a commit 265735b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
52 changes: 31 additions & 21 deletions Extension/browser/chrome/lib/utils/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ adguard.localStorageImpl = (function () {
}

/**
* Checks runtime.lastError and calls "callback" if so.
*
* @returns true if operation caused error
* Creates default handler for async operation
* @param callback Callback, fired with parameters (ex, result)
*/
function checkLastError(callback) {
if (browser.runtime.lastError) {
callback(browser.runtime.lastError);
return true;
}
return false;
function createDefaultAsyncHandler(callback) {

var dfd = new adguard.utils.Promise();
dfd.then(
function (result) {
callback(null, result);
}, function (ex) {
callback(ex);
});

return dfd;
}

/**
Expand All @@ -50,18 +54,19 @@ adguard.localStorageImpl = (function () {
* @param callback Callback
*/
function read(path, callback) {

var dfd = createDefaultAsyncHandler(callback);

try {
browser.storage.local.get(path, function (results) {
if (!checkLastError(callback)) {
var result = null;
if (results) {
result = results[path];
}
callback(null, result);
if (browser.runtime.lastError) {
dfd.reject(browser.runtime.lastError);
} else {
dfd.resolve(results ? results[path] : null);
}
});
} catch (ex) {
callback(ex);
dfd.reject(ex);
}
}

Expand All @@ -72,16 +77,21 @@ adguard.localStorageImpl = (function () {
* @param callback Callback
*/
function write(path, data, callback) {
var item = {};
item[path] = data;

var dfd = createDefaultAsyncHandler(callback);

try {
var item = {};
item[path] = data;
browser.storage.local.set(item, function () {
if (!checkLastError(callback)) {
callback();
if (browser.runtime.lastError) {
dfd.reject(browser.runtime.lastError);
} else {
dfd.resolve();
}
});
} catch (ex) {
callback(ex);
dfd.reject(ex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Extension/lib/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ adguard.utils = (function () {
deferred.resolve(arg);
};

var reject = function () {
deferred.reject();
var reject = function (arg) {
deferred.reject(arg);
};

var then = function (onSuccess, onReject) {
Expand Down

0 comments on commit 265735b

Please sign in to comment.