added assets into stream
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
findStoredAuthToken,
|
||||
persistAuthToken,
|
||||
} from '@/utils/authToken'
|
||||
import { stopPriceRealtimeAuth } from '@/utils/priceSocket'
|
||||
|
||||
import { ref } from 'vue'
|
||||
import {useConfigStore} from "@/store/configStore";
|
||||
@@ -85,6 +86,7 @@ export async function authenticateWithFingerprint() {
|
||||
|
||||
|
||||
function removeToken() {
|
||||
stopPriceRealtimeAuth()
|
||||
return clearStoredAuthTokens()
|
||||
}
|
||||
|
||||
|
||||
+74
-12
@@ -2,6 +2,7 @@ import type { Price } from '@/models/home'
|
||||
import type { Product } from '@/models/products'
|
||||
import { Modal } from 'ant-design-vue'
|
||||
import { getConfigApp } from '@/utils/config'
|
||||
import { findStoredAuthToken } from '@/utils/authToken'
|
||||
import { priceSocketConnected, priceStreamLifecycle, priceWssEnabled } from '@/utils/priceSocketState'
|
||||
|
||||
export { priceSocketConnected, priceStreamLifecycle, priceWssEnabled, shouldUsePricePolling } from '@/utils/priceSocketState'
|
||||
@@ -56,8 +57,16 @@ function resolveStreamUrl(): string | null {
|
||||
return null
|
||||
}
|
||||
|
||||
const token = findStoredAuthToken()
|
||||
if (!token) {
|
||||
return null
|
||||
}
|
||||
|
||||
const base = String(getConfigApp('node_server') || window.location.origin).replace(/\/$/, '')
|
||||
return `${base}${resolveStreamPath()}`
|
||||
const url = new URL(`${base}${resolveStreamPath()}`)
|
||||
url.searchParams.set('token', token)
|
||||
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
function clearReconnectTimer() {
|
||||
@@ -205,6 +214,11 @@ async function applyListFromStream(products: Product[]) {
|
||||
applyProductsListFromSocket(products)
|
||||
}
|
||||
|
||||
async function applyAssetsFromStream(payload: unknown) {
|
||||
const { applyWalletAssetsFromStream } = await import('@/composables/useWalletAssets')
|
||||
applyWalletAssetsFromStream(payload)
|
||||
}
|
||||
|
||||
function handleListPayload(payload: unknown) {
|
||||
if (!Array.isArray(payload) || !payload.length) {
|
||||
return
|
||||
@@ -214,6 +228,15 @@ function handleListPayload(payload: unknown) {
|
||||
void applyListFromStream(payload as Product[])
|
||||
}
|
||||
|
||||
function handleAssetsPayload(payload: unknown) {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return
|
||||
}
|
||||
|
||||
touchStreamActivity()
|
||||
void applyAssetsFromStream(payload)
|
||||
}
|
||||
|
||||
function bindStreamEvents(source: EventSource) {
|
||||
source.addEventListener('last', (event) => {
|
||||
try {
|
||||
@@ -231,6 +254,14 @@ function bindStreamEvents(source: EventSource) {
|
||||
}
|
||||
})
|
||||
|
||||
source.addEventListener('assets', (event) => {
|
||||
try {
|
||||
handleAssetsPayload(JSON.parse(event.data))
|
||||
} catch (error) {
|
||||
console.warn('Price SSE assets parse failed', error)
|
||||
}
|
||||
})
|
||||
|
||||
source.addEventListener('ping', () => {
|
||||
touchStreamActivity()
|
||||
})
|
||||
@@ -414,27 +445,58 @@ function onVisibilityChange() {
|
||||
|
||||
export function isPriceWebSocketEnabled(settingGetter: (key: string) => unknown): boolean {
|
||||
getSetting = settingGetter
|
||||
if (!isTruthySetting(settingGetter('wss_enabled'), true)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!findStoredAuthToken()) {
|
||||
return false
|
||||
}
|
||||
|
||||
return resolveStreamUrl() !== null
|
||||
}
|
||||
|
||||
let channelStarted = false
|
||||
|
||||
export function initPriceRealtimeChannel(settingGetter: (key: string) => unknown) {
|
||||
function shutdownPriceRealtimeChannel() {
|
||||
if (!channelStarted && subscribers <= 0) {
|
||||
return
|
||||
}
|
||||
|
||||
channelStarted = false
|
||||
stopped = true
|
||||
subscribers = 0
|
||||
disconnect()
|
||||
unbindBrowserNetworkListeners()
|
||||
|
||||
if (typeof document !== 'undefined') {
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||||
}
|
||||
}
|
||||
|
||||
export function syncPriceRealtimeChannel(settingGetter: (key: string) => unknown) {
|
||||
getSetting = settingGetter
|
||||
priceWssEnabled.value = isTruthySetting(settingGetter('wss_enabled'), true)
|
||||
|
||||
if (channelStarted) {
|
||||
if (!priceWssEnabled.value || !isPriceWebSocketEnabled(settingGetter)) {
|
||||
shutdownPriceRealtimeChannel()
|
||||
return
|
||||
}
|
||||
|
||||
if (channelStarted && subscribers > 0) {
|
||||
forceReconnect()
|
||||
return
|
||||
}
|
||||
|
||||
channelStarted = true
|
||||
|
||||
if (!priceWssEnabled.value) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!isPriceWebSocketEnabled(settingGetter)) {
|
||||
return
|
||||
}
|
||||
|
||||
stopped = false
|
||||
startPriceSocket(settingGetter)
|
||||
}
|
||||
|
||||
export function initPriceRealtimeChannel(settingGetter: (key: string) => unknown) {
|
||||
syncPriceRealtimeChannel(settingGetter)
|
||||
}
|
||||
|
||||
export function stopPriceRealtimeAuth() {
|
||||
shutdownPriceRealtimeChannel()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user