• 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.

    Parameters

    • previousState: PlaybackState

      Playback state before the track change

    • currentState: PlaybackState

      Current playback state after the track change

    Returns { isEdgeCase: boolean; edgeCase: string; shouldIgnore: boolean }

    Object with edge case information:

    • isEdgeCase: Whether a known edge case was detected
    • edgeCase: Description of the detected edge case
    • shouldIgnore: Whether the track change should be ignored for skip analysis
    // 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;
    }