Skip to content

Instantly share code, notes, and snippets.

@DanWahlin
Last active April 24, 2021 13:28
Show Gist options
  • Save DanWahlin/066ab2af015910d4649a57617f65dd16 to your computer and use it in GitHub Desktop.
Save DanWahlin/066ab2af015910d4649a57617f65dd16 to your computer and use it in GitHub Desktop.
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
}
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