Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe to event emitter once?

// Part of service
public someEvent: EventEmitter<number> = new EventEmitter();

....

// Component
@Component({
  selector: 'some-component',
  template: `...`
})
export class SomeComponent {
  constructor(public service: Service) {
    this.service.someEvent.subscribe((x) => {
      // Do something
    });
  }
}

SomeComponent is displayed in / route. When I navigate to different route in my application, and come back again, SomeComponent will subscribe to the event again, causing callback to fire twice. How to subscribe to the event once or unsubscribe on destroy of the component and subscribe again?

// Can't subscribe after.
ngOnDestroy() {
  this.service.someEvent.unsubscribe();
}
like image 438
Eggy Avatar asked Jan 17 '16 13:01

Eggy


People also ask

Can you subscribe to event emitter?

subscribe() is an EventEmitter method that registers handlers for events emitted by this instance. subscribe() have three optional parameters which can be used to pass values, errors, or completion notification in EventEmitter .

Do I need to unsubscribe from EventEmitter?

Since You are not using child to parent component iteration for fetching data from API there is no need to unsubscribe.

Can we subscribe to an event in Angular?

Angular will subscribe to the add event and call the addTodo() method with the data when the component triggers the next() method.

How do you use event emitter?

Simply use it to emit events from your component. Take a look a the following example. @Component({ selector : 'child', template : ` <button (click)="sendNotification()">Notify my parent! </button> ` }) class Child { @Output() notifyParent: EventEmitter<any> = new EventEmitter(); sendNotification() { this.

What is the best method to subscribe to an EventEmitter?

Method 1 and 2 are probably the easiest to use in most cases. The subscribe method will return a Subscription instance which is also an EventEmitter. The events are the same as the ones for method 2.

How do you subscribe to multiple listeners on the same event?

On the update event, a method is run which logs the time. You can add the same listener over and over again, and each one will subscribe to the event. The second argument of the on () function is a callback that can accept any number of the extra data that was emitted by the event.

How to use emit () function in JavaScript?

The emit () function raises the specified event. First parameter is name of the event as a string and then arguments. An event can be emitted with zero or more arguments. You can specify any name for a custom event in the emit () function. You can also use addListener () methods to subscribe for an event as shown below.

How do I subscribe to events in ETH?

The eth subscribe method This method of subscribing to events is like a “catch-all” method. If you want you can listen to all event logs emitted from the blockchain with this method. Method 1 and 2 are probably the easiest to use in most cases. The subscribe method will return a Subscription instance which is also an EventEmitter.


2 Answers

A call to subscribe returns an instance of Disposable, which has a method dispose.

Or if you are using RxJS 5, dispose has been renamed to unsubscribe (thanks @EricMartinez).

And from the RxJS docs:

...when we're no longer interested in receiving the data as it comes streaming in, we call dispose on our subscription.


Store the result of your call to subscribe and later dispose of the subscription within ngOnDestroy.

RxJS 5:

export class SomeComponent implements OnDestroy {
  constructor(public service: Service) {
    this.subscription = this.service.someEvent.subscribe((x) => {...});
  }
  ngOnDestroy() {
      this.subscription.unsubscribe();
  }
}

RxJS <5:

export class SomeComponent implements OnDestroy {
  constructor(public service: Service) {
    this.subscription = this.service.someEvent.subscribe((x) => {...});
  }
  ngOnDestroy() {
      this.subscription.dispose();
  }
}
like image 174
sdgluck Avatar answered Oct 21 '22 21:10

sdgluck


You can do something like this:

import { OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Rx';

export class SomeComponent implements OnDestroy {
  private _subscription: Subscription;
  constructor(public service: Service) {
    this._subscription = this.service.someEvent.subscribe((x) => {
      // Do something
    });
  }
}

ngOnDestroy(){
  this._subscription.unsubscribe();
}
like image 4
ShellZero Avatar answered Oct 21 '22 21:10

ShellZero