Skip to content

Commit 4d3151c

Browse files
shantanurajtcrammond
authored andcommitted
fix(timer): Automatic start/stop on browser open/close
Closes #1375
1 parent 323ff23 commit 4d3151c

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/scripts/background.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,8 @@ window.TogglButton = {
137137
});
138138
}
139139
if (resp.data.time_entries) {
140-
resp.data.time_entries.some(function (timeEntry) {
141-
if (timeEntry.duration < 0) {
142-
entry = timeEntry;
143-
return true;
144-
}
145-
return false;
146-
});
140+
const { time_entries: timeEntries } = resp.data;
141+
entry = timeEntries.find(te => te.duration < 0) || null;
147142
}
148143

149144
if (TogglButton.hasWorkspaceBeenRevoked(resp.data.workspaces)) {
@@ -580,6 +575,11 @@ window.TogglButton = {
580575
});
581576
},
582577

578+
latestEntry: function () {
579+
const timeEntries = TogglButton.$user.time_entries || [ null ];
580+
return timeEntries[timeEntries.length - 1];
581+
},
582+
583583
checkPomodoroAlarm: async function (entry) {
584584
const duration = new Date() - new Date(entry.start);
585585
const pomodoroInterval = await db.get('pomodoroInterval');
@@ -2042,10 +2042,8 @@ window.TogglButton = {
20422042
startAutomatically &&
20432043
!!TogglButton.$user
20442044
) {
2045-
const lastEntryString = await db.get('latestStoppedEntry');
2046-
if (lastEntryString) {
2047-
const lastEntry = JSON.parse(lastEntryString);
2048-
TogglButton.$latestStoppedEntry = lastEntry;
2045+
TogglButton.$latestStoppedEntry = TogglButton.latestEntry();
2046+
if (TogglButton.$latestStoppedEntry) {
20492047
TogglButton.createTimeEntry(TogglButton.$latestStoppedEntry, null);
20502048
TogglButton.hideNotification('remind-to-track-time');
20512049
}

0 commit comments

Comments
 (0)