new update

This commit is contained in:
2026-06-29 15:27:35 +03:30
parent 2e159dde20
commit be17631109
33 changed files with 3713 additions and 351 deletions
+150 -70
View File
@@ -1,86 +1,166 @@
import {loading} from '@/utils/index'
import {getLastPrice, getPrice} from '@/api/home'
import {ref} from 'vue'
import type {Price} from '@/models/home'
import { getLastPrice, getPrice } from '@/api/home'
import type { Price, PriceUp } from '@/models/home'
import { ref, watch } from 'vue'
import {
priceSocketConnected,
priceStreamLifecycle,
priceWssEnabled,
shouldUsePricePolling,
} from '@/utils/priceSocketState'
const LAST_INTERVAL_MS = 10000
let lastInterval: ReturnType<typeof setInterval> | undefined
let lastSubscribers = 0
let interval: any
export const price = ref<Price>()
export const lastPrice = ref<Price>()
export const olderPrice = ref<number| undefined>(0)
export const olderPrice = ref<number | undefined>(0)
let lastPriceSourceAt = 0
let consecutivePriceFetchFailures = 0
function hasValidPricePayload(data?: Price | null): boolean {
if (!data) {
return false
}
return (
Number(data.buy ?? data.up?.buy ?? 0) > 0
|| Number(data.sell ?? data.up?.sell ?? 0) > 0
|| Number(data.price_last ?? data.price ?? 0) > 0
)
}
export function markPriceSourceReceived() {
lastPriceSourceAt = Date.now()
consecutivePriceFetchFailures = 0
}
export function markPriceSourceFetchFailed() {
consecutivePriceFetchFailures += 1
}
export function hasRecentPriceFromSource(maxAgeMs = 45000): boolean {
return lastPriceSourceAt > 0 && Date.now() - lastPriceSourceAt < maxAgeMs
}
export function consecutivePriceSourceFailures(): number {
return consecutivePriceFetchFailures
}
function startPollingInterval() {
if (lastInterval || !shouldUsePricePolling()) {
return
}
lastInterval = setInterval(() => {
if (shouldUsePricePolling()) {
void getLastPrices()
}
}, LAST_INTERVAL_MS)
}
function stopPollingInterval() {
if (lastInterval) {
clearInterval(lastInterval)
lastInterval = undefined
}
}
function syncLastPricePolling() {
const usePolling = shouldUsePricePolling()
if (usePolling) {
if (lastSubscribers > 0) {
startPollingInterval()
}
return
}
stopPollingInterval()
}
watch(
() => [priceWssEnabled.value, priceSocketConnected.value, priceStreamLifecycle.value] as const,
() => {
syncLastPricePolling()
},
)
export function startLastPricePolling() {
lastSubscribers++
syncLastPricePolling()
}
export async function setIntervalGetAllPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
startLastPricePolling()
}
export async function setIntervalGetLastPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
// await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
startLastPricePolling()
}
export async function getUpPrice() {
try {
// loading.value = true
const data = await getPrice()
// const dataLast = await getLastPrice()
// if (lastPrice.value?.price_last != dataLast.price_last) {
// olderPrice.value = lastPrice.value?.price_last
// }
price.value = data
// lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
}
}
export async function getLastPrices() {
try {
// loading.value = true
// const data = await getPrice()
const dataLast = await getLastPrice()
if (lastPrice.value?.price_last != dataLast.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
// price.value = data
lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
}
export function stopLastPricePolling() {
lastSubscribers = Math.max(0, lastSubscribers - 1)
if (lastSubscribers === 0) {
stopPollingInterval()
}
}
export function stopPriceInterval() {
// console.log('stop')
clearInterval(interval)
stopLastPricePolling()
}
async function fetchUpPrices(): Promise<PriceUp | undefined> {
try {
const up = await getPrice()
if (up?.buy != null && up?.sell != null) {
return { buy: up.buy, sell: up.sell }
}
} catch (error) {
console.log(error)
}
}
export async function getUpPrice() {
await getLastPrices()
}
export async function getLastPrices(): Promise<boolean> {
try {
let dataLast = await getLastPrice()
if (!dataLast?.up) {
const up = await fetchUpPrices()
if (up) {
dataLast = { ...dataLast, up }
}
}
applyLastPricePayload(dataLast)
return hasValidPricePayload(dataLast)
} catch (error) {
console.log(error)
markPriceSourceFetchFailed()
return false
}
}
export function applyLastPricePayload(dataLast: Price) {
if (lastPrice.value?.price_last != dataLast.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
lastPrice.value = dataLast
price.value = dataLast
if (hasValidPricePayload(dataLast)) {
markPriceSourceReceived()
}
}
export function percentPriceChanged() {
// console.log(olderPrice.value)
// console.log(lastPrice.value?.price_last)
if (!olderPrice.value)
return 0
return ((lastPrice.value?.price_last-olderPrice.value) *100)/olderPrice.value
}
if (!olderPrice.value) {
return 0
}
return ((lastPrice.value?.price_last! - olderPrice.value) * 100) / olderPrice.value
}