chore: sync price monitor updates
This commit is contained in:
+37
-19
@@ -7,6 +7,9 @@ import {
|
||||
priceSocketConnected,
|
||||
priceStreamConnectingSince,
|
||||
priceStreamLifecycle,
|
||||
priceStreamList,
|
||||
priceStreamProducts,
|
||||
priceStreamServerUnix,
|
||||
priceWssEnabled,
|
||||
} from '@/utils/priceSocketState'
|
||||
import {
|
||||
@@ -282,28 +285,21 @@ export function notifyPriceLastFetchFailed() {
|
||||
void maybeShowNetworkAlert()
|
||||
}
|
||||
|
||||
function onTvBoard() {
|
||||
if (typeof window === 'undefined') return false
|
||||
const path = window.location.pathname.replace(/\/+$/, '') || '/'
|
||||
return path === '/tv' || path.endsWith('/tv') || path === '/monitor' || path.endsWith('/monitor')
|
||||
}
|
||||
|
||||
async function runFallbackPoll() {
|
||||
if (onTvBoard() || tvStreamKey || !priceWssEnabled.value || priceSocketConnected.value) {
|
||||
if (tvStreamKey || !priceWssEnabled.value || priceSocketConnected.value) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const { getLastPrices, hasRecentPriceFromSource } = await import('@/utils/price')
|
||||
const jobs: Promise<unknown>[] = [getLastPrices()]
|
||||
if (!tvStreamKey) {
|
||||
const { refreshProductsPageData } = await import(
|
||||
'@/views/base/products/composables/useProductsPageData'
|
||||
)
|
||||
jobs.push(refreshProductsPageData())
|
||||
}
|
||||
const { refreshProductsPageData } = await import(
|
||||
'@/views/base/products/composables/useProductsPageData'
|
||||
)
|
||||
|
||||
const [priceResult] = await Promise.allSettled(jobs)
|
||||
const [priceResult] = await Promise.allSettled([
|
||||
getLastPrices(),
|
||||
refreshProductsPageData(),
|
||||
])
|
||||
|
||||
const priceOk =
|
||||
priceResult.status === 'fulfilled'
|
||||
@@ -508,6 +504,7 @@ function handleListPayload(payload: unknown) {
|
||||
}
|
||||
|
||||
touchStreamActivity()
|
||||
priceStreamList.value = products as Array<Record<string, unknown>>
|
||||
applyProductsListFromStream(products)
|
||||
}
|
||||
|
||||
@@ -572,6 +569,31 @@ function bindStreamEvents(source: AuthPriceStream) {
|
||||
}
|
||||
})
|
||||
|
||||
source.addEventListener('products', (event) => {
|
||||
try {
|
||||
const rows = JSON.parse(event.data)
|
||||
if (Array.isArray(rows) && rows.length) {
|
||||
touchStreamActivity()
|
||||
priceStreamProducts.value = rows
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Price SSE products parse failed', error)
|
||||
}
|
||||
})
|
||||
|
||||
source.addEventListener('time', (event) => {
|
||||
try {
|
||||
const payload = JSON.parse(event.data) as { unix?: number }
|
||||
const unix = Number(payload?.unix)
|
||||
if (unix > 0) {
|
||||
touchStreamActivity()
|
||||
priceStreamServerUnix.value = unix
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Price SSE time parse failed', error)
|
||||
}
|
||||
})
|
||||
|
||||
source.addEventListener('app_notification', (event) => {
|
||||
try {
|
||||
handleAppNotificationPayload(JSON.parse(event.data))
|
||||
@@ -750,10 +772,6 @@ function unbindBrowserNetworkListeners() {
|
||||
window.removeEventListener('offline', onBrowserOffline)
|
||||
}
|
||||
|
||||
export function isTvStreamActive() {
|
||||
return Boolean(tvStreamKey)
|
||||
}
|
||||
|
||||
export function startTvPriceStream(
|
||||
settingGetter: (key: string) => unknown,
|
||||
key: string,
|
||||
|
||||
Reference in New Issue
Block a user