-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding handler * added decorators * refactory * refactory pubsub * update readme * for feature added * refact: fixing * updated code * updated angular * updated readme * updated readme * updated readme * fixing feature module * refact: fixing code * updated example app * added examples * fix: test
- Loading branch information
1 parent
e1d1b20
commit f064060
Showing
43 changed files
with
1,502 additions
and
784 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
[](https://github.com/rupeshtiwari/fsms-angular-pubsub/actions) | ||
|
||
# Angular Pub/Sub Framework for Angular versions | ||
|
||
Angular publish subscribe framework written by using `RxJS` only. | ||
# Angular PubSub | ||
|
||
## Installing Package | ||
Angular 11.x implementation of the [publish subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) Pattern. | ||
|
||
- Run below to install | ||
   [](https://badge.fury.io/js/%40fsms%2Fangular-pubsub)    [](https://david-dm.org/FullStackMaster1/fsms-angular-pubsub)   | ||
|
||
``` | ||
## Installing | ||
|
||
**npm installation** | ||
|
||
```shell | ||
npm i -S @fsms/angular-pubsub | ||
``` | ||
## Using Pub Sub | ||
## Using PubSub For Inline Style | ||
|
||
1. Importing Module | ||
1. **Importing PubsubModule in application module**. | ||
|
||
Initialize module for root in your angular root module | ||
|
||
|
@@ -39,64 +41,72 @@ bootstrap: [RootComponent] | |
|
||
``` | ||
|
||
2. Subscribing to Message | ||
2. **Injecting `PubsubService` as dependency in component** | ||
|
||
Go to desired component and subscribe to a message. | ||
|
||
```ts | ||
import { Component } from '@angular/core'; | ||
import { PubSubService } from '@fsms/angular-pubsub'; | ||
import { PlaceOrder, PlaceOrderType } from './placeorder-message'; | ||
import { PubsubService } from '@fsms/angular-pubsub';// <= HERE | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'], | ||
}) | ||
export class AppComponent { | ||
constructor(private messageService: PubSubService) { | ||
this.messageService.subscribe({ // <= HERE | ||
messageType: PlaceOrderType, | ||
callback: (msg) => console.log('received', msg), | ||
}); | ||
} | ||
constructor( | ||
private pubsubService: PubsubService/* <= HERE */) {} | ||
} | ||
``` | ||
3. Publishing Message | ||
3. **Subscribing to message** | ||
|
||
In `ngOnInit` method of angular, you can subscribe to the events that you want to react upon. | ||
|
||
Now on a button click, I want to publish a message with some payload. | ||
```ts | ||
ngOnInit(): void { | ||
this.pubsubService.subscribe({ // <= HERE | ||
messageType: PlaceOrderType, | ||
callback: (msg) => console.log('received', msg), | ||
}); | ||
} | ||
``` | ||
4. **Publishing Message** | ||
The `publish` method takes one argument where it expect the `message` object. | ||
|
||
Example: Now on a button click, I want to publish a message with some payload. | ||
|
||
```ts | ||
import { Component } from '@angular/core'; | ||
import { PubSubService } from '@fsms/angular-pubsub'; | ||
import { PlaceOrder, PlaceOrderType } from './placeorder-message'; | ||
import { OrderPlaced } from './messages/placeorder-message'; | ||
import { PubsubService } from '@fsms/angular-pubsub';// <= HERE | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'], | ||
}) | ||
export class AppComponent { | ||
constructor(private messageService: PubSubService) { | ||
this.messageService.subscribe({ | ||
messageType: PlaceOrderType, | ||
callback: (msg) => console.log('received', msg), | ||
}); | ||
} | ||
constructor( | ||
private pubsubService: PubsubService/* <= HERE */) {} | ||
|
||
sendMessage($event: KeyboardEvent) { | ||
orderPlaced($event: KeyboardEvent) { | ||
$event.preventDefault(); | ||
this.messageService.publish(new PlaceOrder('20 Apples'));// <= HERE | ||
|
||
this.pubsubService.publish(new OrderPlaced('20 Apples'));// <= HERE | ||
} | ||
} | ||
``` | ||
5. **Unsubscribing Messages** | ||
|
||
|
||
--- | ||
|
||
### Thank You! | ||
**Thank You!** | ||
|
||
**💖 Say 👋 to me!** | ||
Rupesh Tiwari <br/> | ||
<a href="https://www.rupeshtiwari.com"> www.rupeshtiwari.com</a> <br/> | ||
✉️ <a href="mailto:[email protected]?subject=Hi"> Email Rupesh</a><br/> | ||
💖 Say 👋 to me! | ||
Rupesh Tiwari | ||
<a href="https://www.rupeshtiwari.com"> www.rupeshtiwari.com</a> | ||
✉️ <a href="mailto:[email protected]?subject=Hi"> Email Rupesh</a> | ||
Founder of <a href="https://www.fullstackmaster.net"> Fullstack Master</a> | ||
|
Oops, something went wrong.