prevent API request every time checkAuth is called from switching tabs

This commit is contained in:
Mario Basic
2015-06-27 00:20:39 +02:00
parent 8b77268728
commit 12b153b323
3 changed files with 124 additions and 141 deletions

View File

@@ -83,66 +83,55 @@ class WakaTime {
* and sends it to WakaTime for logging.
*/
recordHeartbeat() {
this.checkAuth().done(data => {
if (data !== false) {
chrome.storage.sync.get({
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, (items) => {
if (items.loggingEnabled === true) {
chrome.storage.sync.get({
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, (items) => {
if (items.loggingEnabled === true) {
changeExtensionState('allGood');
changeExtensionState('allGood');
chrome.idle.queryState(config.detectionIntervalInSeconds, (newState) => {
chrome.idle.queryState(config.detectionIntervalInSeconds, (newState) => {
if (newState === 'active') {
// Get current tab URL.
chrome.tabs.query({active: true}, (tabs) => {
if (newState === 'active') {
// Get current tab URL.
chrome.tabs.query({active: true}, (tabs) => {
var currentActiveTab = tabs[0];
var currentActiveTab = tabs[0];
var debug = false;
// If the current active tab has devtools open
if (in_array(currentActiveTab.id, this.tabsWithDevtoolsOpen)) debug = true;
var debug = false;
// If the current active tab has devtools open
if (in_array(currentActiveTab.id, this.tabsWithDevtoolsOpen)) debug = true;
if (items.loggingStyle == 'blacklist') {
if (! contains(currentActiveTab.url, items.blacklist)) {
this.sendHeartbeat(currentActiveTab.url, debug);
}
else {
changeExtensionState('blacklisted');
console.log(currentActiveTab.url + ' is on a blacklist.');
}
}
if (items.loggingStyle == 'whitelist') {
if (contains(currentActiveTab.url, items.whitelist)) {
this.sendHeartbeat(currentActiveTab.url, debug);
}
else {
changeExtensionState('whitelisted');
console.log(currentActiveTab.url + ' is not on a whitelist.');
}
}
});
if (items.loggingStyle == 'blacklist') {
if (! contains(currentActiveTab.url, items.blacklist)) {
this.sendHeartbeat(currentActiveTab.url, debug);
}
else {
changeExtensionState('blacklisted');
console.log(currentActiveTab.url + ' is on a blacklist.');
}
}
if (items.loggingStyle == 'whitelist') {
if (contains(currentActiveTab.url, items.whitelist)) {
this.sendHeartbeat(currentActiveTab.url, debug);
}
else {
changeExtensionState('whitelisted');
console.log(currentActiveTab.url + ' is not on a whitelist.');
}
}
});
}
else {
changeExtensionState('notLogging');
}
});
}
else {
// User is not logged in.
// Change extension icon to red color.
changeExtensionState('notSignedIn');
changeExtensionState('notLogging');
}
});
}
@@ -239,17 +228,21 @@ class WakaTime {
contentType: 'application/json',
method: method,
data: payload,
statusCode: {
401: function () {
changeExtensionState('notSignedIn');
},
201: function () {
// nothing to do here
}
},
success: (response) => {
deferredObject.resolve(this);
},
error: (xhr, status, err) => {
console.error(config.heartbeatApiUrl, status, err.toString());
deferredObject.resolve(this);
}
});