0

I have an EventEmitter that inform a user that notify a component that the state of the application has changed.

This event inform whether a user who is trying to authenticate has been successful.

private emitAuthStatus(success: boolean) {
    if (success) {
        this.locationWatcher.emit({
            authenticated: this.authenticated,
            token: this._authData.token,
            expires: this.expires
        });
    } else {
        this.locationWatcher.error();
    }

    this.locationWatcher.complete();
}

Now when I call .emit followed by .complete everything works properly. But if I call .error, the .complete throws an ObjectUnsubscribedError.

What is .error intended for and am what do I do incorrectly in this code?

Linvi
  • 2,077
  • 1
  • 14
  • 28
  • 2
    But why do you call error() and complete() here? you should call/use them where you are subscribing to `locationWatcher observer` in the component. – micronyks Jul 17 '16 at 13:38
  • Does an error mean that something went wrong with the event, or that the event is raising a special type of event? When I look at the documentation it seems to be raising a special type of events, but surely it should not mean that the `EventEmitter` is no longer subscribed? – Linvi Jul 17 '16 at 13:44
  • 1
    I don't think `error()` and `complete()` are supposed to be used at all. `EventEmitter` currently extends `Observable` but that is an implementation detail and is expected to change at any time without any deprecation phase. Use `EventEmitter` only for `@Output()` only in components and only use the method `emit()`. – Günter Zöchbauer Jul 17 '16 at 14:06
  • Update your code with the component which uses this. So we'll be able to tell you more. – micronyks Jul 17 '16 at 14:08
  • Thank you both I have changed my code to only use the .emit and send the failure information in the emitted object. – Linvi Jul 17 '16 at 15:12

1 Answers1

0

I have changed my code to not use .error and .complete which are not meant to be used to transit information.

private emitAuthStatus() {
    this.locationWatcher.emit({
        // The .authenticated allow us to know if the EventEmitter is a success
        authenticated: this.authenticated, 
        token: this._authData.token,
        expires: this.expires
    });
}
Linvi
  • 2,077
  • 1
  • 14
  • 28