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(timer): timer stops when switching tabs #2228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .xstate/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const fetchMachine = createMachine({
}
},
running: {
entry: ["setStartTime"],
invoke: {
src: "interval",
id: "interval"
Expand Down
13 changes: 12 additions & 1 deletion packages/machines/timer/src/timer.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function machine(userContext: UserDefinedContext) {
interval: 250,
...ctx,
currentMs: ctx.startMs ?? 0,
startTimeMs: calculateStartTime(ctx.startMs ?? 0, ctx.countdown),
},

on: {
Expand Down Expand Up @@ -39,6 +40,7 @@ export function machine(userContext: UserDefinedContext) {
},
},
running: {
entry: ["setStartTime"],
every: {
TICK_INTERVAL: ["sendTickEvent"],
},
Expand Down Expand Up @@ -73,9 +75,12 @@ export function machine(userContext: UserDefinedContext) {
TICK_INTERVAL: (ctx) => ctx.interval,
},
actions: {
setStartTime(ctx) {
ctx.startTimeMs = calculateStartTime(ctx.currentMs, ctx.countdown)
},
updateTime(ctx) {
const sign = ctx.countdown ? -1 : 1
ctx.currentMs = ctx.currentMs + sign * ctx.interval
ctx.currentMs = sign * (Date.now() - ctx.startTimeMs)
},
sendTickEvent(_ctx, _evt, { send }) {
send({ type: "TICK" })
Expand All @@ -84,6 +89,7 @@ export function machine(userContext: UserDefinedContext) {
let targetMs = ctx.targetMs
if (targetMs == null && ctx.countdown) targetMs = 0
ctx.currentMs = ctx.startMs ?? 0
ctx.startTimeMs = calculateStartTime(ctx.currentMs, ctx.countdown)
},
invokeOnTick(ctx) {
ctx.onTick?.({
Expand All @@ -108,6 +114,11 @@ export function machine(userContext: UserDefinedContext) {
)
}

function calculateStartTime(currentMs: number, countdown?: boolean): number {
const sign = countdown ? -1 : 1
return Date.now() - sign * currentMs
}

function msToTime(ms: number): Time {
const milliseconds = ms % 1000
const seconds = Math.floor(ms / 1000) % 60
Expand Down
5 changes: 5 additions & 0 deletions packages/machines/timer/src/timer.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ interface PrivateContext {
* The timer count in milliseconds.
*/
currentMs: number
/**
* @internal
* Timestamp at the beginning of counting.
*/
startTimeMs: number
}

type ComputedContext = Readonly<{
Expand Down