From 12b153b3233d8b664901aab47dba526a3e82ce90 Mon Sep 17 00:00:00 2001 From: Mario Basic Date: Sat, 27 Jun 2015 00:20:39 +0200 Subject: [PATCH] prevent API request every time checkAuth is called from switching tabs --- assets/js/core/WakaTime.js | 109 +++++++++++++++++-------------------- public/js/app.js | 99 ++++++++++++++++----------------- public/js/events.js | 99 ++++++++++++++++----------------- 3 files changed, 145 insertions(+), 162 deletions(-) diff --git a/assets/js/core/WakaTime.js b/assets/js/core/WakaTime.js index 32e136f7..2df04fd7 100644 --- a/assets/js/core/WakaTime.js +++ b/assets/js/core/WakaTime.js @@ -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) { - - changeExtensionState('allGood'); - - chrome.idle.queryState(config.detectionIntervalInSeconds, (newState) => { - - if (newState === 'active') { - // 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; - - 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.'); - } - } - - }); + + chrome.storage.sync.get({ + loggingEnabled: config.loggingEnabled, + loggingStyle: config.loggingStyle, + blacklist: '', + whitelist: '' + }, (items) => { + if (items.loggingEnabled === true) { + + changeExtensionState('allGood'); + + chrome.idle.queryState(config.detectionIntervalInSeconds, (newState) => { + + if (newState === 'active') { + // 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; + + 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); - } }); diff --git a/public/js/app.js b/public/js/app.js index 15fa7f06..17a5b193 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -636,61 +636,50 @@ var WakaTime = (function () { value: function recordHeartbeat() { var _this = this; - this.checkAuth().done(function (data) { - - if (data !== false) { - - chrome.storage.sync.get({ - loggingEnabled: config.loggingEnabled, - loggingStyle: config.loggingStyle, - blacklist: '', - whitelist: '' - }, function (items) { - if (items.loggingEnabled === true) { - - changeExtensionState('allGood'); - - chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { - - if (newState === 'active') { - // Get current tab URL. - chrome.tabs.query({ active: true }, function (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; - - 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.'); - } - } - }); + chrome.storage.sync.get({ + loggingEnabled: config.loggingEnabled, + loggingStyle: config.loggingStyle, + blacklist: '', + whitelist: '' + }, function (items) { + if (items.loggingEnabled === true) { + + changeExtensionState('allGood'); + + chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { + + if (newState === 'active') { + // Get current tab URL. + chrome.tabs.query({ active: true }, function (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; + + 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'); } }); } @@ -798,12 +787,16 @@ var WakaTime = (function () { contentType: 'application/json', method: method, data: payload, + statusCode: { + 401: function _() { + changeExtensionState('notSignedIn'); + }, + 201: function _() {} + }, success: function success(response) { - deferredObject.resolve(_this3); }, error: function error(xhr, status, err) { - console.error(config.heartbeatApiUrl, status, err.toString()); deferredObject.resolve(_this3); @@ -820,6 +813,8 @@ var WakaTime = (function () { exports['default'] = WakaTime; 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){ /* global chrome */ diff --git a/public/js/events.js b/public/js/events.js index e09774c2..338d6581 100644 --- a/public/js/events.js +++ b/public/js/events.js @@ -263,61 +263,50 @@ var WakaTime = (function () { value: function recordHeartbeat() { var _this = this; - this.checkAuth().done(function (data) { - - if (data !== false) { - - chrome.storage.sync.get({ - loggingEnabled: config.loggingEnabled, - loggingStyle: config.loggingStyle, - blacklist: '', - whitelist: '' - }, function (items) { - if (items.loggingEnabled === true) { - - changeExtensionState('allGood'); - - chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { - - if (newState === 'active') { - // Get current tab URL. - chrome.tabs.query({ active: true }, function (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; - - 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.'); - } - } - }); + chrome.storage.sync.get({ + loggingEnabled: config.loggingEnabled, + loggingStyle: config.loggingStyle, + blacklist: '', + whitelist: '' + }, function (items) { + if (items.loggingEnabled === true) { + + changeExtensionState('allGood'); + + chrome.idle.queryState(config.detectionIntervalInSeconds, function (newState) { + + if (newState === 'active') { + // Get current tab URL. + chrome.tabs.query({ active: true }, function (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; + + 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'); } }); } @@ -425,12 +414,16 @@ var WakaTime = (function () { contentType: 'application/json', method: method, data: payload, + statusCode: { + 401: function _() { + changeExtensionState('notSignedIn'); + }, + 201: function _() {} + }, success: function success(response) { - deferredObject.resolve(_this3); }, error: function error(xhr, status, err) { - console.error(config.heartbeatApiUrl, status, err.toString()); deferredObject.resolve(_this3); @@ -447,6 +440,8 @@ var WakaTime = (function () { exports['default'] = WakaTime; 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){ /* global chrome */