-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
189 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { orderBy } from 'lodash'; | ||
|
||
import { isWsCandlesResponse, isWsCandlesUpdateResponse } from '@/types/indexer/indexerChecks'; | ||
import { IndexerWsCandleResponse } from '@/types/indexer/indexerManual'; | ||
|
||
import { Loadable, loadableLoaded, loadablePending } from '../lib/loadable'; | ||
import { logAbacusTsError } from '../logs'; | ||
import { makeWsValueManager } from './lib/indexerValueManagerHelpers'; | ||
import { IndexerWebsocket } from './lib/indexerWebsocket'; | ||
import { WebsocketDerivedValue } from './lib/websocketDerivedValue'; | ||
|
||
function candlesWebsocketValueCreator( | ||
websocket: IndexerWebsocket, | ||
{ marketIdAndResolution }: { marketIdAndResolution: string } | ||
) { | ||
return new WebsocketDerivedValue<Loadable<IndexerWsCandleResponse>>( | ||
websocket, | ||
{ | ||
channel: 'v4_candles', | ||
id: marketIdAndResolution, | ||
handleBaseData: (baseMessage) => { | ||
const message = isWsCandlesResponse(baseMessage); | ||
return loadableLoaded({ | ||
candles: orderBy(message.candles, [(a) => a.startedAt], ['asc']), | ||
}); | ||
}, | ||
handleUpdates: (baseUpdates, value) => { | ||
const updates = isWsCandlesUpdateResponse(baseUpdates); | ||
const startingValue = value.data; | ||
if (startingValue == null) { | ||
logAbacusTsError('CandlesTracker', 'found unexpectedly null base data in update'); | ||
return value; | ||
} | ||
if (startingValue.candles.length === 0) { | ||
return loadableLoaded({ candles: updates }); | ||
} | ||
|
||
const allNewTimes = new Set(updates.map(({ startedAt }) => startedAt)); | ||
const newArr = [ | ||
...updates, | ||
...startingValue.candles.filter(({ startedAt }) => !allNewTimes.has(startedAt)), | ||
]; | ||
const sorted = orderBy(newArr, [(a) => a.startedAt], ['asc']); | ||
return loadableLoaded({ candles: sorted }); | ||
}, | ||
}, | ||
loadablePending() | ||
); | ||
} | ||
|
||
export const CandlesValuesManager = makeWsValueManager(candlesWebsocketValueCreator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createStoreEffect } from '@/abacus-ts/lib/createStoreEffect'; | ||
import { selectWebsocketUrl } from '@/abacus-ts/socketSelectors'; | ||
import { CandlesValuesManager } from '@/abacus-ts/websocket/candles'; | ||
import { subscribeToWsValue } from '@/abacus-ts/websocket/lib/indexerValueManagerHelpers'; | ||
import type { | ||
LibrarySymbolInfo, | ||
ResolutionString, | ||
SubscribeBarsCallback, | ||
} from 'public/tradingview/charting_library'; | ||
|
||
import { CandleResolution, RESOLUTION_MAP } from '@/constants/candles'; | ||
|
||
import { type RootStore } from '@/state/_store'; | ||
|
||
import { mapCandle } from '../../lib/tradingView/utils'; | ||
|
||
export const subscriptionsByGuid: { | ||
[guid: string]: | ||
| { | ||
guid: string; | ||
unsub: () => void; | ||
} | ||
| undefined; | ||
} = {}; | ||
|
||
export const subscribeOnStream = ({ | ||
store, | ||
orderbookCandlesToggleOn, | ||
symbolInfo, | ||
resolution, | ||
onRealtimeCallback, | ||
listenerGuid, | ||
onResetCacheNeededCallback, | ||
}: { | ||
store: RootStore; | ||
orderbookCandlesToggleOn: boolean; | ||
symbolInfo: LibrarySymbolInfo; | ||
resolution: ResolutionString; | ||
onRealtimeCallback: SubscribeBarsCallback; | ||
listenerGuid: string; | ||
onResetCacheNeededCallback: Function; | ||
}) => { | ||
if (!symbolInfo.ticker) return; | ||
|
||
const channelId = `${symbolInfo.ticker}/${RESOLUTION_MAP[resolution]}`; | ||
let isFirstRun = true; | ||
|
||
const tearDown = createStoreEffect(store, selectWebsocketUrl, (wsUrl) => { | ||
// if the websocket url changes, force a refresh | ||
if (isFirstRun) { | ||
isFirstRun = false; | ||
} else { | ||
setTimeout(() => onResetCacheNeededCallback(), 0); | ||
} | ||
|
||
let mostRecentFirstPointStartedAt: string | undefined; | ||
const unsub = subscribeToWsValue( | ||
CandlesValuesManager, | ||
{ wsUrl, marketIdAndResolution: channelId }, | ||
({ data }) => { | ||
if (data == null || data.candles.length === 0) { | ||
return; | ||
} | ||
// if we've never seen data before, it's either the existing data or the subscribed message | ||
// either way, we take it as the basis and only send further updates | ||
// there is a small race condition where messages could be missed between | ||
// when trandingview does the rest query and when we start receiving updates | ||
if (mostRecentFirstPointStartedAt == null) { | ||
mostRecentFirstPointStartedAt = data.candles.at(-1)?.startedAt; | ||
return; | ||
} | ||
data.candles.forEach((candle) => { | ||
if (candle.startedAt >= mostRecentFirstPointStartedAt!) { | ||
onRealtimeCallback( | ||
mapCandle(orderbookCandlesToggleOn)({ | ||
...candle, | ||
resolution: candle.resolution as unknown as CandleResolution, | ||
orderbookMidPriceClose: candle.orderbookMidPriceClose ?? undefined, | ||
orderbookMidPriceOpen: candle.orderbookMidPriceOpen ?? undefined, | ||
}) | ||
); | ||
} | ||
}); | ||
mostRecentFirstPointStartedAt = data.candles.at(-1)?.startedAt; | ||
} | ||
); | ||
|
||
// happens on network change or unsubscribe from stream | ||
return () => { | ||
unsub(); | ||
}; | ||
}); | ||
|
||
subscriptionsByGuid[listenerGuid] = { guid: listenerGuid, unsub: tearDown }; | ||
}; | ||
|
||
export const unsubscribeFromStream = (subscriberUID: string) => { | ||
subscriptionsByGuid[subscriberUID]?.unsub(); | ||
subscriptionsByGuid[subscriberUID] = undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters