-
-
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.
- Loading branch information
Showing
15 changed files
with
641 additions
and
616 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,3 @@ | ||
interface Constructor<T> { | ||
new (...args: any[]): T; | ||
} |
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 |
---|---|---|
@@ -1,18 +1,15 @@ | ||
declare module 'aurelia-event-aggregator' { | ||
import * as LogManager from 'aurelia-logging'; | ||
export interface Subscription { | ||
export interface Subscription { | ||
dispose(): void; | ||
} | ||
class Handler { | ||
constructor(messageType: any, callback: any); | ||
handle(message: any): any; | ||
} | ||
export class EventAggregator { | ||
constructor(); | ||
publish(event: string | any, data?: any): void; | ||
subscribe(event: string | Function, callback: Function): Subscription; | ||
subscribeOnce(event: string | Function, callback: Function): Subscription; | ||
} | ||
export function includeEventsIn(obj: Object): EventAggregator; | ||
export function configure(config: Object): void; | ||
} | ||
} | ||
export declare class EventAggregator { | ||
private eventLookup; | ||
private messageHandlers; | ||
publish<T>(event: T): void; | ||
publish(event: string, data?: any): void; | ||
subscribe<T>(event: Constructor<T>, callback: (message: T) => void): Subscription; | ||
subscribe(event: string, callback: (message: any, event?: string) => void): Subscription; | ||
subscribeOnce<T>(event: Constructor<T>, callback: (message: T) => void): Subscription; | ||
subscribeOnce(event: string, callback: (message: any, event?: string) => void): Subscription; | ||
} | ||
export declare function includeEventsIn(obj: any): EventAggregator; | ||
export declare function configure(config: any): void; |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,18 +1,15 @@ | ||
declare module 'aurelia-event-aggregator' { | ||
import * as LogManager from 'aurelia-logging'; | ||
export interface Subscription { | ||
export interface Subscription { | ||
dispose(): void; | ||
} | ||
class Handler { | ||
constructor(messageType: any, callback: any); | ||
handle(message: any): any; | ||
} | ||
export class EventAggregator { | ||
constructor(); | ||
publish(event: string | any, data?: any): void; | ||
subscribe(event: string | Function, callback: Function): Subscription; | ||
subscribeOnce(event: string | Function, callback: Function): Subscription; | ||
} | ||
export function includeEventsIn(obj: Object): EventAggregator; | ||
export function configure(config: Object): void; | ||
} | ||
} | ||
export declare class EventAggregator { | ||
private eventLookup; | ||
private messageHandlers; | ||
publish<T>(event: T): void; | ||
publish(event: string, data?: any): void; | ||
subscribe<T>(event: Constructor<T>, callback: (message: T) => void): Subscription; | ||
subscribe(event: string, callback: (message: any, event?: string) => void): Subscription; | ||
subscribeOnce<T>(event: Constructor<T>, callback: (message: T) => void): Subscription; | ||
subscribeOnce(event: string, callback: (message: any, event?: string) => void): Subscription; | ||
} | ||
export declare function includeEventsIn(obj: any): EventAggregator; | ||
export declare function configure(config: any): void; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,106 @@ | ||
import * as LogManager from 'aurelia-logging'; | ||
|
||
const logger = LogManager.getLogger('event-aggregator'); | ||
|
||
class Handler { | ||
constructor(public messageType: any, public callback: (message: any) => void) { | ||
} | ||
|
||
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; | ||
} |
Oops, something went wrong.