fix: save progress on player actions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { state } from './state';
|
||||
import { saveProgress } from './progress';
|
||||
|
||||
export const formatTime = (seconds: number): string => {
|
||||
if (!Number.isFinite(seconds) || seconds < 0) return '00:00';
|
||||
@@ -203,6 +204,7 @@ export const setupControls = (): void => {
|
||||
state.video.addEventListener('pause', () => {
|
||||
updatePlayPauseIcons(false);
|
||||
showControls();
|
||||
void saveProgress();
|
||||
});
|
||||
state.video.addEventListener('volumechange', syncVolumeUI);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
seekBy,
|
||||
setVolume,
|
||||
} from './controls';
|
||||
import { saveProgress } from './progress';
|
||||
|
||||
/**
|
||||
* Sets up keyboard shortcuts for player control.
|
||||
@@ -26,6 +27,7 @@ export const setupKeyboard = (): void => {
|
||||
e.preventDefault();
|
||||
togglePlayPause();
|
||||
showControls();
|
||||
void saveProgress();
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
case 'KeyJ':
|
||||
|
||||
@@ -16,11 +16,16 @@ const sendBeacon = (payload: string) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
let saveProgressInFlight: Promise<void> | null = null;
|
||||
|
||||
/**
|
||||
* Saves current progress to backend.
|
||||
* Debounced: skips if within 5s of last save for same episode.
|
||||
*/
|
||||
export const saveProgress = async (): Promise<void> => {
|
||||
if (saveProgressInFlight) return saveProgressInFlight;
|
||||
|
||||
const request = (async (): Promise<void> => {
|
||||
if (state.transitionEpisode !== null || !state.malID || state.video.currentTime < 1) return;
|
||||
// progress is user-scoped; avoid spamming 401s for anonymous sessions
|
||||
if (!document.cookie.includes('mal_session=')) return;
|
||||
@@ -32,8 +37,9 @@ export const saveProgress = async (): Promise<void> => {
|
||||
if (
|
||||
state.lastSavedProgress.episode === state.currentEpisode &&
|
||||
Math.abs(state.lastSavedProgress.seconds - safeTime) < 5
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = buildPayload(episode, safeTime);
|
||||
try {
|
||||
@@ -45,6 +51,16 @@ export const saveProgress = async (): Promise<void> => {
|
||||
if (!res.ok) return;
|
||||
state.lastSavedProgress = { episode: state.currentEpisode, seconds: safeTime };
|
||||
} catch {}
|
||||
})();
|
||||
|
||||
saveProgressInFlight = request;
|
||||
try {
|
||||
await request;
|
||||
} finally {
|
||||
if (saveProgressInFlight === request) {
|
||||
saveProgressInFlight = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// schedules periodic save every 30s during playback
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { state } from '../state';
|
||||
import { displayTimeFromAbsolute, absoluteTimeFromDisplay } from '../timeline';
|
||||
import { showControls } from '../controls';
|
||||
import { saveProgress } from '../progress';
|
||||
|
||||
// button label based on segment type
|
||||
const skipLabel = (type: string): string => (type === 'ed' ? 'Skip outro' : 'Skip intro');
|
||||
@@ -29,6 +30,7 @@ export const updateSkipButton = (currentTime: number): void => {
|
||||
const autoSkip = localStorage.getItem('mal:autoskip-enabled') === 'true';
|
||||
if (autoSkip && displayTime >= segment.start && displayTime < segment.end) {
|
||||
state.video.currentTime = absoluteTimeFromDisplay(segment.end + 0.01);
|
||||
void saveProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user