From 1b8dc007a12dd2bd6bab0ed172fef441a2f1ad38 Mon Sep 17 00:00:00 2001 From: Jeffrey Lanters Date: Sun, 25 Sep 2022 13:30:19 +0200 Subject: [PATCH] Updated event listener types --- module/source/hooks/use-event-system.ts | 23 +++++++++++-------- module/source/interfaces/event-listener.ts | 4 +++- module/source/interfaces/event-system-hook.ts | 8 +++++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/module/source/hooks/use-event-system.ts b/module/source/hooks/use-event-system.ts index ef91e48..7af438c 100644 --- a/module/source/hooks/use-event-system.ts +++ b/module/source/hooks/use-event-system.ts @@ -12,7 +12,7 @@ import { IEventSystemHook } from "../interfaces/event-system-hook"; const mountedEventDispatchers: (( eventName: string, ...parameters: ReactUnityEventParameterType[] -) => void)[] = []; +) => ReactUnityEventParameterType)[] = []; /** * Dispatches an event to all mounted event systems. @@ -22,17 +22,16 @@ const mountedEventDispatchers: (( const dispatchReactUnityEvent = ( eventName: string, ...parameters: ReactUnityEventParameterType[] -): void => { +): ReactUnityEventParameterType => { // Loops through all of the mounted event systems and dispatches the event. - // In case there are multiple event systems, the return value - // origin is undefined. - let returnValue: any = undefined; + // In case there are multiple event systems, the return value origin is + // undefined. + let returnValue: ReactUnityEventParameterType = undefined; mountedEventDispatchers.forEach((dispatchEvent) => { returnValue = dispatchEvent(eventName, ...parameters); }); - return returnValue; -} +}; if (isBrowserEnvironment === true) { // It is possible for the application being rendered server side. We'll check @@ -61,7 +60,9 @@ const useEventSystem = (): IEventSystemHook => { */ ( eventName: string, - callback: (...parameters: ReactUnityEventParameterType[]) => void + callback: ( + ...parameters: ReactUnityEventParameterType[] + ) => ReactUnityEventParameterType ) => { // Add the event listener will be added to the array of event listeners. eventListeners.current = [ @@ -82,7 +83,9 @@ const useEventSystem = (): IEventSystemHook => { */ ( eventName: string, - callback: (...parameters: ReactUnityEventParameterType[]) => void + callback: ( + ...parameters: ReactUnityEventParameterType[] + ) => ReactUnityEventParameterType ) => { // The event listener will be filtered from the event listeners array // based on its name and the reference to the callback. @@ -106,7 +109,7 @@ const useEventSystem = (): IEventSystemHook => { ( eventName: string, ...parameters: ReactUnityEventParameterType[] - ): void => { + ): ReactUnityEventParameterType => { // The event listener will be filtered from the event listeners array // based on its name. const eventListener = eventListeners.current.find( diff --git a/module/source/interfaces/event-listener.ts b/module/source/interfaces/event-listener.ts index 6ac4f5b..39e72b5 100644 --- a/module/source/interfaces/event-listener.ts +++ b/module/source/interfaces/event-listener.ts @@ -10,7 +10,9 @@ interface IEventListener { /** * The callback to invoke when the event is fired. */ - callback: (...parameters: ReactUnityEventParameterType[]) => void; + callback: ( + ...parameters: ReactUnityEventParameterType[] + ) => ReactUnityEventParameterType; } export type { IEventListener }; diff --git a/module/source/interfaces/event-system-hook.ts b/module/source/interfaces/event-system-hook.ts index 686d902..aaffd00 100644 --- a/module/source/interfaces/event-system-hook.ts +++ b/module/source/interfaces/event-system-hook.ts @@ -9,7 +9,9 @@ interface IEventSystemHook { */ readonly addEventListener: ( eventName: string, - callback: (...parameters: ReactUnityEventParameterType[]) => void + callback: ( + ...parameters: ReactUnityEventParameterType[] + ) => ReactUnityEventParameterType ) => void; /** @@ -19,7 +21,9 @@ interface IEventSystemHook { */ readonly removeEventListener: ( eventName: string, - callback: (...parameters: ReactUnityEventParameterType[]) => void + callback: ( + ...parameters: ReactUnityEventParameterType[] + ) => ReactUnityEventParameterType ) => void; }