chart subscription fixes
This commit is contained in:
@@ -7,8 +7,10 @@ export const WIDE_PRICE_FORMAT = {decimals:38, width:512, signed:false}; // 38 d
|
|||||||
export function subOHLC( chainId, pool, period ) {
|
export function subOHLC( chainId, pool, period ) {
|
||||||
const key = `${pool}|${period}`
|
const key = `${pool}|${period}`
|
||||||
const ckey = `${chainId}|${key}`
|
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
|
ohlcSubCounts[ckey] = 1
|
||||||
|
console.log('subscribing OHLCs', chainId, key)
|
||||||
socket.emit('subOHLCs', chainId, [key])
|
socket.emit('subOHLCs', chainId, [key])
|
||||||
} else
|
} else
|
||||||
ohlcSubCounts[ckey]++
|
ohlcSubCounts[ckey]++
|
||||||
@@ -18,15 +20,18 @@ export function subOHLC( chainId, pool, period ) {
|
|||||||
export function unsubOHLC( chainId, pool, period ) {
|
export function unsubOHLC( chainId, pool, period ) {
|
||||||
const key = `${pool}|${period}`
|
const key = `${pool}|${period}`
|
||||||
const ckey = `${chainId}|${key}`
|
const ckey = `${chainId}|${key}`
|
||||||
|
// console.log('unsubOHLC', chainId, pool, period, ckey, ohlcSubCounts[ckey])
|
||||||
if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
|
if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
|
||||||
console.error('overdecremented ohlc', pool, period)
|
console.error('overdecremented ohlc', pool, period)
|
||||||
ohlcSubCounts[ckey] = 1
|
ohlcSubCounts[ckey] = 1
|
||||||
} else {
|
} else {
|
||||||
ohlcSubCounts[ckey]--
|
ohlcSubCounts[ckey]--
|
||||||
if (ohlcSubCounts[key] === 0)
|
if (ohlcSubCounts[ckey] === 0) {
|
||||||
|
console.log('unsubscribing OHLCs', chainId, key)
|
||||||
// noinspection JSCheckFunctionSignatures
|
// noinspection JSCheckFunctionSignatures
|
||||||
socket.emit('unsubOHLCs', chainId, [key])
|
socket.emit('unsubOHLCs', chainId, [key])
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -338,30 +338,38 @@ export const DataFeed = {
|
|||||||
subscriberUID,
|
subscriberUID,
|
||||||
onResetCacheNeededCallback,
|
onResetCacheNeededCallback,
|
||||||
) => {
|
) => {
|
||||||
console.log('[subscribeBars]: Method call with subscriberUID:', subscriberUID);
|
console.log('[subscribeBars]', symbolInfo, resolution, subscriberUID);
|
||||||
const chainId = useStore().chainId.value;
|
const chainId = useStore().chainId.value;
|
||||||
const poolAddr = useChartOrderStore().selectedPool[0];
|
const poolAddr = useChartOrderStore().selectedPool[0];
|
||||||
const period = tvResolutionToPeriodString(resolution);
|
const period = tvResolutionToPeriodString(resolution);
|
||||||
subscriptions[subscriberUID] = [chainId, poolAddr, period]
|
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)
|
subOHLC(chainId, poolAddr, period)
|
||||||
},
|
},
|
||||||
|
|
||||||
unsubscribeBars: (subscriberUID) => {
|
unsubscribeBars: (subscriberUID) => {
|
||||||
console.log('[unsubscribeBars]: Method call with subscriberUID:', subscriberUID);
|
console.log('[unsubscribeBars]', subscriberUID);
|
||||||
const [chainId, poolAddr, period] = subscriptions[subscriberUID]
|
const [chainId, poolAddr, period] = subscriptions[subscriberUID]
|
||||||
delete subscriptions[subscriberUID]
|
|
||||||
unsubOHLC(chainId, poolAddr, period)
|
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) {
|
poolCallback(chainId, poolPeriod, ohlcs) {
|
||||||
console.log("poolCallback: chainId, pool, ohlcs:", chainId, pool, ohlcs)
|
console.log("poolCallback: chainId, pool, ohlcs:", chainId, poolPeriod, ohlcs)
|
||||||
if (ohlcs == null) {
|
if (ohlcs == null) {
|
||||||
console.log("poolCallback: ohlcs == null, nothing to do.")
|
console.log("poolCallback: ohlcs == null, nothing to do.")
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const key = `${chainId}|${poolPeriod}`;
|
||||||
|
console.log('key lookup',key,subscription_callbacks[key])
|
||||||
|
const [onRealtimeCallback, onResetCacheNeededCallback] = subscription_callbacks[key]
|
||||||
let ohlc = ohlcs.at(-1);
|
let ohlc = ohlcs.at(-1);
|
||||||
console.log("poolCallBack ohlc:", new Date(Number(ohlc[0])*1000).toGMTString(), ohlc)
|
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])
|
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)
|
bar = maybeInvertBar(bar)
|
||||||
checkBar(bar, "poolCallback, after inversion:")
|
checkBar(bar, "poolCallback, after inversion:")
|
||||||
console.log('DataFeed.poolCallback', date.toGMTString(), ohlcs, bar)
|
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
|
// No last bar then initialize bar
|
||||||
if (lastBar===null) {
|
if (lastBar===undefined) {
|
||||||
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
|
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
|
||||||
DataFeed.subscribeBarsOnRealtimeCallback(bar)
|
onRealtimeCallback(bar)
|
||||||
DataFeed.poolCallbackState.lastBar = bar
|
DataFeed.poolCallbackState.lastBar[key] = bar
|
||||||
}
|
}
|
||||||
// bar time is less than last bar then ignore
|
// bar time is less than last bar then ignore
|
||||||
else if (bar.time < lastBar.time ) {
|
else if (bar.time < lastBar.time ) {
|
||||||
}
|
}
|
||||||
// bar time equal to last bar then replace last bar
|
// 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)
|
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.high < lastBar.high) console.log("bar.high < lastBar.high (lastbar=)")
|
||||||
if (bar.low > lastBar.low) console.log("bar.low > lastBar.low (lastbar=)")
|
if (bar.low > lastBar.low) console.log("bar.low > lastBar.low (lastbar=)")
|
||||||
DataFeed.subscribeBarsOnRealtimeCallback(bar)
|
onRealtimeCallback(bar)
|
||||||
DataFeed.poolCallbackState.lastBar = bar
|
DataFeed.poolCallbackState.lastBar[key] = bar
|
||||||
}
|
}
|
||||||
// new bar, then render last and replace last bar
|
// new bar, then render last and replace last bar
|
||||||
else {
|
else {
|
||||||
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
|
console.log('DataFeed.poolCallback', new Date(bar.time).toGMTString(), 'lastBar=', bar)
|
||||||
DataFeed.subscribeBarsOnRealtimeCallback(bar)
|
onRealtimeCallback(bar)
|
||||||
DataFeed.poolCallbackState.lastBar = bar
|
DataFeed.poolCallbackState.lastBar[key] = bar
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
let defaultSymbol = null
|
let defaultSymbol = null
|
||||||
const subscriptions = {}
|
const subscriptions = {}
|
||||||
|
const subscription_callbacks = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user