@@ -3,12 +3,10 @@ import * as LogManager from 'aurelia-logging';
3
3
const logger = LogManager . getLogger ( 'event-aggregator' ) ;
4
4
5
5
class Handler {
6
- constructor ( messageType , callback ) {
7
- this . messageType = messageType ;
8
- this . callback = callback ;
6
+ constructor ( public messageType : any , public callback : ( message : any ) => void ) {
9
7
}
10
8
11
- handle ( message ) {
9
+ handle ( message : any ) {
12
10
if ( message instanceof this . messageType ) {
13
11
this . callback . call ( null , message ) ;
14
12
}
@@ -18,7 +16,7 @@ class Handler {
18
16
/**
19
17
* Represents a disposable subsciption to an EventAggregator event.
20
18
*/
21
- interface Subscription {
19
+ export interface Subscription {
22
20
/**
23
21
* Disposes the subscription.
24
22
*/
@@ -28,23 +26,24 @@ interface Subscription {
28
26
/**
29
27
* Enables loosely coupled publish/subscribe messaging.
30
28
*/
31
- export class EventAggregator {
29
+ export class EventAggregator {
30
+ private eventLookup : any = { } ;
31
+ private messageHandlers : Handler [ ] = [ ] ;
32
+
32
33
/**
33
- * Creates an instance of the EventAggregator class.
34
+ * Publishes a message.
35
+ * @param event The event data type to publish to.
34
36
*/
35
- constructor ( ) {
36
- this . eventLookup = { } ;
37
- this . messageHandlers = [ ] ;
38
- }
39
-
37
+ publish < T > ( event : T ) : void ;
40
38
/**
41
39
* Publishes a message.
42
- * @param event The event or channel to publish to.
40
+ * @param event The message channel to publish to.
43
41
* @param data The data to publish on the channel.
44
42
*/
45
- publish ( event : string | any , data ?: any ) : void {
46
- let subscribers ;
47
- let i ;
43
+ publish ( event : string , data ?: any ) : void ;
44
+ publish ( event : any , data ?: any ) : void {
45
+ let subscribers : any [ ] ;
46
+ let i : number ;
48
47
49
48
if ( typeof event === 'string' ) {
50
49
subscribers = this . eventLookup [ event ] ;
@@ -76,12 +75,19 @@ export class EventAggregator {
76
75
77
76
/**
78
77
* Subscribes to a message channel or message type.
79
- * @param event The event channel or event data type.
80
- * @param callback The callback to be invoked when when the specified message is published.
78
+ * @param event The message data Type to subscribe to.
79
+ * @param callback The callback to be invoked when the specified message is published.
80
+ */
81
+ subscribe < T > ( event : Constructor < T > , callback : ( message : T ) => void ) : Subscription ;
82
+ /**
83
+ * Subscribes to a message channel or message type.
84
+ * @param event The message channel to subscribe to.
85
+ * @param callback The callback to be invoked when the specified message is published.
81
86
*/
82
- subscribe ( event : string | Function , callback : Function ) : Subscription {
83
- let handler ;
84
- let subscribers ;
87
+ subscribe ( event : string , callback : ( message : any , event ?: string ) => void ) : Subscription ;
88
+ subscribe ( event : string | Constructor < any > , callback : ( message : any , event ?: string ) => void ) : Subscription {
89
+ let handler : Function | Handler ;
90
+ let subscribers : any [ ] ;
85
91
86
92
if ( typeof event === 'string' ) {
87
93
handler = callback ;
@@ -104,12 +110,19 @@ export class EventAggregator {
104
110
}
105
111
106
112
/**
107
- * Subscribes to a message channel or message type, then disposes the subscription automatically after the first message is received.
108
- * @param event The event channel or event data type .
113
+ * Subscribes to a message type, then disposes the subscription automatically after the first message is received.
114
+ * @param event The message data Type to subscribe to .
109
115
* @param callback The callback to be invoked when when the specified message is published.
110
116
*/
111
- subscribeOnce ( event : string | Function , callback : Function ) : Subscription {
112
- let sub = this . subscribe ( event , ( a , b ) => {
117
+ subscribeOnce < T > ( event : Constructor < T > , callback : ( message : T ) => void ) : Subscription ;
118
+ /**
119
+ * Subscribes to a message channel, then disposes the subscription automatically after the first message is received.
120
+ * @param event The message channel to subscribe to.
121
+ * @param callback The callback to be invoked when when the specified message is published.
122
+ */
123
+ subscribeOnce ( event : string , callback : ( message : any , event ?: string ) => void ) : Subscription ;
124
+ subscribeOnce ( event : string | Constructor < any > , callback : ( message : any , event ?: string ) => void ) : Subscription {
125
+ let sub = this . subscribe ( < any > event , ( a , b ) => {
113
126
sub . dispose ( ) ;
114
127
return callback ( a , b ) ;
115
128
} ) ;
@@ -122,28 +135,18 @@ export class EventAggregator {
122
135
* Includes EA functionality into an object instance.
123
136
* @param obj The object to mix Event Aggregator functionality into.
124
137
*/
125
- export function includeEventsIn ( obj : Object ) : EventAggregator {
138
+ export function includeEventsIn ( obj : any ) : EventAggregator {
126
139
let ea = new EventAggregator ( ) ;
127
-
128
- obj . subscribeOnce = function ( event , callback ) {
129
- return ea . subscribeOnce ( event , callback ) ;
130
- } ;
131
-
132
- obj . subscribe = function ( event , callback ) {
133
- return ea . subscribe ( event , callback ) ;
134
- } ;
135
-
136
- obj . publish = function ( event , data ) {
137
- ea . publish ( event , data ) ;
138
- } ;
139
-
140
+ obj . subscribeOnce = ( event : any , callback : any ) => ea . subscribeOnce ( event , callback ) ;
141
+ obj . subscribe = ( event : any , callback : any ) => ea . subscribe ( event , callback ) ;
142
+ obj . publish = ( event : any , data ?: any ) => ea . publish ( event , data ) ;
140
143
return ea ;
141
144
}
142
145
143
146
/**
144
147
* Configures a global EA by merging functionality into the Aurelia instance.
145
148
* @param config The Aurelia Framework configuration object used to configure the plugin.
146
149
*/
147
- export function configure ( config : Object ) : void {
150
+ export function configure ( config : any ) : void {
148
151
config . instance ( EventAggregator , includeEventsIn ( config . aurelia ) ) ;
149
152
}
0 commit comments