Fix intermittent loss of options positions and realtime PnL on refresh.

Use stale-while-revalidate for positions API and UI, throttle sync calls, and avoid overwriting displayed PnL with null on transient failures.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-09 18:09:58 +08:00
parent 5a97e14f3e
commit f99900ac40
7 changed files with 146 additions and 17 deletions
+11 -1
View File
@@ -1081,6 +1081,16 @@ function paintRealtimePnl(v){
pnlEl.classList.toggle("pnl-neg", n < 0);
});
}
let lastRealtimePnl = null;
function updateRealtimePnl(v){
if(v != null && !Number.isNaN(Number(v))){
lastRealtimePnl = Number(v);
paintRealtimePnl(v);
return;
}
if(lastRealtimePnl != null) return;
paintRealtimePnl(v);
}
function sumOrdersFloatPnl(orders){
if(!orders || !orders.length) return null;
let total = 0, found = false;
@@ -1153,7 +1163,7 @@ function applyAccountSnapshot(data){
setFundsFieldText("options-trading-usdc", `${Number(data.options_trading_usdc).toFixed(2)} USDC`);
}
if(typeof data.unrealized_pnl !== "undefined"){
paintRealtimePnl(data.unrealized_pnl);
updateRealtimePnl(data.unrealized_pnl);
}
if(typeof data.available_trading_usdt !== "undefined" && data.available_trading_usdt !== null){
latestAvailableUsdt = Number(data.available_trading_usdt);