@@ -7,6 +7,7 @@ import Grid from "./grid/index";
7
7
import Config from "../components/config" ;
8
8
import { ConfigOptions } from "../middleware/ConfigOptions" ;
9
9
import Source from "../components/source" ;
10
+ import { CallbackVoid } from "../components/types" ;
10
11
11
12
export default class Events {
12
13
@@ -108,29 +109,43 @@ export default class Events {
108
109
Logger ( LoggerTypes . INFO , `${ chalk . red ( 'Parsers' ) } - failed to scrape the articles.` ) ;
109
110
console . log ( e ) ;
110
111
} ) ;
112
+
113
+ this . getAntennae ( ) . on ( "middleware.error" , ( mid : string , e : any ) => {
114
+ Logger ( LoggerTypes . INFO , `${ chalk . red ( 'Middleware' ) } - an error was caught at ${ mid } .` ) ;
115
+ console . log ( e ) ;
116
+ } ) ;
111
117
}
112
118
113
119
}
114
120
}
115
121
116
122
class Antennae {
117
- private _callbacks : any = { } ;
123
+ private _callbacks : { [ event : string ] : CallbackVoid [ ] } = { } ;
124
+ private _allCallbacks : CallbackVoid [ ] = [ ] ;
125
+
126
+ public on ( eventName : string , callback : CallbackVoid ) : void {
127
+ if ( eventName . length === 0 ) throw Error ( "You cannot create an event for nothing!" ) ;
128
+
129
+ if ( eventName === "*" ) {
130
+ this . _allCallbacks . push ( callback ) ;
131
+ return ;
132
+ }
118
133
119
- public on ( eventName : string , callback : ( ... args : any [ ] ) => void ) : void {
120
- if ( eventName . length === 0 ) throw Error ( "You cannot create an event for nothing!" )
134
+ if ( ! this . _callbacks [ eventName ] )
135
+ this . _callbacks [ eventName ] = [ ] ;
121
136
122
- if ( ! this . _callbacks [ eventName ] ) this . _callbacks [ eventName ] = [ ] ;
123
- this . _callbacks [ eventName ] . push ( callback )
137
+ this . _callbacks [ eventName ] . push ( callback ) ;
124
138
}
125
139
126
140
public emit ( eventName : string , ...args : any [ ] ) : void {
127
141
if ( ! this . _callbacks [ eventName ] ) return ;
128
142
129
143
Grid . getInstance ( ) . emit ( eventName , ...args ) ;
130
144
131
- this . _callbacks [ eventName ] . forEach ( ( callback : any ) => callback ( ...args ) ) ;
145
+ // Call all callbacks
146
+ this . _allCallbacks . forEach ( callback => callback ( eventName , ...args ) ) ;
132
147
133
- if ( this . _callbacks [ '*' ] )
134
- this . _callbacks [ "*" ] . forEach ( ( callback : any ) => callback ( ...args ) ) ;
148
+ // Call specified callback
149
+ this . _callbacks [ eventName ] . forEach ( callback => callback ( ...args ) ) ;
135
150
}
136
151
}
0 commit comments