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

View File

@@ -636,61 +636,50 @@ var WakaTime = (function () {
value: function recordHeartbeat() { value: function recordHeartbeat() {
var _this = this; var _this = this;
this.checkAuth().done(function (data) { chrome.storage.sync.get({
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, function (items) {
if (items.loggingEnabled === true) {
if (data !== false) { changeExtensionState('allGood');
chrome.storage.sync.get({ chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) {
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, function (items) {
if (items.loggingEnabled === true) {
changeExtensionState('allGood'); if (newState === 'active') {
// Get current tab URL.
chrome.tabs.query({ active: true }, function (tabs) {
chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { var currentActiveTab = tabs[0];
if (newState === 'active') { var debug = false;
// Get current tab URL. // If the current active tab has devtools open
chrome.tabs.query({ active: true }, function (tabs) { if (in_array(currentActiveTab.id, _this.tabsWithDevtoolsOpen)) debug = true;
var currentActiveTab = tabs[0]; 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.');
}
}
var debug = false; if (items.loggingStyle == 'whitelist') {
// If the current active tab has devtools open if (contains(currentActiveTab.url, items.whitelist)) {
if (in_array(currentActiveTab.id, _this.tabsWithDevtoolsOpen)) debug = true; _this.sendHeartbeat(currentActiveTab.url, debug);
} else {
if (items.loggingStyle == 'blacklist') { changeExtensionState('whitelisted');
if (!contains(currentActiveTab.url, items.blacklist)) { console.log(currentActiveTab.url + ' is not on a whitelist.');
_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 { } else {
changeExtensionState('notLogging');
// User is not logged in.
// Change extension icon to red color.
changeExtensionState('notSignedIn');
} }
}); });
} }
@@ -798,12 +787,16 @@ var WakaTime = (function () {
contentType: 'application/json', contentType: 'application/json',
method: method, method: method,
data: payload, data: payload,
statusCode: {
401: function _() {
changeExtensionState('notSignedIn');
},
201: function _() {}
},
success: function success(response) { success: function success(response) {
deferredObject.resolve(_this3); deferredObject.resolve(_this3);
}, },
error: function error(xhr, status, err) { error: function error(xhr, status, err) {
console.error(config.heartbeatApiUrl, status, err.toString()); console.error(config.heartbeatApiUrl, status, err.toString());
deferredObject.resolve(_this3); deferredObject.resolve(_this3);
@@ -820,6 +813,8 @@ var WakaTime = (function () {
exports['default'] = WakaTime; exports['default'] = WakaTime;
module.exports = exports['default']; module.exports = exports['default'];
// nothing to do here
},{"../helpers/changeExtensionState":8,"./../config":5,"./../helpers/contains":10,"./../helpers/getDomainFromUrl":11,"./../helpers/in_array":12,"jquery":26,"moment":28}],7:[function(require,module,exports){ },{"../helpers/changeExtensionState":8,"./../config":5,"./../helpers/contains":10,"./../helpers/getDomainFromUrl":11,"./../helpers/in_array":12,"jquery":26,"moment":28}],7:[function(require,module,exports){
/* global chrome */ /* global chrome */

View File

@@ -263,61 +263,50 @@ var WakaTime = (function () {
value: function recordHeartbeat() { value: function recordHeartbeat() {
var _this = this; var _this = this;
this.checkAuth().done(function (data) { chrome.storage.sync.get({
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, function (items) {
if (items.loggingEnabled === true) {
if (data !== false) { changeExtensionState('allGood');
chrome.storage.sync.get({ chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) {
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
blacklist: '',
whitelist: ''
}, function (items) {
if (items.loggingEnabled === true) {
changeExtensionState('allGood'); if (newState === 'active') {
// Get current tab URL.
chrome.tabs.query({ active: true }, function (tabs) {
chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { var currentActiveTab = tabs[0];
if (newState === 'active') { var debug = false;
// Get current tab URL. // If the current active tab has devtools open
chrome.tabs.query({ active: true }, function (tabs) { if (in_array(currentActiveTab.id, _this.tabsWithDevtoolsOpen)) debug = true;
var currentActiveTab = tabs[0]; 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.');
}
}
var debug = false; if (items.loggingStyle == 'whitelist') {
// If the current active tab has devtools open if (contains(currentActiveTab.url, items.whitelist)) {
if (in_array(currentActiveTab.id, _this.tabsWithDevtoolsOpen)) debug = true; _this.sendHeartbeat(currentActiveTab.url, debug);
} else {
if (items.loggingStyle == 'blacklist') { changeExtensionState('whitelisted');
if (!contains(currentActiveTab.url, items.blacklist)) { console.log(currentActiveTab.url + ' is not on a whitelist.');
_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 { } else {
changeExtensionState('notLogging');
// User is not logged in.
// Change extension icon to red color.
changeExtensionState('notSignedIn');
} }
}); });
} }
@@ -425,12 +414,16 @@ var WakaTime = (function () {
contentType: 'application/json', contentType: 'application/json',
method: method, method: method,
data: payload, data: payload,
statusCode: {
401: function _() {
changeExtensionState('notSignedIn');
},
201: function _() {}
},
success: function success(response) { success: function success(response) {
deferredObject.resolve(_this3); deferredObject.resolve(_this3);
}, },
error: function error(xhr, status, err) { error: function error(xhr, status, err) {
console.error(config.heartbeatApiUrl, status, err.toString()); console.error(config.heartbeatApiUrl, status, err.toString());
deferredObject.resolve(_this3); deferredObject.resolve(_this3);
@@ -447,6 +440,8 @@ var WakaTime = (function () {
exports['default'] = WakaTime; exports['default'] = WakaTime;
module.exports = exports['default']; module.exports = exports['default'];
// nothing to do here
},{"../helpers/changeExtensionState":5,"./../config":2,"./../helpers/contains":7,"./../helpers/getDomainFromUrl":8,"./../helpers/in_array":9,"jquery":10,"moment":11}],4:[function(require,module,exports){ },{"../helpers/changeExtensionState":5,"./../config":2,"./../helpers/contains":7,"./../helpers/getDomainFromUrl":8,"./../helpers/in_array":9,"jquery":10,"moment":11}],4:[function(require,module,exports){
/* global chrome */ /* global chrome */