Skip to content

Commit

Permalink
Updated event listener types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylanters committed Sep 25, 2022
1 parent 6e7c576 commit 1b8dc00
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
23 changes: 13 additions & 10 deletions module/source/hooks/use-event-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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 = [
Expand All @@ -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.
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion module/source/interfaces/event-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
8 changes: 6 additions & 2 deletions module/source/interfaces/event-system-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ interface IEventSystemHook {
*/
readonly addEventListener: (
eventName: string,
callback: (...parameters: ReactUnityEventParameterType[]) => void
callback: (
...parameters: ReactUnityEventParameterType[]
) => ReactUnityEventParameterType
) => void;

/**
Expand All @@ -19,7 +21,9 @@ interface IEventSystemHook {
*/
readonly removeEventListener: (
eventName: string,
callback: (...parameters: ReactUnityEventParameterType[]) => void
callback: (
...parameters: ReactUnityEventParameterType[]
) => ReactUnityEventParameterType
) => void;
}

Expand Down

0 comments on commit 1b8dc00

Please sign in to comment.