Skip to content

Commit 30fb0dc

Browse files
Merge pull request #160 from elraccoone/improvement/stronger-event-listener-typing
Improvement/stronger event listener typing
2 parents b2b6e4f + 40a13bd commit 30fb0dc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* The event map contains all of the keys and their respective parameter types
3+
* which can be used from adding event listeners to a Unity Context instance.
4+
*/
5+
export default interface IUnityContextEventMap {
6+
/**
7+
* While your game is being downloaded from the server and loaded into memory,
8+
* you might want to display some sort of loading indicator informing the user
9+
* of the progression. The built-in progression event listeners can be used
10+
* for such cases. On Progress is emitted while the Unity player is being
11+
* loaded. The parameter contains the progression from 0 to 1. When the game
12+
* is fully loaded into memory and will start execution, the progression will
13+
* hit 1. The event will invoke everytime the progression advances.
14+
*/
15+
progress: number;
16+
17+
/**
18+
* While your application is being downloaded from the server and loaded into
19+
* memory, you might want to display some sort of overlay or loading screen.
20+
* The built-in loaded event listeners can be used for such cases. On Loaded
21+
* is emitted when the Unity player is loaded into memory and execution is
22+
* started. Event will be invoked only once.
23+
*/
24+
loaded: void;
25+
26+
/**
27+
* When your Applications run into a runtime error, you might want to display
28+
* your players any kind of error screen, or debug the problem yourself. The
29+
* built-in error event listeners can be used for such cases. On Error is
30+
* emitted while the Unity Player runs into an error. This is most likely a
31+
* runtime error. The error details and stack trace are passed along via the
32+
* parameter.
33+
*/
34+
error: string;
35+
36+
/**
37+
* The quitted event is emitted in two cases, when the Unity component is
38+
* unmounted, and when Application.Quit is invoked from within your Unity
39+
* Application. In both cases the Unity Player will be unloaded from memory.
40+
*/
41+
quitted: void;
42+
}

source/models/unity-context.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "../declarations/global";
22
import IUnityConfig from "../interfaces/unity-config";
33
import IUnityEvent from "../interfaces/unity-event";
44
import UnityComponent from "../components/unity";
5+
import IUnityContextEventMap from "../interfaces/unity-context-event-map";
56

67
/**
78
* The Unity Context.
@@ -86,8 +87,16 @@ export default class UnityContext {
8687
* @param {string} eventName the event name
8788
* @param {Function} eventListener the event function
8889
* @returns {any} The Function
90+
*
8991
*/
90-
public on(eventName: string, eventListener: Function): void {
92+
public on<MapKey extends keyof IUnityContextEventMap | (string & {})>(
93+
eventName: keyof IUnityContextEventMap | (MapKey & {}),
94+
eventListener: (
95+
parameter: MapKey extends keyof IUnityContextEventMap
96+
? IUnityContextEventMap[MapKey]
97+
: any
98+
) => void
99+
): void {
91100
this.unityEvents.push({ eventName, eventCallback: eventListener });
92101
(window as any).ReactUnityWebGL[eventName] = (parameter: any) =>
93102
eventListener(parameter);

0 commit comments

Comments
 (0)