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 6cea35f
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 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,14 +2263,14 @@ 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;
}

const windows = await browser.windows.getAll();
if (windows.length === 0 && TogglButton.$curEntry) {
await TogglButton.stopTimeEntry(TogglButton.$curEntry);
}
},

Expand Down Expand Up @@ -2368,27 +2366,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 6cea35f

Please sign in to comment.