added sse and description for withdraw
This commit is contained in:
+65
-19
@@ -1,6 +1,11 @@
|
||||
import { getLastPrice, getPrice } from '@/api/home'
|
||||
import type { Price, PriceUp } from '@/models/home'
|
||||
import { ref } from 'vue'
|
||||
import { ref, watch } from 'vue'
|
||||
import {
|
||||
priceSocketConnected,
|
||||
priceWssEnabled,
|
||||
shouldUsePricePolling,
|
||||
} from '@/utils/priceSocketState'
|
||||
|
||||
const LAST_INTERVAL_MS = 10000
|
||||
|
||||
@@ -26,17 +31,55 @@ export function normalizePriceResponse(data: Price): Price {
|
||||
return data
|
||||
}
|
||||
|
||||
/** polling /api/price/last — فقط صفحه اصلی */
|
||||
export function startLastPricePolling() {
|
||||
lastSubscribers++
|
||||
if (lastInterval) {
|
||||
function startPollingInterval() {
|
||||
if (lastInterval || !shouldUsePricePolling()) {
|
||||
return
|
||||
}
|
||||
|
||||
lastInterval = setInterval(() => {
|
||||
void getLastPrices()
|
||||
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] as const,
|
||||
([wssEnabled, connected], prev) => {
|
||||
const prevConnected = prev?.[1]
|
||||
syncLastPricePolling()
|
||||
|
||||
if (wssEnabled && prevConnected && !connected && lastSubscribers > 0) {
|
||||
void getLastPrices()
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
/** polling /api/price/last — وقتی wss خاموش است یا ws قطع شده */
|
||||
export function startLastPricePolling() {
|
||||
lastSubscribers++
|
||||
syncLastPricePolling()
|
||||
}
|
||||
|
||||
/** @deprecated از startLastPricePolling استفاده کنید */
|
||||
export async function setIntervalGetAllPrice() {
|
||||
startLastPricePolling()
|
||||
@@ -49,9 +92,8 @@ export async function setIntervalGetLastPrice() {
|
||||
|
||||
export function stopLastPricePolling() {
|
||||
lastSubscribers = Math.max(0, lastSubscribers - 1)
|
||||
if (lastSubscribers === 0 && lastInterval) {
|
||||
clearInterval(lastInterval)
|
||||
lastInterval = undefined
|
||||
if (lastSubscribers === 0) {
|
||||
stopPollingInterval()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,21 +129,25 @@ export async function getLastPrices() {
|
||||
}
|
||||
}
|
||||
|
||||
const normalized = normalizePriceResponse(dataLast)
|
||||
if (lastPrice.value?.price_last != normalized.price_last) {
|
||||
olderPrice.value = lastPrice.value?.price_last
|
||||
}
|
||||
lastPrice.value = normalized
|
||||
price.value = {
|
||||
...normalized,
|
||||
buy: getTradeBuyPrice(normalized),
|
||||
sell: getTradeSellPrice(normalized),
|
||||
}
|
||||
applyLastPricePayload(dataLast)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
export function applyLastPricePayload(dataLast: Price) {
|
||||
const normalized = normalizePriceResponse(dataLast)
|
||||
if (lastPrice.value?.price_last != normalized.price_last) {
|
||||
olderPrice.value = lastPrice.value?.price_last
|
||||
}
|
||||
lastPrice.value = normalized
|
||||
price.value = {
|
||||
...normalized,
|
||||
buy: getTradeBuyPrice(normalized),
|
||||
sell: getTradeSellPrice(normalized),
|
||||
}
|
||||
}
|
||||
|
||||
export function percentPriceChanged() {
|
||||
if (!olderPrice.value) {
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user