Skip to content

Commit

Permalink
prevent API request every time checkAuth is called from switching tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Basic committed Jun 26, 2015
1 parent 8b77268 commit 12b153b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 162 deletions.
109 changes: 51 additions & 58 deletions assets/js/core/WakaTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
}
Expand Down Expand Up @@ -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);

}
});

Expand Down
99 changes: 47 additions & 52 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
}
Expand Down Expand Up @@ -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);
Expand All @@ -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 */

Expand Down
99 changes: 47 additions & 52 deletions public/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
}
Expand Down Expand Up @@ -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);
Expand All @@ -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 */

Expand Down

0 comments on commit 12b153b

Please sign in to comment.