Skip to content

Commit

Permalink
refactor: remove previous reconnection api
Browse files Browse the repository at this point in the history
  • Loading branch information
Axolotle committed Jan 19, 2025
1 parent 7cf26ab commit 2ffddf2
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 58 deletions.
44 changes: 2 additions & 42 deletions app/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import { v4 as uuid } from 'uuid'

import { useCache, type StorePath } from '@/composables/data'
import { useInfos } from '@/composables/useInfos'
import { useRequests, type ReconnectingArgs } from '@/composables/useRequests'
import { useRequests } from '@/composables/useRequests'
import { useSettings } from '@/composables/useSettings'
import type { Obj } from '@/types/commons'
import {
APIBadRequestError,
APIErrorLog,
APIUnauthorizedError,
type APIError,
} from './errors'
import { APIBadRequestError, APIErrorLog, APIUnauthorizedError } from './errors'
import { getError, getResponseData } from './handlers'

export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
Expand Down Expand Up @@ -240,39 +235,4 @@ export default {
const { updateRouterKey } = useInfos()
updateRouterKey()
},

/**
* Api reconnection helper. Resolve when server is reachable or fail after n attemps
*
* @param attemps - Number of attemps before rejecting
* @param delay - Delay between calls to the API in ms
* @param initialDelay - Delay before calling the API for the first time in ms
*
* @returns Promise that resolve yunohost version infos
* @throws Throw an `APIError` or subclass depending on server response
*/
tryToReconnect({
attemps = 5,
delay = 2000,
initialDelay = 0,
}: ReconnectingArgs = {}) {
const { getYunoHostVersion } = useInfos()
return new Promise((resolve, reject) => {
function reconnect(n: number) {
getYunoHostVersion()
.then(resolve)
.catch((err: APIError) => {
if (err instanceof APIUnauthorizedError) {
reject(err)
} else if (n < 1) {
reject(err)
} else {
setTimeout(() => reconnect(n - 1), delay)
}
})
}
if (initialDelay > 0) setTimeout(() => reconnect(attemps), initialDelay)
else reconnect(attemps)
})
},
}
6 changes: 0 additions & 6 deletions app/src/composables/useInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import api from '@/api'
import { timeout } from '@/helpers/commons'
import i18n from '@/i18n'
import { useDomains } from './data'
import { useRequests, type ReconnectingArgs } from './useRequests'
import { useSSE } from './useSSE'

type BreadcrumbRoutes = {
Expand Down Expand Up @@ -181,10 +180,6 @@ export const useInfos = createGlobalState(() => {
return api.get('logout')
}

function tryToReconnect(args?: ReconnectingArgs) {
useRequests().reconnecting.value = args
}

function updateRouterKey(to?: RouteLocationNormalized) {
if (!to) {
// Trick to force a view reload
Expand Down Expand Up @@ -223,7 +218,6 @@ export const useInfos = createGlobalState(() => {
onLogout,
login,
logout,
tryToReconnect,
updateHtmlTitle,
updateRouterKey,
}
Expand Down
11 changes: 1 addition & 10 deletions app/src/composables/useRequests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createGlobalState } from '@vueuse/core'
import { computed, reactive, ref, shallowRef } from 'vue'
import { computed, reactive, shallowRef } from 'vue'
import { useRouter } from 'vue-router'

import type { RequestMethod } from '@/api/api'
Expand Down Expand Up @@ -45,18 +45,10 @@ export type RequestMessage = {
variant: StateVariant
}

export type ReconnectingArgs = {
attemps?: number
origin?: string
initialDelay?: number
delay?: number
}

export const useRequests = createGlobalState(() => {
const router = useRouter()

const requests = shallowRef<APIRequest[]>([])
const reconnecting = ref<ReconnectingArgs | undefined>()
const currentRequest = computed(() => {
return requests.value.find((r) => r.showModal)
})
Expand Down Expand Up @@ -230,7 +222,6 @@ export const useRequests = createGlobalState(() => {
requests,
historyList,
currentRequest,
reconnecting,
locked,
startRequest,
endRequest,
Expand Down

0 comments on commit 2ffddf2

Please sign in to comment.