🐛 Stop stale running entries while offline
autoStopStale only ran through beforeSync, and syncNow skipped that while offline: an app left open overnight offline kept yesterday's entry running, and startEntry then threw "an entry is already running" for an entry the Today view does not show. A round now runs its preparation whatever the online state and only leaves out the requests, so the minute tick stops stale entries offline too, and startEntry stops them itself before it checks for a running entry.
This commit is contained in:
@@ -101,6 +101,7 @@ export function runningEntry(): Doc<Entry> | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function startEntry(note = ''): Promise<string> {
|
export async function startEntry(note = ''): Promise<string> {
|
||||||
|
await autoStopStale();
|
||||||
if (runningEntry()) throw new Error('an entry is already running');
|
if (runningEntry()) throw new Error('an entry is already running');
|
||||||
checkNote(note);
|
checkNote(note);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|||||||
@@ -131,6 +131,16 @@ describe('entries', () => {
|
|||||||
expect(await autoStopStale()).toBe(0);
|
expect(await autoStopStale()).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('startEntry stops a stale running entry first, also offline', async () => {
|
||||||
|
vi.useFakeTimers({ toFake: ['Date'] });
|
||||||
|
vi.setSystemTime(new Date('2026-06-16T10:00:00Z'));
|
||||||
|
await save('entries', null, { start_time: Date.parse('2026-06-15T17:00:00Z'), end_time: null, auto_stopped: false, note: 'yesterday', day_key: '2026-06-15' });
|
||||||
|
expect(status.online).toBe(false);
|
||||||
|
const id = await startEntry('today');
|
||||||
|
expect(entriesForDay('2026-06-15')[0]).toMatchObject({ end_time: Date.parse('2026-06-15T23:59:59.999Z'), auto_stopped: true });
|
||||||
|
expect(runningEntry()).toMatchObject({ id, note: 'today', day_key: '2026-06-16' });
|
||||||
|
});
|
||||||
|
|
||||||
it('startEntry twice throws an entry is already running', async () => {
|
it('startEntry twice throws an entry is already running', async () => {
|
||||||
await startEntry('first');
|
await startEntry('first');
|
||||||
await expect(startEntry('second')).rejects.toThrow('an entry is already running');
|
await expect(startEntry('second')).rejects.toThrow('an entry is already running');
|
||||||
|
|||||||
@@ -450,10 +450,11 @@ function remoteChange(): void {
|
|||||||
}, REMOTE_DEBOUNCE);
|
}, REMOTE_DEBOUNCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Runs the preparation and a round; offline only the preparation runs, which keeps the stale entries stopped. */
|
||||||
export function syncNow(): Promise<void> {
|
export function syncNow(): Promise<void> {
|
||||||
if (syncing) return syncing;
|
if (syncing) return syncing;
|
||||||
const m = sm;
|
const m = sm;
|
||||||
if (!m || !status.online) return Promise.resolve();
|
if (!m) return Promise.resolve();
|
||||||
if (!pb.authStore.isValid) {
|
if (!pb.authStore.isValid) {
|
||||||
sessionExpired?.();
|
sessionExpired?.();
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
@@ -461,7 +462,7 @@ export function syncNow(): Promise<void> {
|
|||||||
rejected = false;
|
rejected = false;
|
||||||
m.failures.length = 0;
|
m.failures.length = 0;
|
||||||
const run: Promise<void> = runBeforeSync()
|
const run: Promise<void> = runBeforeSync()
|
||||||
.then(() => m.syncAll())
|
.then(() => (status.online && m === sm ? m.syncAll() : undefined))
|
||||||
.catch((err: Error) => {
|
.catch((err: Error) => {
|
||||||
if (m !== sm) return;
|
if (m !== sm) return;
|
||||||
if (!pb.authStore.isValid || m.failures.some(unauthorized)) sessionExpired?.();
|
if (!pb.authStore.isValid || m.failures.some(unauthorized)) sessionExpired?.();
|
||||||
|
|||||||
@@ -426,6 +426,28 @@ describe('merge after apply', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('minute tick', () => {
|
||||||
|
it('runs the sync preparation also while offline', async () => {
|
||||||
|
await closeDb();
|
||||||
|
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
||||||
|
try {
|
||||||
|
await openDb(`user-${n}`);
|
||||||
|
const before = vi.fn(async () => undefined);
|
||||||
|
onBeforeSync(before);
|
||||||
|
expect(status.online).toBe(false);
|
||||||
|
const pulls = mocks.pulls.count;
|
||||||
|
vi.advanceTimersByTime(60_000);
|
||||||
|
await settled();
|
||||||
|
await settled();
|
||||||
|
expect(before).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mocks.pulls.count).toBe(pulls);
|
||||||
|
} finally {
|
||||||
|
await closeDb();
|
||||||
|
vi.useRealTimers();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('realtime and offline', () => {
|
describe('realtime and offline', () => {
|
||||||
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user