mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-30 11:11:53 +00:00
cancel inflight create session
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { createSingletonPromise } from '@vueuse/core'
|
||||||
|
|
||||||
import { api } from '@/scripts/api'
|
import { api } from '@/scripts/api'
|
||||||
import { isCloud } from '@/platform/distribution/types'
|
import { isCloud } from '@/platform/distribution/types'
|
||||||
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
|
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
|
||||||
@@ -14,55 +16,16 @@ export const useSessionCookie = () => {
|
|||||||
const createSession = async (): Promise<void> => {
|
const createSession = async (): Promise<void> => {
|
||||||
if (!isCloud || logoutInProgress) return
|
if (!isCloud || logoutInProgress) return
|
||||||
|
|
||||||
if (inFlightCreateSession) {
|
const promise = createSessionSingleton()
|
||||||
await inFlightCreateSession
|
inFlightCreateSession = promise
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const authStore = useFirebaseAuthStore()
|
try {
|
||||||
|
await promise
|
||||||
let controller: AbortController | null = null
|
} finally {
|
||||||
|
if (inFlightCreateSession === promise) {
|
||||||
const run = (async () => {
|
|
||||||
const authHeader = await authStore.getAuthHeader()
|
|
||||||
|
|
||||||
if (!authHeader) {
|
|
||||||
throw new Error('No auth header available for session creation')
|
|
||||||
}
|
|
||||||
|
|
||||||
controller = new AbortController()
|
|
||||||
currentCreateController = controller
|
|
||||||
|
|
||||||
const response = await fetch(api.apiURL('/auth/session'), {
|
|
||||||
method: 'POST',
|
|
||||||
credentials: 'include',
|
|
||||||
signal: controller.signal,
|
|
||||||
headers: {
|
|
||||||
...authHeader,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const errorData = await response.json().catch(() => ({}))
|
|
||||||
throw new Error(
|
|
||||||
`Failed to create session: ${errorData.message || response.statusText}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
.catch((error: unknown) => {
|
|
||||||
if (isAbortError(error)) return
|
|
||||||
throw error
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
if (currentCreateController === controller) {
|
|
||||||
currentCreateController = null
|
|
||||||
}
|
|
||||||
inFlightCreateSession = null
|
inFlightCreateSession = null
|
||||||
})
|
}
|
||||||
|
}
|
||||||
inFlightCreateSession = run
|
|
||||||
await run
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,6 +64,7 @@ export const useSessionCookie = () => {
|
|||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
logoutInProgress = false
|
logoutInProgress = false
|
||||||
|
await createSessionSingleton.reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,6 +78,45 @@ let inFlightCreateSession: Promise<void> | null = null
|
|||||||
let currentCreateController: AbortController | null = null
|
let currentCreateController: AbortController | null = null
|
||||||
let logoutInProgress = false
|
let logoutInProgress = false
|
||||||
|
|
||||||
|
const createSessionSingleton = createSingletonPromise(async () => {
|
||||||
|
const authStore = useFirebaseAuthStore()
|
||||||
|
const authHeader = await authStore.getAuthHeader()
|
||||||
|
|
||||||
|
if (!authHeader) {
|
||||||
|
throw new Error('No auth header available for session creation')
|
||||||
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController()
|
||||||
|
currentCreateController = controller
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(api.apiURL('/auth/session'), {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: {
|
||||||
|
...authHeader,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => ({}))
|
||||||
|
throw new Error(
|
||||||
|
`Failed to create session: ${errorData.message || response.statusText}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (!isAbortError(error)) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (currentCreateController === controller) {
|
||||||
|
currentCreateController = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const isAbortError = (error: unknown): boolean => {
|
const isAbortError = (error: unknown): boolean => {
|
||||||
if (!error || typeof error !== 'object') return false
|
if (!error || typeof error !== 'object') return false
|
||||||
const name = 'name' in error ? (error as { name?: string }).name : undefined
|
const name = 'name' in error ? (error as { name?: string }).name : undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user