utils#getUnixAfterDays JavaScript Examples

The following examples show how to use utils#getUnixAfterDays. 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
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]
}
Example #2
Source File: workflows.test.js    From holo-schedule with MIT License 4 votes vote down vote up
test('should sync open lives', async () => {
  Date.now = jest.fn(() => unixTime * 1000)

  // First run
  const currentLivesOne = [
    {
      id: 1,
      start_at: dayjs().subtract(3, 'hour').toISOString(),
      duration: null,
    },
    {
      id: 2,
      start_at: dayjs().subtract(3, 'hour').toISOString(),
      duration: null,
    },
    {
      id: 3,
      start_at: dayjs().subtract(2, 'hour').toISOString(),
      duration: null,
    },
  ]
  const scheduledLivesOne = [
    {
      id: 4,
      start_at: dayjs().add(1, 'hour').toISOString(),
      duration: null,
    },
    {
      id: 5,
      start_at: dayjs().add(2, 'hour').toISOString(),
      duration: null,
    },
  ]
  const openLivesOne = [...currentLivesOne, ...scheduledLivesOne]
  const endedLivesOne = []

  getOpenLives.mockResolvedValueOnce(openLivesOne)

  const returnValueOne = await workflows.syncOpenLives()

  expect(getOpenLives).toHaveBeenCalledWith({
    membersMask: undefined,
    startBefore: getUnixAfterDays(7),
  })
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledTimes(1)
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith({ text: '3' })
  expect(store.data[CURRENT_LIVES]).toEqual(currentLivesOne)
  expect(store.data[SCHEDULED_LIVES]).toEqual(scheduledLivesOne)
  expect(store.data[ENDED_LIVES]).toEqual(endedLivesOne)
  expect(returnValueOne).toEqual(openLivesOne)

  getOpenLives.mockClear()
  browser.browserAction.setBadgeText.mockClear()

  // Second run
  const currentLivesTwo = [
    openLivesOne[1],
    openLivesOne[2],
    {
      ...scheduledLivesOne[0],
      start_at: dayjs().subtract(1, 'hour').toISOString(),
    },
  ]
  const scheduledLivesTwo = [
    scheduledLivesOne[1],
  ]
  const openLivesTwo = [...currentLivesTwo, ...scheduledLivesTwo]
  const endedLivesTwo = [
    {
      ...openLivesOne[0],
      duration: dayjs.duration(3, 'hour').as('second'),
    },
  ]

  getOpenLives.mockResolvedValueOnce(openLivesTwo)

  const returnValueTwo = await workflows.syncOpenLives()

  expect(browser.browserAction.setBadgeText).toHaveBeenCalledTimes(1)
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith({ text: '3' })
  expect(store.data[SCHEDULED_LIVES]).toEqual(scheduledLivesTwo)
  expect(store.data[CURRENT_LIVES]).toEqual(currentLivesTwo)
  expect(store.data[ENDED_LIVES]).toEqual([])
  expect(returnValueTwo).toEqual(openLivesTwo)

  getOpenLives.mockClear()
  browser.browserAction.setBadgeText.mockClear()

  // Second run second
  await store.set({ [ENDED_LIVES]: endedLivesTwo })

  getOpenLives.mockResolvedValueOnce(openLivesTwo)

  const returnValueTwoSecond = await workflows.syncOpenLives()

  expect(browser.browserAction.setBadgeText).toHaveBeenCalledTimes(1)
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith({ text: '3' })
  expect(store.data[SCHEDULED_LIVES]).toEqual(scheduledLivesTwo)
  expect(store.data[CURRENT_LIVES]).toEqual(currentLivesTwo)
  expect(store.data[ENDED_LIVES]).toEqual(endedLivesTwo)
  expect(returnValueTwoSecond).toEqual(openLivesTwo)

  getOpenLives.mockClear()
  browser.browserAction.setBadgeText.mockClear()

  // Third run
  const currentLivesThree = [
    { ...endedLivesTwo[0], duration: null },
    openLivesTwo[0],
    openLivesTwo[2],
  ]
  const scheduledLivesThree = []
  const openLivesThree = [...currentLivesThree, ...scheduledLivesThree]
  const endedLivesThree = [
    endedLivesTwo[0],
    {
      ...openLivesOne[2],
      duration: dayjs.duration(2, 'hour').as('second'),
    },
  ]

  getOpenLives.mockResolvedValueOnce(openLivesThree)

  const returnValueThree = await workflows.syncOpenLives()

  expect(browser.browserAction.setBadgeText).toHaveBeenCalledTimes(1)
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith({ text: '3' })
  expect(store.data[CURRENT_LIVES]).toEqual(currentLivesThree)
  expect(store.data[SCHEDULED_LIVES]).toEqual(scheduledLivesThree)
  expect(store.data[ENDED_LIVES]).toEqual(endedLivesThree)
  expect(returnValueThree).toEqual(openLivesThree)

  getOpenLives.mockClear()
  browser.browserAction.setBadgeText.mockClear()

  // Fourth run
  const currentLivesFour = []
  const scheduledLivesFour = []
  const openLivesFour = [...currentLivesFour, ...scheduledLivesFour]
  const endedLivesFour = [
    endedLivesThree[0],
    {
      ...openLivesThree[1],
      duration: dayjs.duration(3, 'hour').as('second'),
    },
    endedLivesThree[1],
    {
      ...openLivesThree[2],
      duration: dayjs.duration(1, 'hour').as('second'),
    },
  ]

  getOpenLives.mockResolvedValueOnce(openLivesFour)

  const returnValueFour = await workflows.syncOpenLives()

  expect(browser.browserAction.setBadgeText).toHaveBeenCalledTimes(1)
  expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith({ text: '0' })
  expect(store.data[CURRENT_LIVES]).toEqual(currentLivesFour)
  expect(store.data[SCHEDULED_LIVES]).toEqual(scheduledLivesFour)
  expect(store.data[ENDED_LIVES]).toEqual(endedLivesFour)
  expect(returnValueFour).toEqual(openLivesFour)
})