Scenario: User is on a page and selects items which he wants in his shopping cart Items are put into a (global) service As soon as the user enters the shopping cart he sees all his items (which are loaded into the component via service) Problem: If the user reloads the shoppingcart, all items are gone Solution: All items are stored via persistence store and will be called only if the user reloads the site Process: For getting the items after a site reload I'm subscribing to a service, which collects all items from the persistence store. The code for setting the items is nearly the same for, when the user enters the shopping cart (either via reload or via direct navigation from within the application) as a subscription, it is only triggered after a site reload. Example code: ngOnInit() { // We need the double code here because: // 1. The normal code is for normal navigation of the user (e.g. from the home component to the shopping cart). // The shopping cart is already filled with items this.shoppingCart = this.shoppingcartService.shoppingCart; // 2. The user reloads the site (or returns later after having it closed) and the shopping cart is filled (in the background) // from the persistance store and emitted to all listeners of the application. this.shoppingcartEmitter = this.shoppingcartService.shoppingCartEmitter.subscribe( shoppingcart => { this.shoppingCart = shoppingcart; }); } That's a simplified version, but anyone can see that, I need two lines for the same behavior. Question: Is there any possibility to avoid the double code? My problem is that, I need to work with the items in the shopping cart, and I would have to write code for working with the shopping cart inside and outside of the subscription. Continue reading...