-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): add TypeScript implementation of EventAggregator
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as LogManager from 'aurelia-logging'; | ||
|
||
const logger = LogManager.getLogger('event-aggregator'); | ||
|
||
class Handler { | ||
constructor(public messageType: any, public callback: (message: any) => void) { | ||
this.messageType = messageType; | ||
this.callback = callback; | ||
} | ||
|
||
handle(message: any) { | ||
if (message instanceof this.messageType) { | ||
this.callback.call(null, message); | ||
} | ||
} | ||
} | ||
|
||
export interface Subscription { | ||
dispose(): void; | ||
} | ||
|
||
export class EventAggregator { | ||
private eventLookup: any = {}; | ||
private messageHandlers: Handler[] = []; | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
export function configure(config: any): void { | ||
config.instance(EventAggregator, includeEventsIn(config.aurelia)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface Constructor<T> { | ||
new (...args: any[]): T; | ||
} |