Monday, June 25, 2018

the take imported from "rxjs/add/operator/take" in an Angular 4 application

Something like this...

return this.store.select(getWidgets).take(1).subscribe({
   widget => this.latestWidget = widget;
}).unsubscribe();

 
 

...can really just be something like this:

return this.store.select(getWidgets).take(1).subscribe({
   widget => this.latestWidget = widget;
});

 
 

The .take(1) makes the Observable go cold. There is no reason to unsubscribe. The listening is at an end. .take(1) is going to take the first value emitted from the Observable while .take(5) would take the first five. The door is held open at the Observable until the count is reached. .take(5) implies four updates triggered since the initial read. Some links:

No comments:

Post a Comment