I build a simple confirmation dialog service (Angular 2) with this method:
confirm(body?: string, title?: string): Subject<void> {
this.confirmation = new Subject<void>();
// ... show dialog here... "are you sure?"
return this.confirmation;
}
_onYesClicked() {
// ... closing the dialog
this.confirmation.next();
this.confirmation.complete();
}
_onNoClicked() {
// ... closing the dialog
this.confirmation.complete();
}
Usage:
confirmationService.confirm().subscribe(() => alert("CONFIRMED"));
If someone uses the service, he gets a Subject (which is an Observable) returned and can "subscribe()" to it. The subscription is called when "yes" is clicked and therefore the confirmation was given...
Is this the correct way to do this? And more important... will the call to
this.confirmation.complete();
unsubscribe the subscribed listeners and therefore prevent any lingering references (memory leakage)?