question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

share() All Returned Observables?

See original GitHub issue

We 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
RicoSutercommented, Jan 22, 2019

Quick answer: undefined is controlled by required in the spec, null is controlled by isNullable or x-isNullable

0reactions
eluchsingercommented, Oct 10, 2020

What happened with this? @RicoSuter

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found