Last active
April 24, 2021 13:28
-
-
Save DanWahlin/066ab2af015910d4649a57617f65dd16 to your computer and use it in GitHub Desktop.
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
import { Injectable } from '@angular/core'; | |
import { Subject, Subscription, Observable } from 'rxjs/Rx'; | |
@Injectable() | |
export class EventBusService { | |
subject = new Subject<any>(); | |
constructor() { } | |
on(event: Events, action: any) : Subscription { | |
return this.subject | |
.filter((e: EmitEvent) => { | |
return e.name === event; | |
}) | |
.map((event: EmitEvent) => { | |
return event.value; | |
}) | |
.subscribe(action); | |
} | |
emit(event: EmitEvent) { | |
this.subject.next(event); | |
} | |
} | |
export class EmitEvent { | |
constructor(public name: any, public value: any) { } | |
} | |
export enum Events { | |
projectItemChanged, | |
codeEditorChanged, | |
windowResized | |
} | |
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
ngOnInit() { | |
this.eventBusSub = this.eventbus.on(Events.projectItemChanged, (projectItem: IProjectItem) => { | |
this.setItem(projectItem); | |
}); | |
this.windowResizeSub = this.eventbus.on(Events.windowResized, (dimension: IDimension) => { | |
if (this.editor) { | |
this.editor.layout(dimension); | |
} | |
}); | |
} | |
ngAfterViewInit() { | |
this.initLoader(); | |
} | |
ngOnDestroy() { | |
this.eventBusSub.unsubscribe(); | |
this.editorSub.unsubscribe(); | |
this.windowResizeSub.unsubscribe(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment