diff --git a/src/charts/datafeed.js b/src/charts/datafeed.js index f0daab8..b66a819 100644 --- a/src/charts/datafeed.js +++ b/src/charts/datafeed.js @@ -6,6 +6,7 @@ import FlexSearch from "flexsearch"; import {useChartOrderStore} from "@/orderbuild.js"; import {useStore} from "@/store/store.js"; import {subOHLC, unsubOHLC} from "@/blockchain/ohlcs.js"; +import {socket} from "@/socket.js"; let feeDropdown = null let widget = null @@ -276,7 +277,7 @@ export const DataFeed = { } const co = useChartOrderStore(); co.selectedSymbol = symbolItem - const pool = symbolItem.pools[Math.trunc(symbolItem.pools.length/2)]; + const pool = symbolItem.pools[0]; // choose the first-listed pool. server will adjust metadata accordingly. // noinspection JSValidateTypes co.selectedPool = pool updateFeeDropdown() @@ -342,9 +343,12 @@ export const DataFeed = { const chainId = useStore().chainId.value; const poolAddr = useChartOrderStore().selectedPool[0]; const period = tvResolutionToPeriodString(resolution); - subscriptions[subscriberUID] = [chainId, poolAddr, period] - const key = `${chainId}|${poolAddr}|${period}`; - subscription_callbacks[key] = [onRealtimeCallback, onResetCacheNeededCallback] + subscriptions[subscriberUID] = [chainId, poolAddr, period, onRealtimeCallback, onResetCacheNeededCallback] + const key = `${chainId}|${poolAddr}|${period}` + if (key in subscriptionCallbacks) + subscriptionCallbacks[key].push(subscriberUID) + else + subscriptionCallbacks[key] = [subscriberUID] console.log('sub', key) subOHLC(chainId, poolAddr, period) }, @@ -356,7 +360,11 @@ export const DataFeed = { delete subscriptions[subscriberUID] const key = `${chainId}|${poolAddr}|${period}`; console.log('unsub',key) - delete subscription_callbacks[key] + const remainingSubs = subscriptionCallbacks[key].filter((v)=>v!==subscriberUID) + if (remainingSubs.length===0) + delete subscriptionCallbacks[key] + else + subscriptionCallbacks[key] = remainingSubs }, poolCallbackState : {lastBar:{'chain|pool|period':null}}, @@ -368,8 +376,26 @@ export const DataFeed = { return; } const key = `${chainId}|${poolPeriod}`; - console.log('key lookup',key,subscription_callbacks[key]) - const [onRealtimeCallback, onResetCacheNeededCallback] = subscription_callbacks[key] + const subscriptionUIDs = subscriptionCallbacks[key]; + if (subscriptionUIDs===undefined) { + console.log('unsubbing abandoned subscription', poolPeriod) + socket.emit('unsubOHLCs', chainId, [poolPeriod]) + return + } + function onRealtimeCallback() { + for (const subId of subscriptionUIDs) { + const [_chainId, _poolAddr, _period, _onRealtimeCallback, _onResetCacheNeededCallback] = subscriptions[subId] + _onRealtimeCallback(...arguments) + } + } + + function onResetCacheNeededCallback() { + for (const subId of subscriptionUIDs) { + const [_chainId, _poolAddr, _period, _onRealtimeCallback, _onResetCacheNeededCallback] = subscriptions[subId] + _onResetCacheNeededCallback(...arguments) + } + } + let ohlc = ohlcs.at(-1); console.log("poolCallBack ohlc:", new Date(Number(ohlc[0])*1000).toGMTString(), ohlc) for (let i = 0; i