Skip to content

Commit

Permalink
chore(background): Refactor "stop tracking on browser close"
Browse files Browse the repository at this point in the history
Fixes #1687.
  • Loading branch information
tcrammond committed Jun 23, 2020
1 parent b53aad8 commit 8ada670
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const FIVE_MINUTES = 5 * 60;
const ONE_HOUR = 60 * 60;
const RETRY_INTERVAL = 15;

let openWindowsCount = 0;

const FF = navigator.userAgent.indexOf('Chrome') === -1;

const shouldTriggerNotification = (state, seconds) => {
Expand Down Expand Up @@ -2265,15 +2263,17 @@ window.TogglButton = {
},

stopTrackingOnBrowserClosed: async function () {
openWindowsCount--;
const stopAutomatically = await db.get('stopAutomatically');
if (
stopAutomatically &&
openWindowsCount === 0 &&
TogglButton.$curEntry
) {
TogglButton.stopTimeEntry(TogglButton.$curEntry);
if (!stopAutomatically) {
return;
}

browser.windows.getAll().then(function (windows) {
if (windows.length === 0 && TogglButton.$curEntry) {
console.log('let us stop');
TogglButton.stopTimeEntry(TogglButton.$curEntry);
}
});
},

hasWorkspaceBeenRevoked: function (workspaces) {
Expand Down Expand Up @@ -2368,27 +2368,12 @@ if (!FF) {
browser.notifications.onButtonClicked.addListener(TogglButton.notificationBtnClick);
}
browser.windows.onCreated.addListener(TogglButton.startAutomatically);
browser.windows.getAll().then(function (windows) {
openWindowsCount = windows.length;
});
browser.windows.onCreated.addListener(function () {
openWindowsCount++;
});
browser.windows.onRemoved.addListener(TogglButton.stopTrackingOnBrowserClosed);

browser.runtime
.setUninstallURL('https://toggl.com/toggl-button-feedback/')
.catch(bugsnagClient.notify);

window.onbeforeunload = function () {
db.get('stopAutomatically')
.then((stopAutomatically) => {
if (stopAutomatically && TogglButton.$curEntry) {
TogglButton.stopTimeEntry(TogglButton.$curEntry);
}
});
};

// Check whether new version is installed
browser.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
Expand Down

0 comments on commit 8ada670

Please sign in to comment.