utils#uniqRightBy JavaScript Examples

The following examples show how to use utils#uniqRightBy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: workflows.js    From holo-schedule with MIT License 6 votes vote down vote up
syncEndedLives = async () => {
  const cashedLives = getCachedEndedLives() ?? []
  const startBefore = cashedLives.length ? Math.min(
    ...cashedLives.map(({ start_at: startAt }) => getUnix(startAt)),
  ) + 1 : getUnix()

  const lives = filterLives(await getEndedLives({
    membersMask: getMembersMask(getSubscriptionByMember()),
    startAfter: getUnixBeforeDays(3),
    startBefore,
    limit: 25,
  }))

  await Promise.resolve({
    [ENDED_LIVES]: limitRight(uniqRightBy([...reverse(lives), ...cashedLives], 'id'), MAX_LIVES_LENGTH),
  }).then(data => store.set(data))
    .catch(data => store.set(data, { local: false }))

  return getCachedEndedLives()
}
Example #2
Source File: workflows.js    From holo-schedule with MIT License 6 votes vote down vote up
syncOpenLives = async () => {
  const [currentLives, scheduledLives] = partition(filterLives(await getOpenLives({
    membersMask: getMembersMask(getSubscriptionByMember()),
    startBefore: getUnixAfterDays(7),
  })), ({ start_at: startAt }) => dayjs().isAfter(startAt))

  await browser.action.setBadgeText({ text: currentLives.length.toString() })

  console.log(`[background/workflow]Badge text has been set to ${currentLives.length}`)

  // Subscription is simplified cause here is the only mutation of currentLives
  const endedLives = getCachedEndedLives() ?? []
  // Skip if endedLives is empty
  if (endedLives.length > 0) {
    differenceBy((getCachedCurrentLives() ?? []), currentLives, 'id').map(live => ({
      ...live, duration: dayjs().diff(dayjs(live['start_at']), 'second'),
    })).forEach(live => {
      const index = findLastIndex(
        endedLives,
        ({ start_at: startAt }) => startAt <= live['start_at'],
      )
      endedLives.splice(index + 1, 0, live)
    })
  }

  await Promise.resolve({
    [CURRENT_LIVES]: currentLives,
    [SCHEDULED_LIVES]: scheduledLives,
    [ENDED_LIVES]: limitRight(filterLives(uniqRightBy(endedLives, 'id')), MAX_LIVES_LENGTH),
  }).then(data => store.set(data))
    .catch(data => store.set(data, { local: false }))

  return [...currentLives, ...scheduledLives]
}