ember-polyfills / ember-cached-decorator-polyfill

Polyfill for RFC 566: @cached
MIT License
20 stars 6 forks source link

feat: access to own cache during recomputation #22

Open buschtoens opened 3 years ago

buschtoens commented 3 years ago

Crude implementation of https://github.com/emberjs/rfcs/pull/656#issuecomment-691698733.

This allows to access the previously cached value during the recomputation of a cache.

class {
  @tracked bidders = [];

  @cached
  get bidSources() {
    const {
      // Returns cached value or `undefined` on first computation.
      bidSources: previousBidSources = [], 
      bidders
    } = this; 

    // Generate a BidSource from each bidder, but avoid doing
    // it more than once per bidder.
    return bidders.map(bidder => {
      return previousBidSources.find(prev => prev.bidder === bidder) ||
        // expensive as it downloads initial state from server
        new BidSource(bidder);
    });
  }
}

This is not intended to be merged and just for PoC'ing the idea. For this to be merged, the RFC would need to be updated.