Skip to content

Commit

Permalink
Reduce unnecessary requests (#127)
Browse files Browse the repository at this point in the history
* Reduce unnecessary requests.

* Clean state on installed.

* Update src/background/index.js
  • Loading branch information
YunzheZJU authored Jan 24, 2022
1 parent bccc67e commit f308087
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import store from 'store'
import { getUnix } from 'utils'
import workflows from 'workflows'

let shouldCleanState = false
let isStartUp = false

const ALARM_NAME = 'fetch-data-alarm'

Expand Down Expand Up @@ -38,11 +38,11 @@ const initOnce = async () => {
await i18n.init(store)
await workflows.init()

if (shouldCleanState) {
console.log('[background]Clean state on start up')
if (isStartUp) {
// Use new state
await clearCachedEndedLives()
await setIsPopupFirstRun(true)
console.log('[background]States have been cleared.')
}

requests.onSuccessRequest.addEventListener(() => {
Expand Down Expand Up @@ -70,7 +70,9 @@ const initOnce = async () => {
})
}

const initRetryable = () => Promise.all([syncOpenLives(), syncChannels(), syncMembers()])
const initRetryable = () => (
isStartUp ? Promise.all([syncOpenLives(), syncChannels(), syncMembers()]) : syncOpenLives()
)

const init = async () => {
await initOnce()
Expand All @@ -80,9 +82,14 @@ const init = async () => {
})
}

browser.runtime.onInstalled.addListener(async () => {
console.log('[background]on installed.')
isStartUp = true
})

browser.runtime.onStartup.addListener(async () => {
console.log('[background]on start up')
shouldCleanState = true
console.log('[background]on start up.')
isStartUp = true
})

init().then(() => console.log('[background]Init OK')).catch(err => {
Expand Down

0 comments on commit f308087

Please sign in to comment.