Update events

This commit is contained in:
Bill 2021-04-22 00:24:40 -04:00
parent 88c9b1b202
commit a2054aa435
3 changed files with 16 additions and 9 deletions

View File

@ -18,7 +18,7 @@ export function CreateEventDispatcherHook(type: string, eventDispatcher: IEventD
{ {
eventDispatcher.removeEventListener(type, handlerRef.current); eventDispatcher.removeEventListener(type, handlerRef.current);
} }
}, [ type, eventDispatcher, handler ]); }, [ type, eventDispatcher ]);
} }
export function DispatchEventHook(eventDispatcher: IEventDispatcher, event: NitroEvent): void export function DispatchEventHook(eventDispatcher: IEventDispatcher, event: NitroEvent): void

View File

@ -9,14 +9,9 @@ export function CreateMessageHook(eventType: typeof MessageEvent, handler: (even
//@ts-ignore //@ts-ignore
const event = new eventType(handler); const event = new eventType(handler);
console.log('register', eventType.name);
Nitro.instance.communication.registerMessageEvent(event); Nitro.instance.communication.registerMessageEvent(event);
return () => return () => Nitro.instance.communication.removeMessageEvent(event);
{
console.log('unregister', eventType.name);
Nitro.instance.communication.removeMessageEvent(event);
}
}, [ eventType, handler ]); }, [ eventType, handler ]);
} }

View File

@ -40,6 +40,8 @@ export function RoomHostView(props: RoomHostViewProps): JSX.Element
const onRoomEngineObjectEvent = useCallback((event: RoomEngineObjectEvent) => const onRoomEngineObjectEvent = useCallback((event: RoomEngineObjectEvent) =>
{ {
if(!eventDispatcher) return;
const objectId = event.objectId; const objectId = event.objectId;
const category = event.category; const category = event.category;
@ -116,12 +118,22 @@ export function RoomHostView(props: RoomHostViewProps): JSX.Element
switch(event.type) switch(event.type)
{ {
case RoomSessionEvent.CREATED: case RoomSessionEvent.CREATED:
setEventDispatcher(new EventDispatcher()); setEventDispatcher(prevValue =>
{
if(prevValue) prevValue.removeAllListeners();
return new EventDispatcher();
});
StartRoomSession(event.session); StartRoomSession(event.session);
return; return;
case RoomSessionEvent.ENDED: case RoomSessionEvent.ENDED:
setRoomSession(null); setRoomSession(null);
setEventDispatcher(null); setEventDispatcher(prevValue =>
{
if(prevValue) prevValue.removeAllListeners();
return null;
});
return; return;
} }
}, []); }, []);