utils#getUnix JavaScript Examples
The following examples show how to use
utils#getUnix.
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 |
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: index.js From holo-schedule with MIT License | 5 votes |
initOnce = async () => {
global.workflows = workflows
global.store = store
global.alarm = alarm
await store.init()
await alarm.init(store)
await i18n.init(store)
await workflows.init()
if (isStartUp) {
// Use new state
await clearCachedEndedLives()
await setIsPopupFirstRun(true)
console.log('[background]States have been cleared.')
}
requests.onSuccessRequest.addEventListener(() => {
const lastSuccessRequestTime = getLastSuccessRequestTime() ?? getUnix()
const timestamp = getUnix()
if (timestamp - lastSuccessRequestTime > 60 * 5) {
clearCachedEndedLives()
}
setLastSuccessRequestTime(timestamp)
})
store.subscribe(ENDED_LIVES, async (lives, prevLives) => workflows.syncHotnesses(
reject(differenceBy(lives, prevLives, 'id'), 'hotnesses'),
))
browser.alarms.onAlarm.addListener(handleAlarm)
browser.alarms.create(ALARM_NAME, { periodInMinutes: 1 })
browser.runtime.sendMessage('background alive').catch(err => {
if (err.message.startsWith('Could not establish connection.')) {
console.log('[background]on message error. Keep calm, this error is in expect.')
} else {
console.log('[background]on message error', err.message)
}
})
}
Example #3
Source File: workflows.test.js From holo-schedule with MIT License | 5 votes |
test('should sync ended lives', async () => {
Date.now = jest.fn(() => unixTime * 1000)
// First run
const endedLivesOne = [
{ id: 10, start_at: dayjs().subtract(3, 'hour').toISOString() },
{ id: 9, start_at: dayjs().subtract(4, 'hour').toISOString() },
]
const returnValueExpectedOne = [endedLivesOne[1], endedLivesOne[0]]
getEndedLives.mockResolvedValueOnce(endedLivesOne)
const returnValueOne = await workflows.syncEndedLives()
expect(getEndedLives).toHaveBeenCalledWith({
startAfter: getUnixBeforeDays(3),
startBefore: getUnix(),
limit: 25,
})
expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedOne)
expect(returnValueOne).toEqual(returnValueExpectedOne)
// Second run
getEndedLives.mockClear()
const endedLivesTwo = [
{ id: 9, start_at: dayjs().subtract(4, 'hour').toISOString() },
{ id: 8, start_at: dayjs().subtract(5, 'hour').toISOString() },
]
const returnValueExpectedTwo = [endedLivesTwo[1], ...returnValueExpectedOne]
getEndedLives.mockResolvedValueOnce(endedLivesTwo)
const returnValueTwo = await workflows.syncEndedLives()
expect(getEndedLives).toHaveBeenCalledWith({
startAfter: getUnixBeforeDays(3),
startBefore: getUnix(returnValueExpectedOne[0].start_at) + 1,
limit: 25,
})
expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedTwo)
expect(returnValueTwo).toEqual(returnValueExpectedTwo)
// Third run
getEndedLives.mockClear()
const endedLivesThree = []
const returnValueExpectedThree = returnValueExpectedTwo
getEndedLives.mockResolvedValueOnce(endedLivesThree)
const returnValueThree = await workflows.syncEndedLives()
expect(getEndedLives).toHaveBeenCalledWith({
startAfter: getUnixBeforeDays(3),
startBefore: getUnix(returnValueExpectedTwo[0].start_at) + 1,
limit: 25,
})
expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedThree)
expect(returnValueThree).toEqual(returnValueExpectedThree)
})