Commit 1e4736a4b83452ca9c07d919dfb3afb0f3c35bc0
1 parent
f74c41d2
Exists in
master
and in
7 other branches
added events-hub service
Showing
4 changed files
with
109 additions
and
2 deletions
Show diff stats
src/app/index.ts
1 | -import {bootstrap} from "ng-forward"; | |
1 | +import {bootstrap, provide} from "ng-forward"; | |
2 | 2 | import {noosferoModuleConfig} from "./index.config"; |
3 | 3 | import {noosferoAngularRunBlock} from "./index.run"; |
4 | 4 | import {MainComponent} from "./main/main.component"; |
... | ... | @@ -22,4 +22,13 @@ angular.module('noosfero.init', ['noosfero.templates.app', 'noosfero.templates.p |
22 | 22 | run(noosferoAngularRunBlock). |
23 | 23 | constant("moment", moment). |
24 | 24 | constant("AuthEvents", AuthEvents); |
25 | -bootstrap(MainComponent); | |
25 | + | |
26 | + | |
27 | +import { EVENTS_HUB_KNOWN_LIST } from './shared/services/events-hub.service'; | |
28 | +import { EVENTS_HUB_KNOWN_EVENTS_NAMES } from './events-hub-known-events.constants'; | |
29 | + | |
30 | +bootstrap(MainComponent, | |
31 | + [ | |
32 | + provide(EVENTS_HUB_KNOWN_LIST, { useConstant: Object.getOwnPropertyNames(EVENTS_HUB_KNOWN_EVENTS_NAMES) }) | |
33 | + ] | |
34 | +); | ... | ... |
... | ... | @@ -0,0 +1,52 @@ |
1 | +import { OpaqueToken } from 'ng-forward'; | |
2 | +import { EventsHubService } from './events-hub.service'; | |
3 | + | |
4 | + | |
5 | +describe("EventsHubService", () => { | |
6 | + let eventsHubService: EventsHubService; | |
7 | + | |
8 | + it("emits events for the known events", (done) => { | |
9 | + let event = "Event1"; | |
10 | + | |
11 | + let eventListener = () => { | |
12 | + }; | |
13 | + // creates the events hub service which known the event "Event1" | |
14 | + eventsHubService = new EventsHubService([ | |
15 | + event | |
16 | + ]); | |
17 | + // subscribe to the event passing the done Function as the eventListener | |
18 | + // if the event emits works the done function is called and the | |
19 | + // test will pass | |
20 | + eventsHubService.subscribeToEvent<any>(event, done); | |
21 | + // emits the event | |
22 | + eventsHubService.emitEvent(event, null); | |
23 | + }); | |
24 | + | |
25 | + it("throws error when trying to emit an unknow event", () => { | |
26 | + let eventListener = () => { | |
27 | + }; | |
28 | + // creates the events hub service which known the event "Event1" | |
29 | + eventsHubService = new EventsHubService([ | |
30 | + 'Event1' | |
31 | + ]); | |
32 | + | |
33 | + // emits the event | |
34 | + expect( | |
35 | + () => { eventsHubService.emitEvent('NotKnownEvent', null); } | |
36 | + ).toThrowError('Unknown event named NotKnownEvent'); | |
37 | + }); | |
38 | + | |
39 | + it("throws error when trying to subscribe to an unknow event", () => { | |
40 | + let eventListener = () => { | |
41 | + }; | |
42 | + // creates the events hub service which known the event "Event1" | |
43 | + eventsHubService = new EventsHubService([ | |
44 | + 'Event1' | |
45 | + ]); | |
46 | + | |
47 | + // emits the event | |
48 | + expect( | |
49 | + () => { eventsHubService.subscribeToEvent<void>('NotKnownEvent', () => {}); } | |
50 | + ).toThrowError('Unknown event named NotKnownEvent'); | |
51 | + }); | |
52 | +}); | |
0 | 53 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,41 @@ |
1 | +import { Injectable, Inject, OpaqueToken, EventEmitter } from 'ng-forward'; | |
2 | + | |
3 | +export const EVENTS_HUB_KNOWN_LIST = new OpaqueToken('EVENTS_HUB_KNOWN_LIST'); | |
4 | + | |
5 | +@Injectable() | |
6 | +@Inject(EVENTS_HUB_KNOWN_LIST) | |
7 | +export class EventsHubService { | |
8 | + | |
9 | + private emitters: Map<string, EventEmitter<any>>; | |
10 | + | |
11 | + constructor(private knownEvents: string[]) { | |
12 | + this.emitters = new Map<string, EventEmitter<any>>(); | |
13 | + this.setupEmitters(); | |
14 | + } | |
15 | + | |
16 | + emitEvent(eventType: string, payload?: any) { | |
17 | + this.checkKnownEvent(eventType); | |
18 | + let event = this.emitters.get(eventType); | |
19 | + if ( event ) this.emitters.get(eventType).next(payload); | |
20 | + } | |
21 | + | |
22 | + subscribeToEvent<T>(eventType: string, generatorOrNext?: ((p?: T) => void), error?: any, complete?: any) { | |
23 | + this.checkKnownEvent(eventType); | |
24 | + let event = this.emitters.get(eventType); | |
25 | + if (event) event.subscribe(generatorOrNext, error, complete); | |
26 | + } | |
27 | + | |
28 | + private setupEmitters() { | |
29 | + for (let i: number = 0; i < this.knownEvents.length; i++) { | |
30 | + this.emitters.set(this.knownEvents[i], new EventEmitter<any>()); | |
31 | + } | |
32 | + } | |
33 | + | |
34 | + private checkKnownEvent(eventType: string) { | |
35 | + if (!this.emitters.has(eventType)) { | |
36 | + throw new Error('Unknown event named ' + eventType.toString()); | |
37 | + } | |
38 | + } | |
39 | + | |
40 | + | |
41 | +} | |
0 | 42 | \ No newline at end of file | ... | ... |