src.app.shared.event-manager.service.ts Maven / Gradle / Ivy
The newest version!
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/
import { Observable, Observer, Subscription } from 'rxjs';
import { Injectable } from '@angular/core';
import { filter, share } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class EventManagerService {
observable: Observable;
observer: Observer;
constructor() {
this.observable = Observable.create((observer: Observer) => {
this.observer = observer;
}).pipe(share());
}
/**
* Method to broadcast the event to observer
*/
broadcast(event) {
if (this.observer != null) {
this.observer.next(event);
}
}
/**
* Method to subscribe to an event with callback
*/
subscribe(eventName, callback) {
return this.observable.pipe(filter((event) => {
return event.name === eventName;
})).subscribe(callback);
}
/**
* Method to unsubscribe the subscription
*/
destroy(subscriber: Subscription) {
subscriber && subscriber.unsubscribe();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy