Skip to content

Commit 1405b23

Browse files
committed
Fix Lighter market pulse incremental updates
1 parent 878b300 commit 1405b23

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/components/MarketPulse.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ const precisePriceFormatter = new Intl.NumberFormat('en-US', {
125125
const visibleItems = computed(() => {
126126
const items = quotes.value;
127127
if (!items.length) return [];
128-
return Array.from({ length: renderedSlotCount.value }, (_, offset) => {
128+
const slotCount = Math.min(renderedSlotCount.value, items.length);
129+
return Array.from({ length: slotCount }, (_, offset) => {
129130
const marketIndex = (windowStart.value + offset) % items.length;
130131
const item = items[marketIndex];
131132
return formatMarketItem(item, {
@@ -152,7 +153,7 @@ const shouldAutoScroll = computed(() =>
152153
&& !isHovered.value
153154
&& !isPointerActive.value
154155
&& selectedQuote.value === null
155-
&& quotes.value.length > 0
156+
&& quotes.value.length > renderedSlotCount.value
156157
);
157158
158159
const trackStyle = computed(() => {
@@ -213,7 +214,8 @@ function getVisibleAssetSet(items = quotes.value) {
213214
const length = items.length;
214215
const assets = new Set();
215216
if (!length) return assets;
216-
for (let offset = 0; offset < renderedSlotCount.value; offset += 1) {
217+
const slotCount = Math.min(renderedSlotCount.value, length);
218+
for (let offset = 0; offset < slotCount; offset += 1) {
217219
const item = items[(windowStart.value + offset) % length];
218220
if (item?.asset) {
219221
assets.add(item.asset);
@@ -325,7 +327,7 @@ function scheduleManualScrollEnd() {
325327
}
326328
327329
function onScrollIteration() {
328-
if (!shouldAutoScroll.value || isManualScroll.value || !quotes.value.length) return;
330+
if (!shouldAutoScroll.value || isManualScroll.value || quotes.value.length <= renderedSlotCount.value) return;
329331
windowStart.value = (windowStart.value + 1) % quotes.value.length;
330332
}
331333

src/services/marketApi.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,20 @@ function applyMidsToQuotes(currentItems, mids) {
372372
.filter(Boolean);
373373
}
374374

375+
function mergeQuoteUpdates(currentItems, updates) {
376+
if (!currentItems.length) return updates;
377+
if (!updates.length) return currentItems;
378+
379+
const nextByAsset = new Map(currentItems.map(item => [item.asset, item]));
380+
updates.forEach(item => {
381+
nextByAsset.set(item.asset, item);
382+
});
383+
384+
return TARGET_ASSETS
385+
.map(target => nextByAsset.get(target.asset))
386+
.filter(Boolean);
387+
}
388+
375389
function cacheQuotes(items) {
376390
try {
377391
localStorage.setItem(CACHE_KEY, JSON.stringify({ items, cachedAt: Date.now() }));
@@ -802,12 +816,14 @@ export function subscribeMarketQuotes({ onUpdate, onError } = {}) {
802816
const handleLighterStats = data => {
803817
const items = normalizeLighterPayload(data);
804818
if (!items.length) return;
819+
const isIncrementalUpdate = data?.type === 'update/market_stats';
820+
const nextItems = isIncrementalUpdate ? mergeQuoteUpdates(currentItems, items) : items;
805821
latestMids = null;
806822
socketSource = MARKET_SOURCE_LIGHTER;
807823
lastSocketMidsAt = Date.now();
808824
socketStreaming = true;
809825
stopFallback();
810-
emit(items);
826+
emit(nextItems);
811827
};
812828

813829
const processQueuedSocketData = () => {

0 commit comments

Comments
 (0)