11import { inspect } from 'ember-utils' ;
22import { assert , warn , Error as EmberError } from 'ember-debug' ;
33import { set } from './property_set' ;
4- import { meta as metaFor , peekMeta , UNDEFINED } from './meta' ;
4+ import { meta as metaFor , peekMeta } from './meta' ;
55import expandProperties from './expand_properties' ;
66import {
77 Descriptor ,
@@ -317,9 +317,8 @@ ComputedPropertyPrototype.didChange = function(obj, keyName) {
317317 return ;
318318 }
319319
320- let cache = meta . readableCache ( ) ;
321- if ( cache !== undefined && cache [ keyName ] !== undefined ) {
322- cache [ keyName ] = undefined ;
320+ let cache = peekCacheFor ( obj ) ;
321+ if ( cache !== undefined && cache . delete ( keyName ) ) {
323322 removeDependentKeys ( this , obj , keyName , meta ) ;
324323 }
325324} ;
@@ -329,20 +328,17 @@ ComputedPropertyPrototype.get = function(obj, keyName) {
329328 return this . _getter . call ( obj , keyName ) ;
330329 }
331330
332- let meta = metaFor ( obj ) ;
333- let cache = meta . writableCache ( ) ;
331+ let cache = getCacheFor ( obj ) ;
334332
335- let result = cache [ keyName ] ;
336- if ( result === UNDEFINED ) {
337- return undefined ;
338- } else if ( result !== undefined ) {
339- return result ;
333+ if ( cache . has ( keyName ) ) {
334+ return cache . get ( keyName ) ;
340335 }
341336
342337 let ret = this . _getter . call ( obj , keyName ) ;
343338
344- cache [ keyName ] = ret === undefined ? UNDEFINED : ret ;
339+ cache . set ( keyName , ret ) ;
345340
341+ let meta = metaFor ( obj ) ;
346342 let chainWatchers = meta . readableChainWatchers ( ) ;
347343 if ( chainWatchers !== undefined ) {
348344 chainWatchers . revalidate ( keyName ) ;
@@ -373,7 +369,7 @@ ComputedPropertyPrototype._throwReadOnlyError = function computedPropertyThrowRe
373369} ;
374370
375371ComputedPropertyPrototype . clobberSet = function computedPropertyClobberSet ( obj , keyName , value ) {
376- let cachedValue = cacheFor ( obj , keyName ) ;
372+ let cachedValue = getCachedValueFor ( obj , keyName ) ;
377373 defineProperty ( obj , keyName , null , cachedValue ) ;
378374 set ( obj , keyName , value ) ;
379375 return value ;
@@ -395,15 +391,9 @@ ComputedPropertyPrototype.setWithSuspend = function computedPropertySetWithSuspe
395391
396392ComputedPropertyPrototype . _set = function computedPropertySet ( obj , keyName , value ) {
397393 let meta = metaFor ( obj ) ;
398- let cache = meta . writableCache ( ) ;
399-
400- let val = cache [ keyName ] ;
401- let hadCachedValue = val !== undefined ;
402-
403- let cachedValue ;
404- if ( hadCachedValue && val !== UNDEFINED ) {
405- cachedValue = val ;
406- }
394+ let cache = getCacheFor ( obj ) ;
395+ let hadCachedValue = cache . has ( keyName ) ;
396+ let cachedValue = cache . get ( keyName ) ;
407397
408398 let ret = this . _setter . call ( obj , keyName , value , cachedValue ) ;
409399
@@ -416,7 +406,7 @@ ComputedPropertyPrototype._set = function computedPropertySet(obj, keyName, valu
416406 addDependentKeys ( this , obj , keyName , meta ) ;
417407 }
418408
419- cache [ keyName ] = ret === undefined ? UNDEFINED : ret ;
409+ cache . set ( keyName , ret ) ;
420410
421411 notifyPropertyChange ( obj , keyName , meta ) ;
422412
@@ -428,10 +418,9 @@ ComputedPropertyPrototype.teardown = function(obj, keyName, meta) {
428418 if ( this . _volatile ) {
429419 return ;
430420 }
431- let cache = meta . readableCache ( ) ;
432- if ( cache !== undefined && cache [ keyName ] !== undefined ) {
421+ let cache = peekCacheFor ( obj ) ;
422+ if ( cache !== undefined && cache . delete ( keyName ) ) {
433423 removeDependentKeys ( this , obj , keyName , meta ) ;
434- cache [ keyName ] = undefined ;
435424 }
436425} ;
437426
@@ -535,6 +524,8 @@ export default function computed(...args) {
535524 return cp ;
536525}
537526
527+ const COMPUTED_PROPERTY_CACHED_VALUES = new WeakMap ( ) ;
528+
538529/**
539530 Returns the cached value for a property, if one exists.
540531 This can be useful for peeking at the value of a computed
@@ -550,39 +541,27 @@ export default function computed(...args) {
550541 @return {Object } the cached value
551542 @public
552543*/
553- function cacheFor ( obj , key ) {
554- let meta = peekMeta ( obj ) ;
555- let cache = meta !== undefined ? meta . source === obj && meta . readableCache ( ) : undefined ;
556- let ret = cache !== undefined ? cache [ key ] : undefined ;
557-
558- if ( ret === UNDEFINED ) {
559- return undefined ;
544+ export function getCacheFor ( obj ) {
545+ let cache = COMPUTED_PROPERTY_CACHED_VALUES . get ( obj ) ;
546+ if ( cache === undefined ) {
547+ cache = new Map ( ) ;
548+ COMPUTED_PROPERTY_CACHED_VALUES . set ( obj , cache ) ;
560549 }
561- return ret ;
550+ return cache ;
562551}
563552
564- cacheFor . set = function ( cache , key , value ) {
565- if ( value === undefined ) {
566- cache [ key ] = UNDEFINED ;
567- } else {
568- cache [ key ] = value ;
553+ export function getCachedValueFor ( obj , key ) {
554+ let cache = COMPUTED_PROPERTY_CACHED_VALUES . get ( obj ) ;
555+ if ( cache !== undefined ) {
556+ return cache . get ( key ) ;
569557 }
570- } ;
571-
572- cacheFor . get = function ( cache , key ) {
573- let ret = cache [ key ] ;
574- if ( ret === UNDEFINED ) {
575- return undefined ;
576- }
577- return ret ;
578- } ;
558+ }
579559
580- cacheFor . remove = function ( cache , key ) {
581- cache [ key ] = undefined ;
582- } ;
560+ export function peekCacheFor ( obj ) {
561+ return COMPUTED_PROPERTY_CACHED_VALUES . get ( obj ) ;
562+ }
583563
584564export {
585565 ComputedProperty ,
586- computed ,
587- cacheFor
566+ computed
588567} ;
0 commit comments