mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-19 23:16:21 +00:00
Pilot: move InfoStand event listeners to useAvatarInfoWidget owner
InfoStandWidgetUserView previously subscribed to three room-session events (RSUBE_BADGES, USER_FIGURE, FAVOURITE_GROUP_UPDATE) and pushed the result back to its parent via a setAvatarInfo prop, with each handler running CloneObject(prev) before patching one field. Three issues with that shape: - CloneObject was deep-cloning the whole AvatarInfoUser shape blindly with no class-prototype awareness; - the three listeners raced on shallow merges across the same prev reference in StrictMode dev; - the subscriptions lived outside the state owner, forcing a prop callback barrier per event. The subscriptions are now in useAvatarInfoWidget — the actual owner of avatarInfo — and call three pure reducers extracted to src/hooks/rooms/widgets/avatarInfo.reducers.ts (applyUserBadgesUpdate, applyUserFigureUpdate, applyFavouriteGroupUpdate). Each reducer returns the same reference when the event doesn't apply so React bail-outs work. The clone now constructs a fresh AvatarInfoUser preserving prototype. dedupeBadges is extracted to its own pure module under src/api/avatar/ so Vitest can cover it without pulling in the renderer. InfoStandWidgetUserView loses the setAvatarInfo prop (parent updated) and the CloneObject import.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { RoomSessionFavoriteGroupUpdateEvent, RoomSessionUserBadgesEvent, RoomSessionUserFigureUpdateEvent } from '@nitrots/nitro-renderer';
|
||||
import { AvatarInfoUser, dedupeBadges, IAvatarInfo } from '../../../api';
|
||||
|
||||
/**
|
||||
* Pure reducers consumed by useAvatarInfoWidget to update the inspected
|
||||
* AvatarInfoUser when room-session events fire. Exported standalone for
|
||||
* Vitest coverage — no React, no renderer dispatcher access.
|
||||
*
|
||||
* Each reducer returns the same reference if the event doesn't apply
|
||||
* (state unchanged) so React bail-outs work and consumers don't re-render
|
||||
* uselessly.
|
||||
*/
|
||||
|
||||
const cloneAvatarInfoUser = (state: AvatarInfoUser): AvatarInfoUser =>
|
||||
{
|
||||
const clone = new AvatarInfoUser(state.type);
|
||||
|
||||
Object.assign(clone, state);
|
||||
|
||||
return clone;
|
||||
};
|
||||
|
||||
export const applyUserBadgesUpdate = (state: IAvatarInfo | null, event: RoomSessionUserBadgesEvent): IAvatarInfo | null =>
|
||||
{
|
||||
if(!(state instanceof AvatarInfoUser)) return state;
|
||||
if(state.webID !== event.userId) return state;
|
||||
|
||||
const dedupedBadges = dedupeBadges(event.badges);
|
||||
|
||||
if(state.badges.join('') === dedupedBadges.join('')) return state;
|
||||
|
||||
const next = cloneAvatarInfoUser(state);
|
||||
|
||||
next.badges = dedupedBadges;
|
||||
|
||||
return next;
|
||||
};
|
||||
|
||||
export const applyUserFigureUpdate = (state: IAvatarInfo | null, event: RoomSessionUserFigureUpdateEvent): IAvatarInfo | null =>
|
||||
{
|
||||
if(!(state instanceof AvatarInfoUser)) return state;
|
||||
if(state.roomIndex !== event.roomIndex) return state;
|
||||
|
||||
const next = cloneAvatarInfoUser(state);
|
||||
|
||||
next.figure = event.figure;
|
||||
next.motto = event.customInfo;
|
||||
next.achievementScore = event.activityPoints;
|
||||
next.nickIcon = event.nickIcon;
|
||||
next.prefixText = event.prefixText;
|
||||
next.prefixColor = event.prefixColor;
|
||||
next.prefixIcon = event.prefixIcon;
|
||||
next.prefixEffect = event.prefixEffect;
|
||||
next.displayOrder = event.displayOrder;
|
||||
next.backgroundId = event.backgroundId;
|
||||
next.standId = event.standId;
|
||||
next.overlayId = event.overlayId;
|
||||
next.cardBackgroundId = event.cardBackgroundId ?? 0;
|
||||
|
||||
return next;
|
||||
};
|
||||
|
||||
export const applyFavouriteGroupUpdate = (
|
||||
state: IAvatarInfo | null,
|
||||
event: RoomSessionFavoriteGroupUpdateEvent,
|
||||
resolveGroupBadge: (groupId: number) => string
|
||||
): IAvatarInfo | null =>
|
||||
{
|
||||
if(!(state instanceof AvatarInfoUser)) return state;
|
||||
if(state.roomIndex !== event.roomIndex) return state;
|
||||
|
||||
const clearGroup = (event.status === -1) || (event.habboGroupId <= 0);
|
||||
const next = cloneAvatarInfoUser(state);
|
||||
|
||||
next.groupId = clearGroup ? -1 : event.habboGroupId;
|
||||
next.groupName = clearGroup ? null : event.habboGroupName;
|
||||
next.groupBadgeId = clearGroup ? null : resolveGroupBadge(event.habboGroupId);
|
||||
|
||||
return next;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GetRoomEngine, GetSessionDataManager, RoomEngineObjectEvent, RoomEngineUseProductEvent, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomSessionPetInfoUpdateEvent, RoomSessionPetStatusUpdateEvent, RoomSessionUserDataUpdateEvent } from '@nitrots/nitro-renderer';
|
||||
import { GetRoomEngine, GetSessionDataManager, RoomEngineObjectEvent, RoomEngineUseProductEvent, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomSessionFavoriteGroupUpdateEvent, RoomSessionPetInfoUpdateEvent, RoomSessionPetStatusUpdateEvent, RoomSessionUserBadgesEvent, RoomSessionUserDataUpdateEvent, RoomSessionUserFigureUpdateEvent } from '@nitrots/nitro-renderer';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { AvatarInfoFurni, AvatarInfoName, AvatarInfoPet, AvatarInfoRentableBot, AvatarInfoUser, AvatarInfoUtilities, CanManipulateFurniture, FurniCategory, IAvatarInfo, IsOwnerOfFurniture, RoomWidgetUpdateRoomObjectEvent, UseProductItem } from '../../../api';
|
||||
import { useNitroEvent, useUiEvent } from '../../events';
|
||||
@@ -6,6 +6,7 @@ import { useFriends } from '../../friends';
|
||||
import { useWired } from '../../wired';
|
||||
import { useObjectDeselectedEvent, useObjectRollOutEvent, useObjectRollOverEvent, useObjectSelectedEvent } from '../engine';
|
||||
import { useRoom } from '../useRoom';
|
||||
import { applyFavouriteGroupUpdate, applyUserBadgesUpdate, applyUserFigureUpdate } from './avatarInfo.reducers';
|
||||
|
||||
const useAvatarInfoWidgetState = () =>
|
||||
{
|
||||
@@ -296,6 +297,21 @@ const useAvatarInfoWidgetState = () =>
|
||||
setIsDecorating(true);
|
||||
});
|
||||
|
||||
useNitroEvent<RoomSessionUserBadgesEvent>(RoomSessionUserBadgesEvent.RSUBE_BADGES, event =>
|
||||
{
|
||||
setAvatarInfo(prev => applyUserBadgesUpdate(prev, event));
|
||||
});
|
||||
|
||||
useNitroEvent<RoomSessionUserFigureUpdateEvent>(RoomSessionUserFigureUpdateEvent.USER_FIGURE, event =>
|
||||
{
|
||||
setAvatarInfo(prev => applyUserFigureUpdate(prev, event));
|
||||
});
|
||||
|
||||
useNitroEvent<RoomSessionFavoriteGroupUpdateEvent>(RoomSessionFavoriteGroupUpdateEvent.FAVOURITE_GROUP_UPDATE, event =>
|
||||
{
|
||||
setAvatarInfo(prev => applyFavouriteGroupUpdate(prev, event, groupId => GetSessionDataManager().getGroupBadge(groupId)));
|
||||
});
|
||||
|
||||
useObjectSelectedEvent(event =>
|
||||
{
|
||||
getObjectInfo(event.id, event.category);
|
||||
|
||||
Reference in New Issue
Block a user