Currently, I am in the process of properly rewriting a piece of code using rxjs operators. The objective of this code is to make an http call to a webapi controller, which returns a Guid as the id of a long running background operation to be executed on the server. Once the operation is complete, the server will notify the client via SignalR that it has finished, and the client-side code will resume execution (until then, it will remain in a "waiting for response" state).
The original snippet looks like this:
this._service
.myHttpCall(parameter1)
.subscribe(
operationId => {
let sub = this._notifierHub.onOperationSuccess
.subscribe(data => {
if (operationId !== data.operationId)
return;
this.notifySuccess();
sub.unsubscribe();
sub2.unsubscribe();
});
let sub2 = this._notifierHub.onOperationFailure
.subscribe(data => {
if (operationId !== data.operationId)
return;
this.notifyFailure();
sub2.unsubscribe();
sub.unsubscribe();
});
}
);
This _notifier service in Angular exposes a Subject as an Observable, emitting whenever the SignalR HubConnection emits.
I aim to incorporate the management of the operation Id into the myHttpCall method so that, from the perspective of the calling component, the http call will only be considered complete once the actual operation is marked as complete through SignalR.
What I would like to write is something along these lines:
myHttpCall(parameter1: any) {
const url = `${this.action("MyHttpCall")}`;
const body = { parameter1: parameter1 };
this._http
.post(url, body, { responseType: "text" })
.switchMap(operationId => {
const successObservable = this._notifier
.onOperationSuccess
.filter(data => data.operationId === operationId);
const failureObservable = this._notifier
.onOperationFailure
.filter(data => data.operationId === operationId);
return <what?!>
}
What should be included in the method body so that, as the caller, I can use this structure:
this._service
.myHttpCall(parameter1)
.subscribe(
() => this.notifySuccess(),
() => this.notifyFailure(),
() => console.log("HttpCall completed")
);
?