title: Memoized selector with computation in @ngrx/store
body: <p>My normalized ngrx store looks like this:</p>
  
  <pre><code>export interface State {
      carts: EntityState&lt;Cart&gt;;
      items: EntityState&lt;Item&gt;;
  }
  
  export interface Cart {
      id: number;
      maxVolume: number;
  }
  
  export interface Item {
      id: number;
      cartId: number;
      volume: number;
  }
  </code></pre>
  
  <p>It's a pretty basic setup where a cart can contain several items.
  My selector needs to return an array with all the carts with arrays containing their items, but also compute if items are in danger of dropping out of their respective cart:</p>
  
  <pre><code>export const select: MemoizedSelector&lt;object, any&gt; = createSelector(
    selectAllCarts, selectAllItems, 
    (allCarts: Cart[], allItems: Item[]) =&gt; {
      return allCarts.map(c =&gt; {
        const items = allItems.filter(i =&gt; i.cartId == i.id);
        return {
          id: c.id,
          items: items.map(i =&gt; {
            // computations, should not run if cart's items have not changed
            // needs to be memoized
            const computed = isCartOverfilled(i, c, items);
            return {
              id: i.id,
              mightFallOut: computed//computed value needs to be output
            }
          })
        }
      });
  });
  </code></pre>
  
  <p>Every time an item updates, <em>isCartOverfilled</em> will run for each item in the store. But, <em>isCartOverfilled</em> is potentially expensive and depends only on the items inside a cart. When an item updates, e.g. gets added to a cart, <em>isCartOverfilled</em> should execute <strong>ONLY</strong> for the items inside, i.e. memoized by cart id.</p>
  
  <p>How do I achieve this?</p>
  
  <p>I have tried selecting items from a single cart:</p>
  
  <pre><code>export const selectCart = (cartId: number) =&gt; createSelector(
    selectItemsByCartId(cartId), selectCartById(cartId),
    (items: Item[], cart: Cart) =&gt; {
    return {
      id: cart.id,
      items: items.map(i =&gt; {
        const computed = isCartOverfilled(i, cart, items);
        return {
          id: i.id,
          mightFallOut: computed
        }
      })
    }
  });
  </code></pre>
  
  <p>This selector would not excessively compute, but I need all the carts, and I'm not sure if it's doable with a selector.</p>