From 8ada670417a6b7cbc22b14926c7c8a69b8743e20 Mon Sep 17 00:00:00 2001 From: Tyler Crammond Date: Tue, 23 Jun 2020 11:47:52 +0100 Subject: [PATCH] chore(background): Refactor "stop tracking on browser close" Fixes #1687. --- src/scripts/background.js | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index aecd17261..f71e39651 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -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) => { @@ -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) { @@ -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') {