Skip to content

Auto-update the relative date #29

Open
@matthieusieben

Description

@matthieusieben

Relative dates have a particularity: being relative to the current time, which (sadly?) is ever increasing, they are themselves always changing.

The following decorator will allow to take this particularity into account, making the relative dates update themselves.

const getDigestDelay = (date) => {
  const _date = date instanceof Date ? date : new Date(date)
  const delta = Math.abs(new Date() - _date) / 1000
  if (delta < 60) return 5
  if (delta < 120) return 30
  if (delta < 7200) return 60
  return 600
}

export default function relativeDateFilterDecorator ($delegate, $timeout) {
  let timer = null

  relativeDateFilter.$stateful = true

  return relativeDateFilter

  function relativeDateFilter (date) {
    const delay = getDigestDelay(date)

    if (!timer || delay < timer.delay) {
      $timeout.cancel(timer)
      timer = $timeout(() => { timer = null }, delay * 1000, true)
      timer.delay = delay
    }

    return $delegate(date)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions