Skip to content

Commit

Permalink
feat: wait before displaying disconnected state
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 7, 2024
1 parent 55abcc0 commit 635fad8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/app-frontend/src/features/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default defineComponent({
},
setup() {
const { isConnected, isInitializing } = useAppConnection()
const { isConnected, isInitializing, showDisplayDisconnected } = useAppConnection()
function updateTheme(theme: string) {
if (theme === 'dark' || theme === 'high-contrast' || (theme === 'auto' && chromeTheme === 'dark')) {
Expand Down Expand Up @@ -80,6 +80,7 @@ export default defineComponent({
return {
isConnected,
isInitializing,
showDisplayDisconnected,
showAppsSelector,
orientation,
isChrome,
Expand All @@ -103,7 +104,7 @@ export default defineComponent({
/>

<AppDisconnected
v-else-if="!isConnected"
v-else-if="showDisplayDisconnected"
class="absolute inset-0"
/>

Expand Down
28 changes: 26 additions & 2 deletions packages/app-frontend/src/features/connection/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import { ref } from 'vue'
import { computed, ref } from 'vue'
import { useNow } from '@vueuse/core'

const isConnected = ref(false)
const isInitializing = ref(true)
const lastDisconnect = ref(0)

export function useAppConnection() {
const now = useNow({
interval: 1000,
})
const showDisplayDisconnected = computed(() => {
if (isInitializing.value) {
return false
}
if (lastDisconnect.value === 0) {
return false
}
// Wait for 5 seconds before showing the disconnected message
return !isConnected && now.value.getTime() - lastDisconnect.value > 5000
})

return {
isConnected,
isInitializing,
lastDisconnect,
showDisplayDisconnected,
}
}

export function setAppConnected(value: boolean) {
export function setAppConnected(value: boolean, force = false) {
if (force) {
lastDisconnect.value = 0
}
else if (!value && isConnected.value) {
lastDisconnect.value = Date.now()
}
isConnected.value = value
}

Expand Down
2 changes: 1 addition & 1 deletion packages/app-frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function initDevTools(shell: Shell) {
app.mount('#app')
connectApp(app, shell)
shell.onReload(() => {
setAppConnected(false)
setAppConnected(false, true)
getBridge()?.removeAllListeners()
connectApp(app, shell)
})
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-chrome/src/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ initDevTools({
setAppConnected(false)

// Retry
retryConnectTimer = setTimeout(connect, 1000)
retryConnectTimer = setTimeout(connect, 500)
})

if (connectCount > 1) {
Expand All @@ -49,7 +49,7 @@ initDevTools({
setAppConnected(false)

// Retry
retryConnectTimer = setTimeout(connect, 5000)
retryConnectTimer = setTimeout(connect, 1000)
}
}
connect()
Expand Down

0 comments on commit 635fad8

Please sign in to comment.