Update packet listeners

This commit is contained in:
Bill 2021-04-21 22:06:02 -04:00
parent 71f9540e99
commit afba605318
4 changed files with 24 additions and 21 deletions

View File

@ -1,20 +1,23 @@
import { IMessageComposer, IMessageEvent } from 'nitro-renderer'; import { IMessageComposer, IMessageEvent, MessageEvent } from 'nitro-renderer';
import { Nitro } from 'nitro-renderer/src/nitro/Nitro'; import { Nitro } from 'nitro-renderer/src/nitro/Nitro';
import { useEffect } from 'react'; import { useEffect } from 'react';
export function CreateMessageHook(event: IMessageEvent): void export function CreateMessageHook(eventType: typeof MessageEvent, handler: (event: IMessageEvent) => void): void
{ {
useEffect(() => useEffect(() =>
{ {
console.log('register', event); //@ts-ignore
const event = new eventType(handler);
console.log('register', eventType.name);
Nitro.instance.communication.registerMessageEvent(event); Nitro.instance.communication.registerMessageEvent(event);
return () => return () =>
{ {
console.log('unregister', event); console.log('unregister', eventType.name);
Nitro.instance.communication.removeMessageEvent(event); Nitro.instance.communication.removeMessageEvent(event);
} }
}, []); }, [ eventType, handler ]);
} }
export function SendMessageHook(event: IMessageComposer<unknown[]>): void export function SendMessageHook(event: IMessageComposer<unknown[]>): void

View File

@ -133,15 +133,15 @@ export function NavigatorMessageHandler(props: NavigatorMessageHandlerProps): JS
setSearchResults(parser.result.results); setSearchResults(parser.result.results);
}, [ setSearchResults ]); }, [ setSearchResults ]);
CreateMessageHook(new UserInfoEvent(onUserInfoEvent)); CreateMessageHook(UserInfoEvent, onUserInfoEvent);
CreateMessageHook(new RoomForwardEvent(onRoomForwardEvent)); CreateMessageHook(RoomForwardEvent, onRoomForwardEvent);
CreateMessageHook(new RoomInfoOwnerEvent(onRoomInfoOwnerEvent)); CreateMessageHook(RoomInfoOwnerEvent, onRoomInfoOwnerEvent);
CreateMessageHook(new RoomInfoEvent(onRoomInfoEvent)); CreateMessageHook(RoomInfoEvent, onRoomInfoEvent);
CreateMessageHook(new RoomDoorbellEvent(onRoomDoorbellEvent)); CreateMessageHook(RoomDoorbellEvent, onRoomDoorbellEvent);
CreateMessageHook(new RoomDoorbellAcceptedEvent(onRoomDoorbellAcceptedEvent)); CreateMessageHook(RoomDoorbellAcceptedEvent, onRoomDoorbellAcceptedEvent);
CreateMessageHook(new GenericErrorEvent(onGenericErrorEvent)); CreateMessageHook(GenericErrorEvent, onGenericErrorEvent);
CreateMessageHook(new NavigatorMetadataEvent(onNavigatorMetadataEvent)); CreateMessageHook(NavigatorMetadataEvent, onNavigatorMetadataEvent);
CreateMessageHook(new NavigatorSearchEvent(onNavigatorSearchEvent)); CreateMessageHook(NavigatorSearchEvent, onNavigatorSearchEvent);
return null; return null;
} }

View File

@ -65,9 +65,9 @@ export function PurseView(props: PurseViewProps): JSX.Element
}); });
} }
CreateMessageHook(new UserCreditsEvent(onUserCreditsEvent)); CreateMessageHook(UserCreditsEvent, onUserCreditsEvent);
CreateMessageHook(new UserCurrencyEvent(onUserCurrencyEvent)); CreateMessageHook(UserCurrencyEvent, onUserCurrencyEvent);
CreateMessageHook(new UserCurrencyUpdateEvent(onUserCurrencyUpdateEvent)); CreateMessageHook(UserCurrencyUpdateEvent, onUserCurrencyUpdateEvent);
useEffect(() => useEffect(() =>
{ {

View File

@ -1,7 +1,7 @@
import { RoomInfoComposer } from 'nitro-renderer'; import { RoomInfoComposer } from 'nitro-renderer';
import { UserInfoEvent } from 'nitro-renderer/src/nitro/communication/messages/incoming/user/data/UserInfoEvent'; import { UserInfoEvent } from 'nitro-renderer/src/nitro/communication/messages/incoming/user/data/UserInfoEvent';
import { UserInfoDataParser } from 'nitro-renderer/src/nitro/communication/messages/parser/user/data/UserInfoDataParser'; import { UserInfoDataParser } from 'nitro-renderer/src/nitro/communication/messages/parser/user/data/UserInfoDataParser';
import { KeyboardEvent, MouseEvent, useState } from 'react'; import { KeyboardEvent, MouseEvent, useCallback, useState } from 'react';
import { NavigatorEvent } from '../../events'; import { NavigatorEvent } from '../../events';
import { dispatchUiEvent } from '../../hooks/events/ui/ui-event'; import { dispatchUiEvent } from '../../hooks/events/ui/ui-event';
import { CreateMessageHook, SendMessageHook } from '../../hooks/messages/message-event'; import { CreateMessageHook, SendMessageHook } from '../../hooks/messages/message-event';
@ -18,12 +18,12 @@ export function ToolbarView(props: ToolbarViewProps): JSX.Element
const unseenFriendListCount = 0; const unseenFriendListCount = 0;
const unseenAchievementsCount = 0; const unseenAchievementsCount = 0;
const onUserInfoEvent = (event: UserInfoEvent) => const onUserInfoEvent = useCallback((event: UserInfoEvent) =>
{ {
const parser = event.getParser(); const parser = event.getParser();
setUserInfo(parser.userInfo); setUserInfo(parser.userInfo);
}; }, []);
function onKeyUp(event: KeyboardEvent<HTMLInputElement>) function onKeyUp(event: KeyboardEvent<HTMLInputElement>)
{ {
@ -48,7 +48,7 @@ export function ToolbarView(props: ToolbarViewProps): JSX.Element
} }
} }
CreateMessageHook(new UserInfoEvent(onUserInfoEvent)); CreateMessageHook(UserInfoEvent, onUserInfoEvent);
return ( return (
<> <>