add angular material project
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { EventBusService } from './event-bus.service';
|
||||
|
||||
describe('EventBusService', () => {
|
||||
let service: EventBusService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(EventBusService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Subject, Subscription } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { EventData } from './event.class';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class EventBusService {
|
||||
private subject$ = new Subject<EventData>();
|
||||
|
||||
emit(event: EventData) {
|
||||
this.subject$.next(event);
|
||||
}
|
||||
|
||||
on(eventName: string, action: any): Subscription {
|
||||
return this.subject$.pipe(
|
||||
filter((e: EventData) => e.name === eventName),
|
||||
map((e: EventData) => e["value"])).subscribe(action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export class EventData {
|
||||
name: string;
|
||||
value: any;
|
||||
|
||||
constructor(name: string, value: any) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user