UNPKG

angular7-pubsub

Version:
80 lines (75 loc) 3.15 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; import { ReplaySubject } from 'rxjs'; const ServiceName = 'PubSub Service'; class PubSubService { events = {}; constructor() { } /** * a void function when we don't want to use the subscription from 'RxJs'; * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. * @param {(value: any) => void} callback - callback function * @param {(error: any) => void} error - exception catch function * @param {() => void} complete - complete function */ $sub(event, callback, error, complete) { if (!event) { throw new Error(`[${ServiceName}] => Subscription method must get event name.`); } if (this.events[event] === undefined) { this.events[event] = new ReplaySubject(); } if (typeof callback !== 'function') { return this.events[event].asObservable(); } else { return this.events[event] .asObservable() .subscribe(callback, error, complete); } } /** * Base Module needed to use PubSubService. * @param {event} event - the specific name to subscribe on * @param {eventObject} eventObject - the optional paramter to send when raising the event */ $pub(event, eventObject) { if (!event) { throw new Error(`[${ServiceName}] => Publish method must get event name.`); } else if (!this.events[event]) { return; } this.events[event].next(eventObject); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubService }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubService, decorators: [{ type: Injectable }], ctorParameters: () => [] }); /** * Base Module needed to use PubSubService. * @constructor */ class PubSubModule { static forRoot() { return { ngModule: PubSubModule, providers: [ PubSubService ] }; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: PubSubModule }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubModule }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: PubSubModule, decorators: [{ type: NgModule }] }); /** * Generated bundle index. Do not edit. */ export { PubSubModule, PubSubService }; //# sourceMappingURL=angular7-pubsub.mjs.map