fix bug of main-page for guest
This commit is contained in:
+20
-21
@@ -38,7 +38,7 @@ const STREAM_CONNECTING = 0
|
||||
const STREAM_OPEN = 1
|
||||
const STREAM_CLOSED = 2
|
||||
|
||||
/** EventSource نمیتواند Authorization بفرستد؛ استریم همچنان auth:customer میخواهد. */
|
||||
/** fetch + ReadableStream — با توکن اختلاف قیمت ویژه؛ بدون توکن همان مظنهٔ عمومی (مهمان). */
|
||||
class AuthPriceStream extends EventTarget {
|
||||
readyState = STREAM_CONNECTING
|
||||
onopen: (() => void) | null = null
|
||||
@@ -46,7 +46,7 @@ class AuthPriceStream extends EventTarget {
|
||||
onmessage: (() => void) | null = null
|
||||
private abort = new AbortController()
|
||||
|
||||
constructor(url: string, token: string) {
|
||||
constructor(url: string, token?: string) {
|
||||
super()
|
||||
void this.start(url, token)
|
||||
}
|
||||
@@ -56,13 +56,18 @@ class AuthPriceStream extends EventTarget {
|
||||
this.abort.abort()
|
||||
}
|
||||
|
||||
private async start(url: string, token: string) {
|
||||
private async start(url: string, token?: string) {
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
Accept: 'text/event-stream',
|
||||
}
|
||||
const bearer = String(token ?? '').trim()
|
||||
if (bearer) {
|
||||
headers.Authorization = `Bearer ${bearer}`
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Accept: 'text/event-stream',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
headers,
|
||||
cache: 'no-store',
|
||||
signal: this.abort.signal,
|
||||
})
|
||||
@@ -191,10 +196,6 @@ function resolveStreamUrl(): string | null {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!findStoredAuthToken()) {
|
||||
return null
|
||||
}
|
||||
|
||||
const base = String(getConfigApp('node_server') || window.location.origin).replace(/\/$/, '')
|
||||
return `${base}${resolveStreamPath()}`
|
||||
}
|
||||
@@ -301,10 +302,12 @@ async function runFallbackPoll() {
|
||||
'@/views/base/products/composables/useProductsPageData'
|
||||
)
|
||||
|
||||
const [priceResult] = await Promise.allSettled([
|
||||
getLastPrices(),
|
||||
refreshProductsPageData(),
|
||||
])
|
||||
const pollTasks: Promise<unknown>[] = [getLastPrices()]
|
||||
if (findStoredAuthToken()) {
|
||||
pollTasks.push(refreshProductsPageData())
|
||||
}
|
||||
|
||||
const [priceResult] = await Promise.allSettled(pollTasks)
|
||||
|
||||
const priceOk =
|
||||
priceResult.status === 'fulfilled'
|
||||
@@ -643,7 +646,7 @@ function connect() {
|
||||
|
||||
const url = resolveStreamUrl()
|
||||
const token = findStoredAuthToken()
|
||||
if (!url || !token) {
|
||||
if (!url) {
|
||||
priceSocketConnected.value = false
|
||||
priceStreamLifecycle.value = 'closed'
|
||||
clearConnectingLifecycle()
|
||||
@@ -661,7 +664,7 @@ function connect() {
|
||||
markConnectingLifecycle()
|
||||
|
||||
try {
|
||||
eventSource = new AuthPriceStream(url, token)
|
||||
eventSource = new AuthPriceStream(url, token ?? undefined)
|
||||
} catch (error) {
|
||||
console.warn('Price SSE connect failed', error)
|
||||
handleStreamLost()
|
||||
@@ -824,10 +827,6 @@ export function isPriceWebSocketEnabled(settingGetter: (key: string) => unknown)
|
||||
return false
|
||||
}
|
||||
|
||||
if (!findStoredAuthToken()) {
|
||||
return false
|
||||
}
|
||||
|
||||
return resolveStreamUrl() !== null
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user