function storeUserInteraction()

in packages/rum-core/src/performance-monitoring/metrics/inp/process.js [149:187]


function storeUserInteraction(entry) {
  // Don't store interactions that are faster than the current ones
  // This also makes sure we don't store repeated values
  const leastSlow =
    inpState.longestInteractions[inpState.longestInteractions.length - 1]
  if (
    typeof leastSlow !== 'undefined' &&
    entry.duration <= leastSlow.duration &&
    entry.interactionId != leastSlow.id
  ) {
    return
  }

  /**
   * different entries can share the same interactionId
   * Please see the EventTiming spec for more details about
   * the algorithm: https://www.w3.org/TR/event-timing/#sec-computing-interactionid
   */
  const filteredInteraction = inpState.longestInteractions.filter(
    interaction => interaction.id === entry.interactionId
  )
  if (filteredInteraction.length > 0) {
    // Override existing interaction
    const foundInteraction = filteredInteraction[0]
    foundInteraction.duration = Math.max(
      foundInteraction.duration,
      entry.duration
    )
  } else {
    inpState.longestInteractions.push({
      id: entry.interactionId,
      duration: entry.duration
    })
  }

  // Sort the interactions to keep the slowest interactions at the end and keep only the max interactions
  inpState.longestInteractions.sort((a, b) => b.duration - a.duration)
  inpState.longestInteractions.splice(MAX_INTERACTIONS_TO_CONSIDER)
}