fix(anilist): pass fresh time-pos to post-watch threshold check

- Thread live mpv time-position through to AniList watched-seconds check
- Prevents missed progress updates when the cached value lags behind playback
This commit is contained in:
2026-05-16 17:48:55 -07:00
parent a36e628512
commit 215e0f804b
8 changed files with 86 additions and 8 deletions
+4
View File
@@ -0,0 +1,4 @@
type: fixed
area: anilist
- Used fresh mpv time-position events for AniList post-watch threshold checks so progress updates still fire when playback reaches the watched threshold.
+1 -1
View File
@@ -3986,7 +3986,7 @@ const {
reportJellyfinRemoteStopped: () => { reportJellyfinRemoteStopped: () => {
void reportJellyfinRemoteStopped(); void reportJellyfinRemoteStopped();
}, },
maybeRunAnilistPostWatchUpdate: () => maybeRunAnilistPostWatchUpdate(), maybeRunAnilistPostWatchUpdate: (options) => maybeRunAnilistPostWatchUpdate(options),
logSubtitleTimingError: (message, error) => logger.error(message, error), logSubtitleTimingError: (message, error) => logger.error(message, error),
broadcastToOverlayWindows: (channel, payload) => { broadcastToOverlayWindows: (channel, payload) => {
broadcastToOverlayWindows(channel, payload); broadcastToOverlayWindows(channel, payload);
@@ -121,6 +121,46 @@ test('createMaybeRunAnilistPostWatchUpdateHandler force-runs manual watched upda
assert.ok(calls.includes('osd:updated ok')); assert.ok(calls.includes('osd:updated ok'));
}); });
test('createMaybeRunAnilistPostWatchUpdateHandler uses provided watched seconds from time-position events', async () => {
const calls: string[] = [];
const handler = createMaybeRunAnilistPostWatchUpdateHandler({
getInFlight: () => false,
setInFlight: (value) => calls.push(`inflight:${value}`),
getResolvedConfig: () => ({}),
isAnilistTrackingEnabled: () => true,
getCurrentMediaKey: () => '/tmp/video.mkv',
hasMpvClient: () => true,
getTrackedMediaKey: () => '/tmp/video.mkv',
resetTrackedMedia: () => {},
getWatchedSeconds: () => 0,
maybeProbeAnilistDuration: async () => 1000,
ensureAnilistMediaGuess: async () => ({ title: 'Show', season: null, episode: 8 }),
hasAttemptedUpdateKey: () => false,
processNextAnilistRetryUpdate: async () => ({ ok: true, message: 'noop' }),
refreshAnilistClientSecretState: async () => 'token',
enqueueRetry: () => calls.push('enqueue'),
markRetryFailure: () => calls.push('mark-failure'),
markRetrySuccess: () => calls.push('mark-success'),
refreshRetryQueueState: () => calls.push('refresh'),
updateAnilistPostWatchProgress: async () => {
calls.push('update');
return { status: 'updated', message: 'updated ok' };
},
rememberAttemptedUpdateKey: () => calls.push('remember'),
showMpvOsd: (message) => calls.push(`osd:${message}`),
logInfo: (message) => calls.push(`info:${message}`),
logWarn: (message) => calls.push(`warn:${message}`),
minWatchSeconds: 600,
minWatchRatio: 0.85,
});
await handler({ watchedSeconds: 850 });
assert.ok(calls.includes('update'));
assert.ok(calls.includes('remember'));
assert.ok(calls.includes('osd:updated ok'));
});
test('createMaybeRunAnilistPostWatchUpdateHandler blocks concurrent runs before async gating', async () => { test('createMaybeRunAnilistPostWatchUpdateHandler blocks concurrent runs before async gating', async () => {
const calls: string[] = []; const calls: string[] = [];
let inFlight = false; let inFlight = false;
+5 -1
View File
@@ -18,6 +18,7 @@ type RetryQueueItem = {
type AnilistPostWatchRunOptions = { type AnilistPostWatchRunOptions = {
force?: boolean; force?: boolean;
watchedSeconds?: number;
}; };
export function buildAnilistAttemptKey(mediaKey: string, episode: number): string { export function buildAnilistAttemptKey(mediaKey: string, episode: number): string {
@@ -146,7 +147,10 @@ export function createMaybeRunAnilistPostWatchUpdateHandler(deps: {
let watchedSeconds = 0; let watchedSeconds = 0;
if (!force) { if (!force) {
watchedSeconds = deps.getWatchedSeconds(); watchedSeconds =
typeof options.watchedSeconds === 'number' && Number.isFinite(options.watchedSeconds)
? options.watchedSeconds
: deps.getWatchedSeconds();
if (!Number.isFinite(watchedSeconds) || watchedSeconds < deps.minWatchSeconds) { if (!Number.isFinite(watchedSeconds) || watchedSeconds < deps.minWatchSeconds) {
return; return;
} }
@@ -223,6 +223,23 @@ test('time-pos and pause handlers report progress with correct urgency', () => {
]); ]);
}); });
test('time-pos handler passes fresh playback time to AniList post-watch', async () => {
const watchedSeconds: unknown[] = [];
const timeHandler = createHandleMpvTimePosChangeHandler({
recordPlaybackPosition: () => {},
reportJellyfinRemoteProgress: () => {},
refreshDiscordPresence: () => {},
maybeRunAnilistPostWatchUpdate: async (options) => {
watchedSeconds.push(options?.watchedSeconds);
},
});
timeHandler({ time: 850 });
await Promise.resolve();
assert.deepEqual(watchedSeconds, [850]);
});
test('time-pos handler logs post-watch update rejection without blocking later handlers', async () => { test('time-pos handler logs post-watch update rejection without blocking later handlers', async () => {
const calls: string[] = []; const calls: string[] = [];
const timeHandler = createHandleMpvTimePosChangeHandler({ const timeHandler = createHandleMpvTimePosChangeHandler({
+6 -2
View File
@@ -1,5 +1,9 @@
import type { SubtitleData } from '../../types'; import type { SubtitleData } from '../../types';
type AnilistPostWatchRunOptions = {
watchedSeconds?: number;
};
export function createHandleMpvSubtitleChangeHandler(deps: { export function createHandleMpvSubtitleChangeHandler(deps: {
setCurrentSubText: (text: string) => void; setCurrentSubText: (text: string) => void;
getImmediateSubtitlePayload?: (text: string) => SubtitleData | null; getImmediateSubtitlePayload?: (text: string) => SubtitleData | null;
@@ -105,7 +109,7 @@ export function createHandleMpvTimePosChangeHandler(deps: {
recordPlaybackPosition: (time: number) => void; recordPlaybackPosition: (time: number) => void;
reportJellyfinRemoteProgress: (forceImmediate: boolean) => void; reportJellyfinRemoteProgress: (forceImmediate: boolean) => void;
refreshDiscordPresence: () => void; refreshDiscordPresence: () => void;
maybeRunAnilistPostWatchUpdate?: () => Promise<void>; maybeRunAnilistPostWatchUpdate?: (options?: AnilistPostWatchRunOptions) => Promise<void>;
logError?: (message: string, error: unknown) => void; logError?: (message: string, error: unknown) => void;
onTimePosUpdate?: (time: number) => void; onTimePosUpdate?: (time: number) => void;
}) { }) {
@@ -113,7 +117,7 @@ export function createHandleMpvTimePosChangeHandler(deps: {
deps.recordPlaybackPosition(time); deps.recordPlaybackPosition(time);
deps.reportJellyfinRemoteProgress(false); deps.reportJellyfinRemoteProgress(false);
deps.refreshDiscordPresence(); deps.refreshDiscordPresence();
void deps.maybeRunAnilistPostWatchUpdate?.().catch((error) => { void deps.maybeRunAnilistPostWatchUpdate?.({ watchedSeconds: time }).catch((error) => {
deps.logError?.('AniList post-watch update failed unexpectedly', error); deps.logError?.('AniList post-watch update failed unexpectedly', error);
}); });
deps.onTimePosUpdate?.(time); deps.onTimePosUpdate?.(time);
+6 -2
View File
@@ -18,6 +18,10 @@ import {
type MpvEventClient = Parameters<ReturnType<typeof createBindMpvClientEventHandlers>>[0]; type MpvEventClient = Parameters<ReturnType<typeof createBindMpvClientEventHandlers>>[0];
type AnilistPostWatchRunOptions = {
watchedSeconds?: number;
};
export function createBindMpvMainEventHandlersHandler(deps: { export function createBindMpvMainEventHandlersHandler(deps: {
reportJellyfinRemoteStopped: () => void; reportJellyfinRemoteStopped: () => void;
syncOverlayMpvSubtitleSuppression: () => void; syncOverlayMpvSubtitleSuppression: () => void;
@@ -34,7 +38,7 @@ export function createBindMpvMainEventHandlersHandler(deps: {
recordImmersionSubtitleLine: (text: string, start: number, end: number) => void; recordImmersionSubtitleLine: (text: string, start: number, end: number) => void;
hasSubtitleTimingTracker: () => boolean; hasSubtitleTimingTracker: () => boolean;
recordSubtitleTiming: (text: string, start: number, end: number) => void; recordSubtitleTiming: (text: string, start: number, end: number) => void;
maybeRunAnilistPostWatchUpdate: () => Promise<void>; maybeRunAnilistPostWatchUpdate: (options?: AnilistPostWatchRunOptions) => Promise<void>;
logSubtitleTimingError: (message: string, error: unknown) => void; logSubtitleTimingError: (message: string, error: unknown) => void;
setCurrentSubText: (text: string) => void; setCurrentSubText: (text: string) => void;
@@ -149,7 +153,7 @@ export function createBindMpvMainEventHandlersHandler(deps: {
reportJellyfinRemoteProgress: (forceImmediate) => reportJellyfinRemoteProgress: (forceImmediate) =>
deps.reportJellyfinRemoteProgress(forceImmediate), deps.reportJellyfinRemoteProgress(forceImmediate),
refreshDiscordPresence: () => deps.refreshDiscordPresence(), refreshDiscordPresence: () => deps.refreshDiscordPresence(),
maybeRunAnilistPostWatchUpdate: () => deps.maybeRunAnilistPostWatchUpdate(), maybeRunAnilistPostWatchUpdate: (options) => deps.maybeRunAnilistPostWatchUpdate(options),
logError: (message, error) => deps.logSubtitleTimingError(message, error), logError: (message, error) => deps.logSubtitleTimingError(message, error),
onTimePosUpdate: (time) => deps.onTimePosUpdate?.(time), onTimePosUpdate: (time) => deps.onTimePosUpdate?.(time),
}); });
+7 -2
View File
@@ -1,5 +1,9 @@
import type { MergedToken, SubtitleData } from '../../types'; import type { MergedToken, SubtitleData } from '../../types';
type AnilistPostWatchRunOptions = {
watchedSeconds?: number;
};
export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: { export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: {
appState: { appState: {
initialArgs?: { initialArgs?: {
@@ -42,7 +46,7 @@ export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: {
quitApp: () => void; quitApp: () => void;
reportJellyfinRemoteStopped: () => void; reportJellyfinRemoteStopped: () => void;
syncOverlayMpvSubtitleSuppression: () => void; syncOverlayMpvSubtitleSuppression: () => void;
maybeRunAnilistPostWatchUpdate: () => Promise<void>; maybeRunAnilistPostWatchUpdate: (options?: AnilistPostWatchRunOptions) => Promise<void>;
logSubtitleTimingError: (message: string, error: unknown) => void; logSubtitleTimingError: (message: string, error: unknown) => void;
broadcastToOverlayWindows: (channel: string, payload: unknown) => void; broadcastToOverlayWindows: (channel: string, payload: unknown) => void;
getImmediateSubtitlePayload?: (text: string) => SubtitleData | null; getImmediateSubtitlePayload?: (text: string) => SubtitleData | null;
@@ -126,7 +130,8 @@ export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: {
hasSubtitleTimingTracker: () => Boolean(deps.appState.subtitleTimingTracker), hasSubtitleTimingTracker: () => Boolean(deps.appState.subtitleTimingTracker),
recordSubtitleTiming: (text: string, start: number, end: number) => recordSubtitleTiming: (text: string, start: number, end: number) =>
deps.appState.subtitleTimingTracker?.recordSubtitle?.(text, start, end), deps.appState.subtitleTimingTracker?.recordSubtitle?.(text, start, end),
maybeRunAnilistPostWatchUpdate: () => deps.maybeRunAnilistPostWatchUpdate(), maybeRunAnilistPostWatchUpdate: (options?: AnilistPostWatchRunOptions) =>
deps.maybeRunAnilistPostWatchUpdate(options),
logSubtitleTimingError: (message: string, error: unknown) => logSubtitleTimingError: (message: string, error: unknown) =>
deps.logSubtitleTimingError(message, error), deps.logSubtitleTimingError(message, error),
setCurrentSubText: (text: string) => { setCurrentSubText: (text: string) => {