43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { ref } from 'vue'
|
|
|
|
export const priceSocketConnected = ref(false)
|
|
export const priceWssEnabled = ref(false)
|
|
|
|
export type PriceStreamLifecycle = 'idle' | 'connecting' | 'open' | 'closed'
|
|
|
|
export const priceStreamLifecycle = ref<PriceStreamLifecycle>('idle')
|
|
|
|
export const priceStreamConnectingSince = ref(0)
|
|
|
|
/** آخرین رویداد list استریم */
|
|
export const priceStreamList = ref<Array<Record<string, unknown>>>([])
|
|
|
|
/** آخرین بدنهٔ tv/products که از SSE آمده */
|
|
export const priceStreamProducts = ref<Array<Record<string, unknown>>>([])
|
|
|
|
/** unix ساعت سرور از رویداد time استریم */
|
|
export const priceStreamServerUnix = ref(0)
|
|
|
|
const CONNECTING_POLL_AFTER_MS = 8000
|
|
|
|
export function shouldUsePricePolling(): boolean {
|
|
if (!priceWssEnabled.value) {
|
|
return true
|
|
}
|
|
|
|
if (priceStreamLifecycle.value === 'open') {
|
|
return false
|
|
}
|
|
|
|
if (priceStreamLifecycle.value === 'connecting') {
|
|
const since = priceStreamConnectingSince.value
|
|
if (since > 0 && Date.now() - since >= CONNECTING_POLL_AFTER_MS) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|