Search code examples
angularrxjseventemitter

Angular2 EventEmitter does not complete after invoking .error


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?


Solution

  • 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
        });
    }