Playback state before the track change
Current playback state after the track change
Object with edge case information:
// Check for edge cases before processing a track change
const edgeCase = handleTrackChangeEdgeCases(previousState, currentState);
if (edgeCase.isEdgeCase) {
console.log(`Edge case detected: ${edgeCase.edgeCase}`);
if (edgeCase.shouldIgnore) {
console.log('Skipping track change analysis due to edge case');
return;
}
}
export function handleTrackChangeEdgeCases(
previousState: PlaybackState,
currentState: PlaybackState,
): {
isEdgeCase: boolean;
edgeCase: string;
shouldIgnore: boolean;
} {
// Default response
const result = {
isEdgeCase: false,
edgeCase: "",
shouldIgnore: false,
};
// Validate inputs to avoid errors
if (!previousState || !currentState) {
return result;
}
// Edge Case 1: App switching or reconnection
if (
previousState.lastUpdated &&
currentState.lastUpdated &&
currentState.lastUpdated - previousState.lastUpdated > 1000 * 60 * 5 // 5 minutes
) {
return {
isEdgeCase: true,
edgeCase: "app_reconnection",
shouldIgnore: true,
};
}
// Edge Case 2: Very short connection gap (likely temporary connection loss)
if (
previousState.lastUpdated &&
currentState.lastUpdated &&
currentState.lastUpdated - previousState.lastUpdated > 1000 * 10 && // 10 seconds
currentState.lastUpdated - previousState.lastUpdated < 1000 * 60 // 1 minute
) {
return {
isEdgeCase: true,
edgeCase: "brief_connection_loss",
shouldIgnore: false, // Still process but flag it
};
}
return result;
}
Handles special edge cases in track change detection
Identifies and processes special situations that might affect skip detection accuracy, such as device changes, app restarts, or network interruptions. This helps prevent false positives and improves the reliability of skip analytics.