share() All Returned Observables?
See original GitHub issueWe recently switched from restangular to nswag in an Angular 5/Typescript project. One thing that restangular did nicely was that the results of each http request were shared, so multiple subscriptions would not trigger multiple HTTP requests.
For example:
const service = this.nSwagService.getDataFromApi();
service.subscribe(() => {
// Do something with the results
});
service.subscribe(() => {
// Do something different with the results later
});
The way nswag outputs the Angular Typescript clients, the observables they return are not shared:
return Observable.fromPromise(this.transformOptions(options_)).flatMap(transformedOptions_ => {
return this.http.request("post", url_, transformedOptions_);
}).flatMap((response_: any) => {
return this.transformResult(url_, response_, (r) => this.process(<any>r));
}).catch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.transformResult(url_, response_, (r) => this.process(<any>r));
} catch (e) {
return <Observable<any | null>><any>Observable.throw(e);
}
} else
return <Observable<any | null>><any>Observable.throw(response_);
});
By simply adding .share()
to the end of the previous block, the results of the HTTP request can be easily shared. This could make nswag behave a little more like some other RESTful client libraries (and a little more like promises, for better or for worse). Is there already a way to do this in the Typescript client generator? If not, would you be interested in adding this as an option?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
return a shared Observable from a service - angular
How can I return a shared observable from a services' method? I want to get only one call to the request and share...
Read more >The magic of RXJS sharing operators and their differences
When observable returned by http call emits, our subject X will take that value and share among all subscribers.
Read more >How to share your RxJS observables for improved performance
In this tutorial, we investigate how to cache and share an observable stream so that we don't need to continually call the set...
Read more >Understanding when and why to use multicasting operators ...
When all subscribers have unsubscribed it will unsubscribe from the source Observable. Share operator is same as using publish()+refCount() ...
Read more >Using observables to pass values
An observable can deliver multiple values of any type —literals, messages, or events, ... The subscribe() call returns a Subscription object that has...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Quick answer: undefined is controlled by required in the spec, null is controlled by isNullable or x-isNullable
What happened with this? @RicoSuter