Commit b3953cbdca8e5b027718b5f5c4a94d2d22e4134b
1 parent
1e4736a4
Exists in
master
and in
7 other branches
improving adding a base interface EventsHubKnownEventNames
Showing
6 changed files
with
46 additions
and
27 deletions
Show diff stats
src/app/events-hub-known-events.constants.ts
| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | +import { EventsHubKnownEventNames } from './shared/services/events-hub.service'; | |
| 2 | + | |
| 3 | +export class NoosferoEventsHubKnownEventNames implements EventsHubKnownEventNames { | |
| 4 | + IMAGE_PROFILE_UPDATED: string = 'IMAGE_PROFILE_UPDATED'; | |
| 5 | + PROFILE_INFO_UPDATED: string = 'IMAGE_PROFILE_UPDATED'; | |
| 6 | + ARTICLE_UPDATED: string = 'ARTICLE_UPDATED'; | |
| 7 | + | |
| 8 | + constructor() { | |
| 9 | + } | |
| 10 | + | |
| 11 | + getNames() { | |
| 12 | + return Object.getOwnPropertyNames(this); | |
| 13 | + } | |
| 14 | +} | |
| 0 | 15 | \ No newline at end of file | ... | ... |
src/app/index.ts
| ... | ... | @@ -24,11 +24,11 @@ angular.module('noosfero.init', ['noosfero.templates.app', 'noosfero.templates.p |
| 24 | 24 | constant("AuthEvents", AuthEvents); |
| 25 | 25 | |
| 26 | 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'; | |
| 27 | +import { EVENTS_HUB_KNOW_EVENT_NAMES } from './shared/services/events-hub.service'; | |
| 28 | +import { NoosferoEventsHubKnownEventNames } from './events-hub-known-events'; | |
| 29 | 29 | |
| 30 | 30 | bootstrap(MainComponent, |
| 31 | 31 | [ |
| 32 | - provide(EVENTS_HUB_KNOWN_LIST, { useConstant: Object.getOwnPropertyNames(EVENTS_HUB_KNOWN_EVENTS_NAMES) }) | |
| 32 | + provide(EVENTS_HUB_KNOW_EVENT_NAMES, { useClass: NoosferoEventsHubKnownEventNames }) | |
| 33 | 33 | ] |
| 34 | 34 | ); | ... | ... |
src/app/main/main.component.ts
| ... | ... | @@ -47,6 +47,8 @@ import {PermissionDirective} from "../shared/components/permission/permission.di |
| 47 | 47 | import {SearchComponent} from "../search/search.component"; |
| 48 | 48 | import {SearchFormComponent} from "../search/search-form/search-form.component"; |
| 49 | 49 | |
| 50 | +import { EVENTS_HUB_KNOW_EVENT_NAMES, EventsHubService } from "../shared/services/events-hub.service"; | |
| 51 | +import { NoosferoEventsHubKnownEventNames } from "../events-hub-known-events"; | |
| 50 | 52 | /** |
| 51 | 53 | * @ngdoc controller |
| 52 | 54 | * @name main.MainContentComponent |
| ... | ... | @@ -62,12 +64,20 @@ import {SearchFormComponent} from "../search/search-form/search-form.component"; |
| 62 | 64 | templateUrl: "app/main/main.html", |
| 63 | 65 | providers: [AuthService, SessionService] |
| 64 | 66 | }) |
| 65 | -@Inject(BodyStateClassesService) | |
| 67 | +@Inject(BodyStateClassesService, EVENTS_HUB_KNOW_EVENT_NAMES) | |
| 66 | 68 | export class MainContentComponent { |
| 67 | 69 | |
| 68 | 70 | public themeSkin: string = 'skin-whbl'; |
| 69 | 71 | |
| 70 | - constructor(private bodyStateClassesService: BodyStateClassesService) { | |
| 72 | + constructor( | |
| 73 | + private bodyStateClassesService: BodyStateClassesService, | |
| 74 | + eventsNames: NoosferoEventsHubKnownEventNames, | |
| 75 | + eventsHubService: EventsHubService | |
| 76 | + ) { | |
| 77 | + try { | |
| 78 | + console.log('Events Names', eventsNames); | |
| 79 | + eventsHubService.subscribeToEvent(eventsNames.IMAGE_PROFILE_UPDATED, () => console.log('Event ImageProfileUpdate emitted!')); | |
| 80 | + } catch (e) { } | |
| 71 | 81 | bodyStateClassesService.start({ |
| 72 | 82 | skin: this.themeSkin |
| 73 | 83 | }); | ... | ... |
src/app/shared/services/events-hub.service.spec.ts
| 1 | 1 | import { OpaqueToken } from 'ng-forward'; |
| 2 | -import { EventsHubService } from './events-hub.service'; | |
| 2 | +import { EventsHubService, EventsHubKnownEventNames } from './events-hub.service'; | |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | describe("EventsHubService", () => { |
| 6 | 6 | let eventsHubService: EventsHubService; |
| 7 | - | |
| 7 | + let event1 = 'Event 1'; | |
| 8 | + let eventsHubKnownEventNames = <EventsHubKnownEventNames>{ getNames: () => { return [ event1]; }}; | |
| 8 | 9 | it("emits events for the known events", (done) => { |
| 9 | - let event = "Event1"; | |
| 10 | 10 | |
| 11 | 11 | let eventListener = () => { |
| 12 | 12 | }; |
| 13 | 13 | // creates the events hub service which known the event "Event1" |
| 14 | - eventsHubService = new EventsHubService([ | |
| 15 | - event | |
| 16 | - ]); | |
| 14 | + eventsHubService = new EventsHubService(eventsHubKnownEventNames); | |
| 17 | 15 | // subscribe to the event passing the done Function as the eventListener |
| 18 | 16 | // if the event emits works the done function is called and the |
| 19 | 17 | // test will pass |
| 20 | - eventsHubService.subscribeToEvent<any>(event, done); | |
| 18 | + eventsHubService.subscribeToEvent<any>(event1, done); | |
| 21 | 19 | // emits the event |
| 22 | - eventsHubService.emitEvent(event, null); | |
| 20 | + eventsHubService.emitEvent(event1, null); | |
| 23 | 21 | }); |
| 24 | 22 | |
| 25 | 23 | it("throws error when trying to emit an unknow event", () => { |
| 26 | 24 | let eventListener = () => { |
| 27 | 25 | }; |
| 28 | 26 | // creates the events hub service which known the event "Event1" |
| 29 | - eventsHubService = new EventsHubService([ | |
| 30 | - 'Event1' | |
| 31 | - ]); | |
| 27 | + eventsHubService = new EventsHubService(eventsHubKnownEventNames); | |
| 32 | 28 | |
| 33 | 29 | // emits the event |
| 34 | 30 | expect( |
| ... | ... | @@ -40,9 +36,7 @@ describe("EventsHubService", () => { |
| 40 | 36 | let eventListener = () => { |
| 41 | 37 | }; |
| 42 | 38 | // creates the events hub service which known the event "Event1" |
| 43 | - eventsHubService = new EventsHubService([ | |
| 44 | - 'Event1' | |
| 45 | - ]); | |
| 39 | + eventsHubService = new EventsHubService(eventsHubKnownEventNames); | |
| 46 | 40 | |
| 47 | 41 | // emits the event |
| 48 | 42 | expect( | ... | ... |
src/app/shared/services/events-hub.service.ts
| 1 | 1 | import { Injectable, Inject, OpaqueToken, EventEmitter } from 'ng-forward'; |
| 2 | 2 | |
| 3 | -export const EVENTS_HUB_KNOWN_LIST = new OpaqueToken('EVENTS_HUB_KNOWN_LIST'); | |
| 3 | +export const EVENTS_HUB_KNOW_EVENT_NAMES = new OpaqueToken('EVENTS_HUB_KNOW_EVENT_NAMES'); | |
| 4 | + | |
| 5 | +export interface EventsHubKnownEventNames { | |
| 6 | + getNames(): string[]; | |
| 7 | +} | |
| 4 | 8 | |
| 5 | 9 | @Injectable() |
| 6 | -@Inject(EVENTS_HUB_KNOWN_LIST) | |
| 10 | +@Inject(EVENTS_HUB_KNOW_EVENT_NAMES) | |
| 7 | 11 | export class EventsHubService { |
| 8 | 12 | |
| 9 | 13 | private emitters: Map<string, EventEmitter<any>>; |
| 14 | + private knownEvents: string[] = []; | |
| 10 | 15 | |
| 11 | - constructor(private knownEvents: string[]) { | |
| 16 | + constructor(private eventsHubKnownEventNames: EventsHubKnownEventNames) { | |
| 17 | + this.knownEvents = eventsHubKnownEventNames.getNames(); | |
| 12 | 18 | this.emitters = new Map<string, EventEmitter<any>>(); |
| 13 | 19 | this.setupEmitters(); |
| 14 | 20 | } | ... | ... |