Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX count wall time instead of CPU time #9

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 45 additions & 5 deletions src/components/Pomodoro.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const Pomodoro = ({ name }) => {
const [isActive, setIsActive] = useState(false);
const [opened, setOpened] = useState(false);
const [sound, setSound] = useState(null);

const [previousTimestamp, setPreviousTimestamp] = useState(null);

useEffect(() => {
var sound = new Audio(pomodoroSound);

Expand All @@ -69,10 +70,45 @@ const Pomodoro = ({ name }) => {
cleanNotifications();

const interval = setInterval(() => {
setSecondsLeft(secondsLeft => secondsLeft - 1);
/*
On first run and after resets, `previousTimestamp` will
be null and we have to artificially subtract one second
to trigger a change in delta.
*/
const refTimestamp = previousTimestamp === null? (Date.now()-1000):previousTimestamp

/*
CPU time vs Wall time
When a browser tab or window is off-screen
or in a tab that isn't focused, the scheduler
doesn't execute the JS engine every
tick/second. Instead, the process is scheduled
to run in less frequent intervals. This means
that one can't assume that the interval timer
will execute once every wall time second, but
rather once every "JS engine active" second.
Since the countdown should count wall time,
we need to calculate a delta for when the
function last ran.
*/
const delta = Math.round((Date.now()-refTimestamp)/1000)

/*
We then subtract the delta time i.e. the
time that has passed since last function
execution.
*/
setSecondsLeft(secondsLeft => secondsLeft - delta);

// Update timestamp for last execution
setPreviousTimestamp(Date.now())
}, 1000);

if (secondsLeft === 0) {
/*
secondsLeft can be less than 0 if the
browser tab/window is running in the background
*/
if (secondsLeft <= 0) {
sound.play();
clearInterval(interval);
restartPomodoro();
Expand All @@ -87,7 +123,7 @@ const Pomodoro = ({ name }) => {
});
}

if (secondsLeft === 0 && mode == "pomodoro") {
if (secondsLeft <= 0 && mode == "pomodoro") {
/*setPomodorosToday(prevState => prevState + 1);*/
setStorage({
...storage,
Expand All @@ -101,6 +137,7 @@ const Pomodoro = ({ name }) => {

const restartPomodoro = () => {
setIsActive(false);
setPreviousTimestamp(null)

switch (mode) {
case "short":
Expand Down Expand Up @@ -187,7 +224,10 @@ const Pomodoro = ({ name }) => {
<ActionIcon
color="red"
variant="light"
onClick={() => setIsActive(false)}
onClick={() => {
setIsActive(false)
setPreviousTimestamp(null)
}}
aria-label="Pause pomodoro"
>
<IconPlayerPause size={18} />
Expand Down