some trigger handled
This commit is contained in:
+32
-16
@@ -2,7 +2,10 @@ import { getLastPrice, getPrice } from '@/api/home'
|
||||
import type { Price, PriceUp } from '@/models/home'
|
||||
import { ref } from 'vue'
|
||||
|
||||
let interval: ReturnType<typeof setInterval> | undefined
|
||||
const LAST_INTERVAL_MS = 10000
|
||||
|
||||
let lastInterval: ReturnType<typeof setInterval> | undefined
|
||||
let lastSubscribers = 0
|
||||
|
||||
/** @deprecated از lastPrice استفاده کنید — برای سازگاری با کد قدیمی sync میشود */
|
||||
export const price = ref<Price>()
|
||||
@@ -23,18 +26,38 @@ export function normalizePriceResponse(data: Price): Price {
|
||||
return data
|
||||
}
|
||||
|
||||
/** polling /api/price/last — فقط صفحه اصلی */
|
||||
export function startLastPricePolling() {
|
||||
lastSubscribers++
|
||||
if (lastInterval) {
|
||||
return
|
||||
}
|
||||
lastInterval = setInterval(() => {
|
||||
void getLastPrices()
|
||||
}, LAST_INTERVAL_MS)
|
||||
}
|
||||
|
||||
/** @deprecated از startLastPricePolling استفاده کنید */
|
||||
export async function setIntervalGetAllPrice() {
|
||||
try {
|
||||
interval = setInterval(async () => {
|
||||
await getLastPrices()
|
||||
}, 10000)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
startLastPricePolling()
|
||||
}
|
||||
|
||||
/** @deprecated از startLastPricePolling استفاده کنید */
|
||||
export async function setIntervalGetLastPrice() {
|
||||
startLastPricePolling()
|
||||
}
|
||||
|
||||
export function stopLastPricePolling() {
|
||||
lastSubscribers = Math.max(0, lastSubscribers - 1)
|
||||
if (lastSubscribers === 0 && lastInterval) {
|
||||
clearInterval(lastInterval)
|
||||
lastInterval = undefined
|
||||
}
|
||||
}
|
||||
|
||||
export async function setIntervalGetLastPrice() {
|
||||
return setIntervalGetAllPrice()
|
||||
/** @deprecated از stopLastPricePolling استفاده کنید */
|
||||
export function stopPriceInterval() {
|
||||
stopLastPricePolling()
|
||||
}
|
||||
|
||||
/** @deprecated فقط getLastPrices را صدا بزنید */
|
||||
@@ -79,13 +102,6 @@ export async function getLastPrices() {
|
||||
}
|
||||
}
|
||||
|
||||
export function stopPriceInterval() {
|
||||
if (interval) {
|
||||
clearInterval(interval)
|
||||
interval = undefined
|
||||
}
|
||||
}
|
||||
|
||||
export function percentPriceChanged() {
|
||||
if (!olderPrice.value) {
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user