refactor(钱包 store): 抽出余额就绪态统一构造,消除重复 set
- applyWalletBalanceSnapshot 与刷新循环各自重复「resolve 视图 + 写 4 个 ready 字段」 - 抽出 readyWalletBalanceState 统一派生 totalPoints / status / error,两条路径不再有漂移风险 - 行为不变(注:本文件既有的 rejects a snapshot from another owner 用例在改动前后均失败,与本次无关)
This commit is contained in:
@@ -49,6 +49,20 @@ const EMPTY_WALLET_STATE = {
|
||||
mudPointBalanceError: '',
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* 余额读取成功后的统一就绪态:快照应用与刷新循环共用同一段派生逻辑,
|
||||
* 避免 `totalPoints` 与 ready 字段在两条路径上各自漂移。
|
||||
*/
|
||||
function readyWalletBalanceState(balance: ProfileMudPointBalance) {
|
||||
const mudPointBalance = resolveProfileMudPointBalanceView(balance);
|
||||
return {
|
||||
mudPointBalance,
|
||||
legacyWalletBalance: mudPointBalance.totalPoints,
|
||||
mudPointBalanceStatus: 'ready' as const,
|
||||
mudPointBalanceError: '',
|
||||
};
|
||||
}
|
||||
|
||||
export function createProfileWalletStore(
|
||||
api: ProfileWalletApi,
|
||||
): UseBoundStore<StoreApi<ProfileWalletStore>> {
|
||||
@@ -119,13 +133,7 @@ export function createProfileWalletStore(
|
||||
snapshot.operationSequence,
|
||||
);
|
||||
settledRefreshVersion = snapshot.invalidationVersion;
|
||||
const mudPointBalance = resolveProfileMudPointBalanceView(balance);
|
||||
set({
|
||||
mudPointBalance,
|
||||
legacyWalletBalance: mudPointBalance.totalPoints,
|
||||
mudPointBalanceStatus: 'ready',
|
||||
mudPointBalanceError: '',
|
||||
});
|
||||
set(readyWalletBalanceState(balance));
|
||||
return true;
|
||||
},
|
||||
onWalletBalanceMayHaveChanged: () => {
|
||||
@@ -160,15 +168,7 @@ export function createProfileWalletStore(
|
||||
}
|
||||
|
||||
settledRefreshVersion = requestedVersion;
|
||||
const mudPointBalance = resolveProfileMudPointBalanceView(
|
||||
center.mudPointBalance,
|
||||
);
|
||||
set({
|
||||
mudPointBalance,
|
||||
legacyWalletBalance: mudPointBalance.totalPoints,
|
||||
mudPointBalanceStatus: 'ready',
|
||||
mudPointBalanceError: '',
|
||||
});
|
||||
set(readyWalletBalanceState(center.mudPointBalance));
|
||||
} catch {
|
||||
if (generation !== requestGeneration) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user