Files
Psychotoxical-psysonic/src/store/scheduleActions.ts
T
Frank Stellmacher 55b2b12fa5 refactor(player): E.36 — extract schedule-pause/resume factory (#600)
Four scheduled-timer actions extracted into `createScheduleActions`
(`src/store/scheduleActions.ts`):

- `schedulePauseIn` / `scheduleResumeIn` — clamp the delay to ≥ 500 ms,
  store absolute target + start timestamps (for the countdown UI),
  and arm a single-shot timer.
- `clearScheduledPause` / `clearScheduledResume` — cancel the timer
  and blank the timestamps.

Pure code-move. playerStore.ts: 1030 → 991 LOC (−39, first sub-1000
state since Phase E started).
2026-05-12 21:06:48 +02:00

64 lines
2.1 KiB
TypeScript

import type { PlayerState } from './playerStoreTypes';
import {
clearScheduledPauseTimers,
clearScheduledResumeTimers,
schedulePauseTimer,
scheduleResumeTimer,
} from './scheduleTimers';
type SetState = (
partial: Partial<PlayerState> | ((state: PlayerState) => Partial<PlayerState>),
) => void;
type GetState = () => PlayerState;
/**
* User-facing scheduled-pause / scheduled-resume actions. Each setter
* clamps the delay to ≥ 500 ms, stores the absolute target + start
* timestamps in the store (so countdown UI can render a progress arc),
* and arms a single-shot timer in `scheduleTimers.ts`. The matching
* `clearScheduled*` actions cancel the timer and blank the timestamps.
*/
export function createScheduleActions(set: SetState, get: GetState): Pick<
PlayerState,
'clearScheduledPause' | 'clearScheduledResume' | 'schedulePauseIn' | 'scheduleResumeIn'
> {
return {
clearScheduledPause: () => {
clearScheduledPauseTimers();
set({ scheduledPauseAtMs: null, scheduledPauseStartMs: null });
},
clearScheduledResume: () => {
clearScheduledResumeTimers();
set({ scheduledResumeAtMs: null, scheduledResumeStartMs: null });
},
schedulePauseIn: (seconds) => {
const s = get();
if (!s.isPlaying) return;
const delayMs = Math.max(500, Math.round(Number(seconds) * 1000));
const startedAt = Date.now();
const at = startedAt + delayMs;
set({ scheduledPauseAtMs: at, scheduledPauseStartMs: startedAt });
schedulePauseTimer(delayMs, () => {
set({ scheduledPauseAtMs: null, scheduledPauseStartMs: null });
get().pause();
});
},
scheduleResumeIn: (seconds) => {
const s = get();
if (s.isPlaying) return;
if (!s.currentTrack && !s.currentRadio) return;
const delayMs = Math.max(500, Math.round(Number(seconds) * 1000));
const startedAt = Date.now();
const at = startedAt + delayMs;
set({ scheduledResumeAtMs: at, scheduledResumeStartMs: startedAt });
scheduleResumeTimer(delayMs, () => {
set({ scheduledResumeAtMs: null, scheduledResumeStartMs: null });
get().resume();
});
},
};
}