From ae6f4c21859f255cd49a9256307c88907b3f93ce Mon Sep 17 00:00:00 2001 From: Sebastian Clausen Date: Fri, 13 Apr 2018 15:12:28 +0200 Subject: [PATCH] Modified example in README Added example for unsubscribing when destroying a component. --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3642e2e..1038c25 100644 --- a/README.md +++ b/README.md @@ -86,22 +86,25 @@ export class AppModule { } @Component({ template: `

{{mesage}}

-

{{(myOtherMessage$ | async)?.payload.toString()}}

` }) -export class ExampleComponent { - public myOtherMessage$: Observable; +export class ExampleComponent implements OnDestroy { + private subscription: Subscription; + public message: string; constructor(private _mqttService: MqttService) { - this._mqttService.observe('my/topic').subscribe((message: MqttMessage) => { - this.myMessage = message.payload.toString(); + this.subscription = this._mqttService.observe('my/topic').subscribe((message: MqttMessage) => { + this.message = message.payload.toString(); }); - this.myOtherMessage$ = this._mqttService.observe('my/other/topic'); } public unsafePublish(topic: string, message: string): void { this._mqttService.unsafePublish(topic, message, {qos: 1, retain: true}); } + + public ngOnDestroy() { + this.subscription.unsubscribe(); + } } ```