11import { isBrowser } from './utils'
2+ import { UnityInstance } from './types'
3+ import type UnityWebgl from './index'
24
35interface EventListener {
46 ( ...args : any [ ] ) : void
@@ -7,10 +9,19 @@ interface EventListener {
79type EventListenerOptions = {
810 once ?: boolean
911}
10- type EventMap = Record < string , EventListener [ ] >
12+ type EventsMap = Record < string , EventListener [ ] >
13+ interface UnityEventMap {
14+ beforeMount : ( instance : UnityWebgl ) => void
15+ mounted : ( instance : UnityWebgl , unityInstance : UnityInstance ) => void
16+ beforeUnmount : ( instance : UnityWebgl ) => void
17+ unmounted : ( ) => void
18+ progress : ( progress : number ) => void
19+ debug : ( msg : string ) => void
20+ error : ( error : string | Error ) => void
21+ }
1122
1223export class UnityWebglEvent {
13- private _e : EventMap // event map
24+ private _e : EventsMap // events map
1425
1526 constructor ( ) {
1627 this . _e = { }
@@ -20,7 +31,7 @@ export class UnityWebglEvent {
2031 if ( ! name . startsWith ( 'unity:' ) ) {
2132 name = `unity:${ name } `
2233 }
23- this . emit . apply ( this , [ name , ...args ] )
34+ this . emit . call ( this , name , ...args )
2435 }
2536 }
2637 }
@@ -31,7 +42,17 @@ export class UnityWebglEvent {
3142 * @param listener event listener
3243 * @param options event listener options
3344 */
34- on ( name : string , listener : EventListener , options ?: EventListenerOptions ) {
45+ on < K extends keyof UnityEventMap > (
46+ name : K ,
47+ listener : UnityEventMap [ K ] ,
48+ options ?: EventListenerOptions
49+ ) : this
50+ on ( name : string , listener : EventListener , options ?: EventListenerOptions ) : this
51+ on < K extends keyof UnityEventMap > (
52+ name : string | K ,
53+ listener : EventListener | UnityEventMap [ K ] ,
54+ options ?: EventListenerOptions
55+ ) {
3556 if ( typeof listener !== 'function' ) {
3657 throw new TypeError ( 'listener must be a function' )
3758 }
@@ -59,7 +80,12 @@ export class UnityWebglEvent {
5980 * @param name event name
6081 * @param listener event listener
6182 */
62- off ( name : string , listener ?: EventListener ) {
83+ off < K extends keyof UnityEventMap > ( name : K , listener : UnityEventMap [ K ] ) : this
84+ off ( name : string , listener : EventListener ) : this
85+ off < K extends keyof UnityEventMap > (
86+ name : string | K ,
87+ listener ?: EventListener | UnityEventMap [ K ]
88+ ) {
6389 if ( ! listener ) {
6490 delete this . _e [ name ]
6591 } else {
@@ -82,7 +108,7 @@ export class UnityWebglEvent {
82108 return this
83109 }
84110
85- this . _e [ name ] . forEach ( ( listener ) => listener ( ... args ) )
111+ this . _e [ name ] . forEach ( ( listener ) => listener . apply ( this , args ) )
86112 return this
87113 }
88114
@@ -114,6 +140,6 @@ export class UnityWebglEvent {
114140 if ( ! name . startsWith ( 'unity:' ) ) {
115141 name = `unity:${ name } `
116142 }
117- return this . off ( name , listener )
143+ return this . off ( name , listener as EventListener )
118144 }
119145}
0 commit comments