├── .travis.yml ├── .vscode └── settings.json ├── .gitignore ├── .npmignore ├── .angulardoc.json ├── src ├── index.ts ├── angular7-pubsub.module.ts ├── angular7-pubsub.service.spec.ts └── angular7-pubsub.service.ts ├── tslint.json ├── ng-package.json ├── publish.sh ├── karma.conf.js ├── tsconfig-ngc.json ├── tsconfig.json ├── webpack.config.test.js ├── karma-test-runner.js ├── webpack.config.umd.js ├── CHANGELOG.md ├── package.json └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | npm-debug.log -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower.json 2 | tsconfig.json 3 | npm-debug.log -------------------------------------------------------------------------------- /.angulardoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "repoId": "52488623-2601-486f-9537-843c5840db1e", 3 | "lastSync": 0 4 | } -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './angular7-pubsub.service'; 2 | export * from './angular7-pubsub.module'; -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "quotemark": [ 4 | true, 5 | "single" 6 | ] 7 | } 8 | } -------------------------------------------------------------------------------- /ng-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/ng-packagr/ng-package.schema.json", 3 | "dest": "dist", 4 | "lib": { 5 | "entryFile": "src/index.ts" 6 | }, 7 | "allowedNonPeerDependencies": [] 8 | } -------------------------------------------------------------------------------- /publish.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Build project 4 | npm run build 5 | 6 | echo "====== PUBLISHING: angular7-pubsub =====" 7 | npm publish ./dist --access public 8 | echo "====== PUBLISHED: angular7-pubsub =====" 9 | -------------------------------------------------------------------------------- /src/angular7-pubsub.module.ts: -------------------------------------------------------------------------------- 1 | import { PubSubService } from './angular7-pubsub.service'; 2 | import { ModuleWithProviders, NgModule } from '@angular/core'; 3 | 4 | /** 5 | * Base Module needed to use PubSubService. 6 | * @constructor 7 | */ 8 | @NgModule() 9 | export class PubSubModule { 10 | public static forRoot(): ModuleWithProviders { 11 | return { 12 | ngModule: PubSubModule, 13 | providers: [ 14 | PubSubService 15 | ] 16 | }; 17 | } 18 | } -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function (config) { 2 | config.set({ 3 | browsers: ['PhantomJS'], 4 | frameworks: ['jasmine'], 5 | reporters: ['mocha'], 6 | singleRun: true, 7 | preprocessors: { './karma-test-runner.js': ['webpack', 'sourcemap'] }, 8 | files: [ 9 | { pattern: 'node_modules/babel-polyfill/browser.js', instrument: false }, 10 | { pattern: './karma-test-runner.js', watched: false } 11 | ], 12 | webpack: require('./webpack.config.test.js'), 13 | 14 | webpackServer: { noInfo: true } 15 | }); 16 | }; -------------------------------------------------------------------------------- /tsconfig-ngc.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "es2015", 5 | "moduleResolution": "node", 6 | "declaration": true, 7 | "experimentalDecorators": true, 8 | "baseUrl": ".", 9 | "stripInternal": true, 10 | "outDir": "./dist/esm", 11 | "rootDir": "./", 12 | "sourceMap": true, 13 | "inlineSources": true, 14 | "types": [], 15 | "lib": [ 16 | "es6", 17 | "dom" 18 | ] 19 | }, 20 | "files": [ 21 | "src/index.ts" 22 | ], 23 | "angularCompilerOptions": { 24 | "genDir": "./dist/esm", 25 | "skipTemplateCodegen": true, 26 | "strictMetadataEmit": true, 27 | "debug": true 28 | } 29 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": "./", 4 | "target": "ES2022", 5 | "module": "ES2022", 6 | "moduleResolution": "node", 7 | "declaration": true, 8 | "sourceMap": true, 9 | "inlineSources": true, 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "types": ["node", "jasmine"], 14 | "lib": ["dom", "es2022"], 15 | "skipLibCheck": true, 16 | "strict": true 17 | }, 18 | "angularCompilerOptions": { 19 | "enableIvy": true, 20 | "compilationMode": "partial", 21 | "strictInjectionParameters": true, 22 | "strictInputAccessModifiers": true, 23 | "strictTemplates": true 24 | }, 25 | "exclude": [ 26 | "node_modules", 27 | "dist" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /webpack.config.test.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const DefinePlugin = require('webpack/lib/DefinePlugin'); 3 | 4 | const ENV = process.env.NODE_ENV = 'development'; 5 | const HOST = process.env.HOST || 'localhost'; 6 | const PORT = process.env.PORT || 8080; 7 | 8 | const metadata = { 9 | env: ENV, 10 | host: HOST, 11 | port: PORT 12 | }; 13 | 14 | module.exports = { 15 | debug: true, 16 | devtool: 'inline-source-map', 17 | module: { 18 | loaders: [ 19 | { test: /\.ts$/, loader: 'ts', query: { compilerOptions: { noEmit: false } } } 20 | ] 21 | }, 22 | plugins: [ 23 | new DefinePlugin({ 'webpack': { 'ENV': JSON.stringify(metadata.env) } }) 24 | ], 25 | resolve: { 26 | extensions: ['', '.ts', '.js'] 27 | } 28 | }; -------------------------------------------------------------------------------- /karma-test-runner.js: -------------------------------------------------------------------------------- 1 | 2 | Error.stackTraceLimit = Infinity; 3 | 4 | require('reflect-metadata'); 5 | require('zone.js/dist/zone');; 6 | require('zone.js/dist/long-stack-trace-zone'); 7 | require('zone.js/dist/async-test'); 8 | require('zone.js/dist/fake-async-test'); 9 | require('zone.js/dist/sync-test'); 10 | require('zone.js/dist/proxy'); 11 | require('zone.js/dist/jasmine-patch'); 12 | 13 | var testing = require('@angular/core/testing'); 14 | var browser = require('@angular/platform-browser-dynamic/testing'); 15 | 16 | testing.TestBed.initTestEnvironment( 17 | browser.BrowserDynamicTestingModule, 18 | browser.platformBrowserDynamicTesting() 19 | ); 20 | 21 | Object.assign(global, testing); 22 | 23 | var testContext = require.context('./src', true, /\.spec\.ts/); 24 | 25 | function requireAll(requireContext) { 26 | return requireContext.keys().map(requireContext); 27 | } 28 | 29 | var modules = requireAll(testContext); -------------------------------------------------------------------------------- /webpack.config.umd.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | // Webpack and its plugins 3 | const CompressionPlugin = require('compression-webpack-plugin'); 4 | 5 | const ENV = process.env.NODE_ENV = 'production'; 6 | 7 | const metadata = { 8 | env: ENV 9 | }; 10 | module.exports = { 11 | devtool: 'source-map', 12 | entry: { 13 | 'main': './src/index.ts' 14 | }, 15 | mode: 'production', 16 | externals: { 17 | '@angular/core': { 18 | root: ['ng', 'core'], 19 | commonjs: '@angular/core', 20 | commonjs2: '@angular/core', 21 | amd: '@angular/core' 22 | }, 23 | '@angular/common': { 24 | root: ['ng', 'common'], 25 | commonjs: '@angular/common', 26 | commonjs2: '@angular/common', 27 | amd: '@angular/common' 28 | }, 29 | '@angular/platform-browser': { 30 | root: ['ng', 'platformBrowser'], 31 | commonjs: '@angular/platform-browser', 32 | commonjs2: '@angular/platform-browser', 33 | amd: '@angular/platform-browser' 34 | }, 35 | 'rxjs/Subscription': { 36 | root: ['rx', 'Subscription'], 37 | commonjs: 'rxjs/Subscription', 38 | commonjs2: 'rxjs/Subscription', 39 | amd: 'rxjs/Subscription' 40 | } 41 | }, 42 | module: { 43 | rules: [ 44 | { test: /\.ts$/, loader: 'ts-loader', query: { compilerOptions: { noEmit: false } } } 45 | ] 46 | }, 47 | output: { 48 | path: path.resolve(__dirname, 'dist/umd'), 49 | filename: 'angular7-pubsub.js', 50 | libraryTarget: 'umd', 51 | library: 'angular7-pubsub' 52 | }, 53 | plugins: [ 54 | new CompressionPlugin({test: /\.css$|\.html$|\.js$|\.map$/ }) 55 | ], 56 | resolve: { 57 | extensions: ['.ts', '.js'], 58 | modules: [path.resolve(__dirname, 'node_modules')] 59 | } 60 | }; -------------------------------------------------------------------------------- /src/angular7-pubsub.service.spec.ts: -------------------------------------------------------------------------------- 1 | /* tslint:disable:no-unused-variable */ 2 | 3 | import { PubSubService } from './angular7-pubsub.service'; 4 | import { Observable, Subscriber } from 'rxjs'; 5 | 6 | describe('PubSubService', (): void => { 7 | let pubService: PubSubService; 8 | 9 | beforeEach(() => { 10 | pubService = new PubSubService(); 11 | }); 12 | 13 | describe('$sub', (): void => { 14 | it('should throw an error when event is falsy', (): void => { 15 | expect(() => pubService.$sub(undefined)).toThrow(); 16 | }); 17 | 18 | it('should return an observable when there is no callback', (): void => { 19 | let result: any = pubService.$sub('test'); 20 | expect(result instanceof Observable).toBeTruthy(); 21 | }); 22 | 23 | it('should return a subscriber when there is a callback specified', (): void => { 24 | let result: any = pubService.$sub('test', (v: any): void => { 25 | ''; 26 | }); 27 | expect(result instanceof Subscriber).toBeTruthy(); 28 | }); 29 | }); 30 | 31 | describe('$pub', (): void => { 32 | it('should throw an error when event is falsy', (): void => { 33 | expect(() => pubService.$pub(undefined)).toThrow(); 34 | }); 35 | 36 | it('should do nothing when an event is not registered', (): void => { 37 | expect(() => pubService.$pub('not-registered')).not.toThrow(); 38 | }); 39 | 40 | it('should publish with parameters if the event is registered', (): void => { 41 | let subscriberEventSpy: jasmine.Spy = jasmine.createSpy('subscriberEvent'); 42 | pubService.$sub('new-event', subscriberEventSpy); 43 | 44 | pubService.$pub('new-event', 'foo'); 45 | 46 | expect(subscriberEventSpy).toHaveBeenCalledWith('foo'); 47 | }); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v4.0.4 2 | - Updated NPM Readme 3 | 4 | ## v4.0.3 5 | - Default version is now 4.x.x 6 | - Fixed type error 7 | 8 | ## Next v4.0.2 9 | - Fixed the issue that created multiple pubsub services (#7). 10 | 11 | ## Next v4.0.0 12 | - Only version changed for anyone using Angular 4.0.0 13 | 14 | ## v2.0.6 15 | - Fixed type error. 16 | 17 | ## v2.0.5 18 | - Fixed the issue that created multiple pubsub services (#7). 19 | 20 | ## v2.0.1 21 | - $pub command return undefined when no event subscribe. 22 | - $sub method bug solved. RxJS Subscriber no longer call subscribe method on start. 23 | 24 | ## v2.0.0 25 | - PubSubService moved into PubSubModule. 26 | - Added unit tests for the service and project restructure updated to use the benefits of webpack and bundling. 27 | 28 | ## v1.1.1 29 | Recovery fix and added interfaces. 30 | 31 | ### Class Overview 32 | ```typescript 33 | declare class PubSubService{ 34 | private events: Object; 35 | $pub(event: string, eventObject?: any): void; 36 | $sub: { 37 | (): undefined; 38 | (event: string): Observable; 39 | (event: string, callback: (value: any) => void): Subscription; 40 | (event: string, callback: (value: any) => void, error: (error: any) => void): Subscription; 41 | (event: string, callback: (value: any) => void, error: (error: any) => void, complete: () => void): Subscription; 42 | } 43 | } 44 | ``` 45 | 46 | ------- 47 | ## v1.1.0 48 | 49 | Added overload to $sub method for to be useful. 50 | 51 | ### Class Overview 52 | ```typescript 53 | declare class PubSubService{ 54 | private events: Object; 55 | $pub(event: string, eventObject?: any): void; 56 | $sub(event: string): >; 57 | $sub(event: string, callback: (value: any) => void, error?: (error: any) => void, complete?: () => void): Subscription; 58 | } 59 | ``` 60 | ------- 61 | ## v1.0.0 62 | A simple publisher/subscriber service. 63 | 64 | ### Class Overview 65 | ```typescript 66 | declare class PubSubService{ 67 | private events: Object; 68 | $pub(event: string, eventObject?: any): void; 69 | $sub(event: string): >; 70 | $sub(event: string, callback: (value: any) => void, error?: (error: any) => void, complete?: () => void): Subscription; 71 | } 72 | ``` -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular7-pubsub", 3 | "version": "3.0.0", 4 | "description": "Pub/Sub service for Angular (7-17)", 5 | "main": "./dist/bundles/angular7-pubsub.umd.js", 6 | "module": "./dist/fesm2022/angular7-pubsub.js", 7 | "typings": "./dist/index.d.ts", 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/mrebati/angular7-pubsub.git" 11 | }, 12 | "scripts": { 13 | "test": "ng test", 14 | "clean": "rimraf dist", 15 | "build": "ng-packagr -p ng-package.json", 16 | "publish": "./publish.sh" 17 | }, 18 | "keywords": [ 19 | "Angular", 20 | "Angular 17", 21 | "pubsub", 22 | "publisher-subscriber", 23 | "angular-service" 24 | ], 25 | "author": { 26 | "name": "Mohammad Rebati", 27 | "email": "m@rebati.net", 28 | "url": "http://github.com/mrebati" 29 | }, 30 | "contributors": [ 31 | { 32 | "name": "Mert Susur", 33 | "email": "mail@mertsusur.com", 34 | "url": "http://github.com/msusur" 35 | }, 36 | { 37 | "name": "Dustin Cleveland", 38 | "url": "https://github.com/dustincleveland" 39 | }, 40 | { 41 | "name": "Mohammad Rebati", 42 | "url": "https://github.com/mrebati" 43 | } 44 | ], 45 | "license": "ISC", 46 | "bugs": { 47 | "url": "https://github.com/mrebati/angular7-pubsub/issues" 48 | }, 49 | "homepage": "https://github.com/mrebati/angular7-pubsub#readme", 50 | "peerDependencies": { 51 | "@angular/common": "^17.0.0", 52 | "@angular/core": "^17.0.0", 53 | "rxjs": "^7.8.1" 54 | }, 55 | "devDependencies": { 56 | "@angular/cli": "^17.0.0", 57 | "@angular/common": "^17.0.0", 58 | "@angular/compiler": "^17.0.0", 59 | "@angular/compiler-cli": "^17.0.0", 60 | "@angular/core": "^17.0.0", 61 | "@types/jasmine": "^5.1.0", 62 | "@types/node": "^20.0.0", 63 | "jasmine-core": "^5.1.0", 64 | "karma": "^6.4.1", 65 | "karma-chrome-launcher": "^3.1.0", 66 | "karma-coverage": "^2.2.1", 67 | "karma-jasmine": "^5.1.0", 68 | "ng-packagr": "^17.0.0", 69 | "rimraf": "^5.0.0", 70 | "rxjs": "^7.8.1", 71 | "tslib": "^2.6.0", 72 | "typescript": "~5.2.0", 73 | "zone.js": "~0.14.0" 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pub/Sub Service for Angular 7 abd above (Now available for Angular 17 and above) 2 | 3 | A simple publisher/subscriber service. 4 | This repository is forked from angular-pubsub and 5 | the only reason I changed it was to use RXJs version 6 and above. and updated the angular packages. 6 | 7 | All credit goes to the owner and contributers of angular-pubsub repo. 8 | 9 | [![NPM](https://nodei.co/npm/angular7-pubsub.png?downloads=true&stars=true)](https://nodei.co/npm/angular7-pubsub/) 10 | 11 | ## Usage 12 | 13 | - Import service in your codes or download via npm or bower. 14 | 15 | `npm i --save angular7-pubsub` 16 | 17 | - Add module bundle to imports in your application. 18 | 19 | ```typescript 20 | ... 21 | 22 | import { PubSubModule } from 'angular7-pubsub'; // <= HERE 23 | 24 | @NgModule({ 25 | declarations: [ 26 | RootComponent, 27 | NavigationComponent, 28 | OverlayComponent 29 | ], 30 | imports: [ 31 | BrowserModule, 32 | FormsModule, 33 | HttpModule, 34 | PubSubModule.forRoot() // <= AND HERE 35 | ], 36 | providers: [], 37 | bootstrap: [RootComponent] 38 | }) 39 | 40 | ... 41 | ``` 42 | 43 | - And import service wherever you want 44 | 45 | ## Documentation 46 | 47 | ### Class Overview 48 | 49 | ```typescript 50 | declare class PubSubService { 51 | private events: Object; 52 | $pub(event: string, eventObject?: any): void; 53 | $sub(): undefined; 54 | $sub(event: string): Observable; 55 | $sub(event: string, callback: (value: any) => void): Subscription; 56 | $sub(event: string, callback: (value: any) => void, error: (error: any) => void): Subscription; 57 | $sub(event: string, callback: (value: any) => void, error: (error: any) => void, complete: () => void): Subscription; 58 | } 59 | ``` 60 | 61 | #### PubSubService.$pub(event: string, eventObject?: any): void 62 | 63 | Publish event to all subscriber. 64 | 65 | etc. 66 | 67 | ```typescript 68 | export class OverlayComponent implements OnInit, OnDestroy { 69 | constructor(private pubsub: PubSubService) { } 70 | 71 | anyFunc(){ 72 | this.pubsub.$pub('pleaseCloseSidenav', 'helloIAmOverlay'); 73 | } 74 | } 75 | ``` 76 | 77 | #### PubSubService.$sub(event: string): Observable 78 | 79 | Subscribe to channel. 80 | 81 | etc. 82 | 83 | ```typescript 84 | export class NavigationComponent implements OnInit, OnDestroy { 85 | closeSidenavSub: Subscription; 86 | openSidenavSub: Subscription; 87 | constructor(private pubsub: EventDispatcherService) { } 88 | 89 | ngOnInit() { 90 | // usage of $sub(event: string): >; 91 | this.closeSidenavSub = this.pubsub.$sub('pleaseCloseSidenav').subscribe((from) => { 92 | this.sidenavOpened = false; 93 | }); 94 | 95 | // usage of $sub(event: string, callback: (value: any) => void, error?: (error: any) => void, complete?: () => void): Subscription; 96 | this.openSidenavSub = this.pubsub.$sub('pleaseOpenSidenav', (from) => { 97 | this.sidenavOpened = true; 98 | }); 99 | } 100 | ngOnDestroy() { 101 | this.closeSidenavSub.unsubscribe(); 102 | this.openSidenavSub.unsubscribe(); 103 | } 104 | ``` 105 | 106 | **See Changelog** ~~$sub method have one bug. RxJS Subscriber call subscribe method on start like Angular 1.x $scope.$watch.~~ 107 | 108 | ## Build the source 109 | 110 | Follow the steps to run the tests and build the source code. 111 | 112 | ```sh 113 | npm install 114 | npm test 115 | npm run build 116 | ``` 117 | 118 | Commands above will generate the ready to use bundles under the `./dist` folder. 119 | -------------------------------------------------------------------------------- /src/angular7-pubsub.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { ReplaySubject, Observable, Subscription } from 'rxjs'; 3 | 4 | const ServiceName: string = 'PubSub Service'; 5 | 6 | @Injectable() 7 | export class PubSubService implements IPubSubService { 8 | private events = {}; 9 | 10 | constructor() {} 11 | 12 | /** 13 | * a subscription which returns everything as an observable from @see {RxJs} 14 | * this method does not use eventObject 15 | * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. 16 | * @returns {Observable} an observable. 17 | * @summary remember to unsubscribe in onDestroy of the components 18 | */ 19 | public $sub(event: string): Observable; 20 | 21 | /** 22 | * a subscription which returns the callback, error and complete as a subscription from @see {RxJs} 23 | * this method does not use eventObject 24 | * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. 25 | * @param {(value: any) => void} callback - callback method which gets the value from eventObject. 26 | * @returns {Subscription} a subscription which we can listen to. 27 | * @summary remember to unsubscribe in onDestroy of the components 28 | */ 29 | public $sub(event: string, callback: (value: any) => void): Subscription; 30 | 31 | /** 32 | * a subscription which returns the callback, error and complete as a subscription from @see {RxJs} 33 | * this method does not use eventObject 34 | * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. 35 | * @param {(error: any) => void} error - exception catch function 36 | * @param {() => void} complete - complete function 37 | * @returns {Subscription} a subscription which we can listen to. 38 | * @summary remember to unsubscribe in onDestroy of the components 39 | */ 40 | public $sub( 41 | event: string, 42 | callback: (value: any) => void, 43 | error: (error: any) => void 44 | ): Subscription; 45 | 46 | /** 47 | * a subscription which returns the callback, error and complete as a subscription from @see {RxJs} 48 | * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. 49 | * @param {(value: any) => void} callback - callback function 50 | * @param {(error: any) => void} error - exception catch function 51 | * @param {() => void} complete - complete function 52 | * @returns {Subscription} a subscription which we can listen to. 53 | * @summary remember to unsubscribe in onDestroy of the components 54 | */ 55 | public $sub( 56 | event: string, 57 | callback: (value: any) => void, 58 | error: (error: any) => void, 59 | complete: () => void 60 | ): Subscription; 61 | 62 | /** 63 | * a void function when we don't want to use the subscription from 'RxJs'; 64 | * @param {event} event - the specific name to subscribe on - the names must be specific to those we publish. 65 | * @param {(value: any) => void} callback - callback function 66 | * @param {(error: any) => void} error - exception catch function 67 | * @param {() => void} complete - complete function 68 | */ 69 | public $sub(event: string,callback?: (value: any) => void,error?: (error: any) => void,complete?: () => void 70 | ) { 71 | if (!event) { 72 | throw new Error( 73 | `[${ServiceName}] => Subscription method must get event name.` 74 | ); 75 | } 76 | 77 | if (this.events[event] === undefined) { 78 | this.events[event] = new ReplaySubject(); 79 | } 80 | 81 | if (typeof callback !== 'function') { 82 | return this.events[event].asObservable(); 83 | } else { 84 | return this.events[event] 85 | .asObservable() 86 | .subscribe(callback, error, complete); 87 | } 88 | } 89 | 90 | /** 91 | * Base Module needed to use PubSubService. 92 | * @param {event} event - the specific name to subscribe on 93 | * @param {eventObject} eventObject - the optional paramter to send when raising the event 94 | */ 95 | public $pub(event: string, eventObject?: any) { 96 | if (!event) { 97 | throw new Error( 98 | `[${ServiceName}] => Publish method must get event name.` 99 | ); 100 | } else if (!this.events[event]) { 101 | return; 102 | } 103 | 104 | this.events[event].next(eventObject); 105 | } 106 | } 107 | 108 | export interface IPubSubService { 109 | $pub(event: string, eventObject?: any): void; 110 | $sub(event: string): Observable; 111 | $sub(event: string, callback: (value: any) => void): Subscription; 112 | $sub( 113 | event: string, 114 | callback: (value: any) => void, 115 | error: (error: any) => void 116 | ): Subscription; 117 | $sub( 118 | event: string, 119 | callback: (value: any) => void, 120 | error: (error: any) => void, 121 | complete: () => void 122 | ): Subscription; 123 | } 124 | 125 | interface I$sub { 126 | (event: string): Observable; 127 | (event: string, callback: (value: any) => void): Subscription; 128 | ( 129 | event: string, 130 | callback: (value: any) => void, 131 | error: (error: any) => void 132 | ): Subscription; 133 | ( 134 | event: string, 135 | callback: (value: any) => void, 136 | error: (error: any) => void, 137 | complete: () => void 138 | ): Subscription; 139 | } 140 | --------------------------------------------------------------------------------