forked from aurelia/event-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
146 lines (126 loc) · 4.08 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as LogManager from 'aurelia-logging';
const logger = LogManager.getLogger('event-aggregator');
class Handler {
constructor(private messageType: any, private callback: (message: any) => void) {
}
handle(message: any) {
if (message instanceof this.messageType) {
this.callback.call(null, message);
}
}
}
/**
* Represents a disposable subsciption to an EventAggregator event.
*/
export interface Subscription {
/**
* Disposes the subscription.
*/
dispose(): void;
}
/**
* Enables loosely coupled publish/subscribe messaging.
*/
export class EventAggregator {
private eventLookup: any = {};
private messageHandlers: Handler[] = [];
/**
* Creates an instance of the EventAggregator class.
*/
constructor() {
}
/**
* Publishes a message.
* @param event The event or channel to publish to.
* @param data The data to publish on the channel.
*/
publish<T>(event: T): void;
publish(event: string, data?: any): void;
publish(event: any, data?: any): void {
let subscribers: any[];
let i: number;
if (typeof event === 'string') {
subscribers = this.eventLookup[event];
if (subscribers) {
subscribers = subscribers.slice();
i = subscribers.length;
try {
while (i--) {
subscribers[i](data, event);
}
} catch (e) {
logger.error(e);
}
}
} else {
subscribers = this.messageHandlers.slice();
i = subscribers.length;
try {
while (i--) {
subscribers[i].handle(event);
}
} catch (e) {
logger.error(e);
}
}
}
/**
* Subscribes to a message channel or message type.
* @param event The event channel or event data type.
* @param callback The callback to be invoked when when the specified message is published.
*/
subscribe<T>(event: Constructor<T>, callback: (message: T) => void): Subscription;
subscribe(event: string, callback: (message: any, event?: string) => void): Subscription;
subscribe(event: string|Constructor<any>, callback: (message: any, event?: string) => void): Subscription {
let handler: Function|Handler;
let subscribers: any[];
if (typeof event === 'string') {
handler = callback;
subscribers = this.eventLookup[event] || (this.eventLookup[event] = []);
} else {
handler = new Handler(event, callback);
subscribers = this.messageHandlers;
}
subscribers.push(handler);
return {
dispose() {
let idx = subscribers.indexOf(handler);
if (idx !== -1) {
subscribers.splice(idx, 1);
}
}
};
}
/**
* Subscribes to a message channel or message type, then disposes the subscription automatically after the first message is received.
* @param event The event channel or event data type.
* @param callback The callback to be invoked when when the specified message is published.
*/
subscribeOnce<T>(event: Constructor<T>, callback: (message: T) => void): Subscription;
subscribeOnce(event: string, callback: (message: any, event?: string) => void): Subscription;
subscribeOnce(event: string|Constructor<any>, callback: (message: any, event?: string) => void): Subscription {
let sub = this.subscribe(<any>event, (a, b) => {
sub.dispose();
return callback(a, b);
});
return sub;
}
}
/**
* Includes EA functionality into an object instance.
* @param obj The object to mix Event Aggregator functionality into.
*/
export function includeEventsIn(obj: any): EventAggregator {
let ea = new EventAggregator();
obj.subscribeOnce = (event: any, callback: any) => ea.subscribeOnce(event, callback);
obj.subscribe = (event: any, callback: any) => ea.subscribe(event, callback);
obj.publish = (event: any, data?: any) => ea.publish(event, data);
return ea;
}
/**
* Configures a global EA by merging functionality into the Aurelia instance.
* @param config The Aurelia Framework configuration object used to configure the plugin.
*/
export function configure(config: any): void {
config.instance(EventAggregator, includeEventsIn(config.aurelia));
}