feat: stop extension status polling when a status is resolved

This commit is contained in:
Andrew Bastin
2021-12-17 15:11:48 +05:30
parent 655e6dc2a4
commit 156011f2cd
3 changed files with 40 additions and 18 deletions

View File

@@ -164,16 +164,29 @@ export function useColorMode() {
export function usePolled<T>( export function usePolled<T>(
pollDurationMS: number, pollDurationMS: number,
pollFunc: () => T pollFunc: (stopPolling: () => void) => T
): Ref<T> { ): Ref<T> {
const result = shallowRef(pollFunc()) let polling = true
let handle: ReturnType<typeof setInterval> | undefined
const handle = setInterval(() => { const stopPolling = () => {
result.value = pollFunc() if (handle) {
}, pollDurationMS) clearInterval(handle)
handle = undefined
polling = false
}
}
const result = shallowRef(pollFunc(stopPolling))
if (polling) {
handle = setInterval(() => {
result.value = pollFunc(stopPolling)
}, pollDurationMS)
}
onBeforeUnmount(() => { onBeforeUnmount(() => {
clearInterval(handle) if (polling) stopPolling()
}) })
return result return result

View File

@@ -57,11 +57,8 @@ import { logPageView } from "~/helpers/fb/analytics"
import { hookKeybindingsListener } from "~/helpers/keybindings" import { hookKeybindingsListener } from "~/helpers/keybindings"
import { defineActionHandler } from "~/helpers/actions" import { defineActionHandler } from "~/helpers/actions"
import useWindowSize from "~/helpers/utils/useWindowSize" import useWindowSize from "~/helpers/utils/useWindowSize"
<<<<<<< HEAD
import { useSentry } from "~/helpers/sentry" import { useSentry } from "~/helpers/sentry"
=======
import { useColorMode } from "~/helpers/utils/composables" import { useColorMode } from "~/helpers/utils/composables"
>>>>>>> refactor: update color-mode usage to new composable
function appLayout() { function appLayout() {
const rightSidebar = useSetting("SIDEBAR") const rightSidebar = useSetting("SIDEBAR")

View File

@@ -249,6 +249,7 @@ import {
hasFirefoxExtensionInstalled, hasFirefoxExtensionInstalled,
} from "~/helpers/strategies/ExtensionStrategy" } from "~/helpers/strategies/ExtensionStrategy"
import { getLocalConfig } from "~/newstore/localpersistence" import { getLocalConfig } from "~/newstore/localpersistence"
import { browserIsChrome, browserIsFirefox } from "~/helpers/utils/userAgent"
const t = useI18n() const t = useI18n()
const toast = useToast() const toast = useToast()
@@ -262,19 +263,30 @@ const EXPAND_NAVIGATION = useSetting("EXPAND_NAVIGATION")
const SIDEBAR_ON_LEFT = useSetting("SIDEBAR_ON_LEFT") const SIDEBAR_ON_LEFT = useSetting("SIDEBAR_ON_LEFT")
const ZEN_MODE = useSetting("ZEN_MODE") const ZEN_MODE = useSetting("ZEN_MODE")
const extensionVersion = usePolled(5000, () => const extensionVersion = usePolled(5000, (stopPolling) => {
hasExtensionInstalled() const result = hasExtensionInstalled()
? window.__POSTWOMAN_EXTENSION_HOOK__.getVersion() ? window.__POSTWOMAN_EXTENSION_HOOK__.getVersion()
: null : null
)
const hasChromeExtInstalled = usePolled(5000, () => // We don't need to poll anymore after we get value
hasChromeExtensionInstalled() if (result) stopPolling()
)
const hasFirefoxExtInstalled = usePolled(5000, () => return result
hasFirefoxExtensionInstalled() })
)
const hasChromeExtInstalled = usePolled(5000, (stopPolling) => {
// If not Chrome, we don't need to worry about this value changing
if (!browserIsChrome()) stopPolling()
return hasChromeExtensionInstalled()
})
const hasFirefoxExtInstalled = usePolled(5000, (stopPolling) => {
// If not Chrome, we don't need to worry about this value changing
if (!browserIsFirefox()) stopPolling()
return hasFirefoxExtensionInstalled()
})
const clearIcon = ref("rotate-ccw") const clearIcon = ref("rotate-ccw")