chart subscription fixes

This commit is contained in:
Tim
2024-03-20 23:41:32 -04:00
parent 8e1c21cf35
commit 30b83ddaaa
2 changed files with 33 additions and 19 deletions

View File

@@ -7,8 +7,10 @@ export const WIDE_PRICE_FORMAT = {decimals:38, width:512, signed:false}; // 38 d
export function subOHLC( chainId, pool, period ) {
const key = `${pool}|${period}`
const ckey = `${chainId}|${key}`
if (!(key in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
// console.log('subOHLC', chainId, pool, period, ckey, ohlcSubCounts[ckey])
if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
ohlcSubCounts[ckey] = 1
console.log('subscribing OHLCs', chainId, key)
socket.emit('subOHLCs', chainId, [key])
} else
ohlcSubCounts[ckey]++
@@ -18,15 +20,18 @@ export function subOHLC( chainId, pool, period ) {
export function unsubOHLC( chainId, pool, period ) {
const key = `${pool}|${period}`
const ckey = `${chainId}|${key}`
// console.log('unsubOHLC', chainId, pool, period, ckey, ohlcSubCounts[ckey])
if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
console.error('overdecremented ohlc', pool, period)
ohlcSubCounts[ckey] = 1
} else {
ohlcSubCounts[ckey]--
if (ohlcSubCounts[key] === 0)
if (ohlcSubCounts[ckey] === 0) {
console.log('unsubscribing OHLCs', chainId, key)
// noinspection JSCheckFunctionSignatures
socket.emit('unsubOHLCs', chainId, [key])
}
}
}

View File

@@ -338,30 +338,38 @@ export const DataFeed = {
subscriberUID,
onResetCacheNeededCallback,
) => {
console.log('[subscribeBars]: Method call with subscriberUID:', subscriberUID);
console.log('[subscribeBars]', symbolInfo, resolution, subscriberUID);
const chainId = useStore().chainId.value;
const poolAddr = useChartOrderStore().selectedPool[0];
const period = tvResolutionToPeriodString(resolution);
subscriptions[subscriberUID] = [chainId, poolAddr, period]
DataFeed.subscribeBarsOnRealtimeCallback = onRealtimeCallback;
const key = `${chainId}|${poolAddr}|${period}`;
subscription_callbacks[key] = [onRealtimeCallback, onResetCacheNeededCallback]
console.log('sub', key)
subOHLC(chainId, poolAddr, period)
},
unsubscribeBars: (subscriberUID) => {
console.log('[unsubscribeBars]: Method call with subscriberUID:', subscriberUID);
console.log('[unsubscribeBars]', subscriberUID);
const [chainId, poolAddr, period] = subscriptions[subscriberUID]
delete subscriptions[subscriberUID]
unsubOHLC(chainId, poolAddr, period)
delete subscriptions[subscriberUID]
const key = `${chainId}|${poolAddr}|${period}`;
console.log('unsub',key)
delete subscription_callbacks[key]
},
poolCallbackState : {lastBar: null},
poolCallbackState : {lastBar:{'chain|pool|period':null}},
poolCallback(chainId, pool, ohlcs) {
console.log("poolCallback: chainId, pool, ohlcs:", chainId, pool, ohlcs)
poolCallback(chainId, poolPeriod, ohlcs) {
console.log("poolCallback: chainId, pool, ohlcs:", chainId, poolPeriod, ohlcs)
if (ohlcs == null) {
console.log("poolCallback: ohlcs == null, nothing to do.")
return;
}
const key = `${chainId}|${poolPeriod}`;
console.log('key lookup',key,subscription_callbacks[key])
const [onRealtimeCallback, onResetCacheNeededCallback] = subscription_callbacks[key]
let ohlc = ohlcs.at(-1);
console.log("poolCallBack ohlc:", new Date(Number(ohlc[0])*1000).toGMTString(), ohlc)
for (let i = 0; i<ohlc.length; i++) if (ohlc[i]!=null) ohlc[i] = Number(ohlc[i])
@@ -379,33 +387,34 @@ export const DataFeed = {
bar = maybeInvertBar(bar)
checkBar(bar, "poolCallback, after inversion:")
console.log('DataFeed.poolCallback', date.toGMTString(), ohlcs, bar)
let lastBar = DataFeed.poolCallbackState.lastBar
let lastBar = DataFeed.poolCallbackState.lastBar[key]
// No last bar then initialize bar
if (lastBar===null) {
if (lastBar===undefined) {
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
DataFeed.subscribeBarsOnRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar = bar
onRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar[key] = bar
}
// bar time is less than last bar then ignore
else if (bar.time < lastBar.time ) {
}
// bar time equal to last bar then replace last bar
else if (bar.time == lastBar.time ) {
else if (bar.time === lastBar.time ) {
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
if (bar.high < lastBar.high) console.log("bar.high < lastBar.high (lastbar=)")
if (bar.low > lastBar.low) console.log("bar.low > lastBar.low (lastbar=)")
DataFeed.subscribeBarsOnRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar = bar
onRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar[key] = bar
}
// new bar, then render last and replace last bar
else {
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
DataFeed.subscribeBarsOnRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar = bar
onRealtimeCallback(bar)
DataFeed.poolCallbackState.lastBar[key] = bar
}
// }
}
};
}
let defaultSymbol = null
const subscriptions = {}
const subscription_callbacks = {}