https://github.com/sqlprovider/angular2-pubsub
Publisher/Subscriber service for Angular 2
https://github.com/sqlprovider/angular2-pubsub
angular angular2 angular2-service angular4 angular4-service pubsub
Last synced: 22 days ago
JSON representation
Publisher/Subscriber service for Angular 2
- Host: GitHub
- URL: https://github.com/sqlprovider/angular2-pubsub
- Owner: sqlProvider
- Created: 2017-02-09T13:48:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-29T12:12:00.000Z (over 7 years ago)
- Last Synced: 2025-04-03T21:35:27.797Z (28 days ago)
- Topics: angular, angular2, angular2-service, angular4, angular4-service, pubsub
- Language: TypeScript
- Size: 33.2 KB
- Stars: 10
- Watchers: 1
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Pub/Sub Service for Angular 2
A simple publisher/subscriber service.
[](https://nodei.co/npm/angular2-pubsub/)
## Usage
- Import service in your codes or download via npm or bower.`npm i --save angular2-pubsub`
if you use Angular version 2.x.x plase install from npm with '2.0.6' tag.
`npm i --save [email protected]`
- Add module bundle to imports in your application.
```typescript
...import { PubSubModule } from 'angular2-pubsub'; // <= HERE
@NgModule({
declarations: [
RootComponent,
NavigationComponent,
OverlayComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
PubSubModule.forRoot() // <= AND HERE
],
providers: [],
bootstrap: [RootComponent]
})...
```
- And import service wherever you want## Documentation
#### Class Overview
```typescript
declare class PubSubService {
private events: Object;
$pub(event: string, eventObject?: any): void;
$sub(): undefined;
$sub(event: string): Observable;
$sub(event: string, callback: (value: any) => void): Subscription;
$sub(event: string, callback: (value: any) => void, error: (error: any) => void): Subscription;
$sub(event: string, callback: (value: any) => void, error: (error: any) => void, complete: () => void): Subscription;
}
```#### PubSubService.$pub(event: string, eventObject?: any): void
Publish event to all subscriber.etc.
```typescript
export class OverlayComponent implements OnInit, OnDestroy {
constructor(private pubsub: PubSubService) { }anyFunc(){
this.pubsub.$pub('pleaseCloseSidenav', 'helloIAmOverlay');
}
}
```#### PubSubService.$sub(event: string): Observable
Subscribe to channel.
etc.
```typescript
export class NavigationComponent implements OnInit, OnDestroy {
sideanvSub: any;
constructor(private pubsub: EventDispatcherService) { }ngOnInit() {
// usage of $sub(event: string): >;
this.closeSidenavSub = this.pubsub.$sub('pleaseCloseSidenav').subscribe((from) => {
this.sidenavOpened = false;
});// usage of $sub(event: string, callback: (value: any) => void, error?: (error: any) => void, complete?: () => void): Subscription;
this.openSidenavSub = this.pubsub.$sub('pleaseOpenSidenav', (from) => {
this.sidenavOpened = true;
});
}
ngOnDestroy() {
this.closeSidenavSub.unsubscribe();
this.openSidenavSub.unsubscribe();
}
```**See Changelog** ~~$sub method have one bug. RxJS Subscriber call subscribe method on start like Angular 1.x $scope.$watch.~~
## Build the source
Follow the steps to run the tests and build the source code.
```sh
npm install
npm test
npm run build
```
Commands above will generate the ready to use bundles under the `./dist` folder.