11import PortMixin from 'ember-debug/mixins/port-mixin' ;
22import { compareVersion } from 'ember-debug/utils/version' ;
3+ import { isComputed , isDescriptor , getDescriptorFor } from 'ember-debug/utils/type-check' ;
34
45const Ember = window . Ember ;
56const {
67 Object : EmberObject , inspect : emberInspect , meta : emberMeta , typeOf,
7- computed, get, set, ComputedProperty , guidFor, isNone, removeObserver,
8+ computed, get, set, guidFor, isNone, removeObserver,
89 Mixin, addObserver, cacheFor, VERSION
910} = Ember ;
1011const { oneWay } = computed ;
1112
1213const keys = Object . keys || Ember . keys ;
1314
14- function inspectValue ( value ) {
15+ /**
16+ * Determine the type and get the value of the passed property
17+ * @param {* } object The parent object we will look for `key` on
18+ * @param {string } key The key for the property which points to a computed, EmberObject, etc
19+ * @param {* } computedValue A value that has already been computed with calculateCP
20+ * @return {{inspect: (string|*), type: string}|{computed: boolean, inspect: string, type: string}|{inspect: string, type: string} }
21+ */
22+ function inspectValue ( object , key , computedValue ) {
1523 let string ;
24+ const value = computedValue ? computedValue : object [ key ] ;
25+
26+ // TODO: this is not very clean. We should refactor calculateCP, etc, rather than passing computedValue
27+ if ( computedValue ) {
28+ return { type : `type-${ typeOf ( value ) } ` , inspect : inspect ( value ) } ;
29+ }
30+
1631 if ( value instanceof EmberObject ) {
1732 return { type : 'type-ember-object' , inspect : value . toString ( ) } ;
18- } else if ( isComputed ( value ) ) {
33+ } else if ( isComputed ( object , key ) ) {
1934 string = '<computed>' ;
2035 return { type : 'type-descriptor' , inspect : string , computed : true } ;
2136 } else if ( isDescriptor ( value ) ) {
@@ -25,11 +40,6 @@ function inspectValue(value) {
2540 }
2641}
2742
28- function isDescriptor ( value ) {
29- // Ember >= 1.11
30- return value && typeof value === 'object' && value . isDescriptor ;
31- }
32-
3343function inspect ( value ) {
3444 if ( typeof value === 'function' ) {
3545 return 'function() { ... }' ;
@@ -360,7 +370,7 @@ export default EmberObject.extend(PortMixin, {
360370 if ( ! name && typeof mixin . toString === 'function' ) {
361371 try {
362372 name = mixin . toString ( ) ;
363- } catch ( e ) {
373+ } catch ( e ) {
364374 name = '(Unable to convert Object to string)' ;
365375 }
366376 }
@@ -407,7 +417,7 @@ export default EmberObject.extend(PortMixin, {
407417 }
408418
409419 if ( ! value || ! ( value instanceof CalculateCPError ) ) {
410- value = inspectValue ( value ) ;
420+ value = inspectValue ( object , property , value ) ;
411421 value . computed = true ;
412422
413423
@@ -424,7 +434,7 @@ export default EmberObject.extend(PortMixin, {
424434
425435 let handler = ( ) => {
426436 let value = get ( object , property ) ;
427- value = inspectValue ( value ) ;
437+ value = inspectValue ( object , property , value ) ;
428438 value . computed = computed ;
429439
430440 this . sendMessage ( 'updateProperty' , { objectId, property, value, mixinIndex } ) ;
@@ -501,8 +511,8 @@ function addProperties(properties, hash) {
501511 }
502512 }
503513
504- if ( isComputed ( hash [ prop ] ) ) {
505- options . dependentKeys = ( hash [ prop ] . _dependentKeys || [ ] ) . map ( ( key ) => key . toString ( ) ) ;
514+ if ( isComputed ( hash , prop ) ) {
515+ options . dependentKeys = ( getDescriptorFor ( hash , prop ) . _dependentKeys || [ ] ) . map ( ( key ) => key . toString ( ) ) ;
506516 if ( ! options . isService ) {
507517 if ( typeof hash [ prop ] . _getter === 'function' ) {
508518 options . code = Function . prototype . toString . call ( hash [ prop ] . _getter ) ;
@@ -512,7 +522,7 @@ function addProperties(properties, hash) {
512522 }
513523 options . readOnly = hash [ prop ] . _readOnly ;
514524 }
515- replaceProperty ( properties , prop , hash [ prop ] , options ) ;
525+ replaceProperty ( properties , prop , inspectValue ( hash , prop ) , options ) ;
516526 }
517527}
518528
@@ -531,7 +541,7 @@ function replaceProperty(properties, name, value, options) {
531541 properties . splice ( i , 1 ) ;
532542 }
533543
534- let prop = { name, value : inspectValue ( value ) } ;
544+ let prop = { name, value } ;
535545 prop . isMandatorySetter = options . isMandatorySetter ;
536546 prop . readOnly = options . readOnly ;
537547 prop . dependentKeys = options . dependentKeys || [ ] ;
@@ -611,7 +621,7 @@ function calculateCPs(object, mixinDetails, errorsForObject, expensiveProperties
611621 if ( cache !== undefined || expensiveProperties . indexOf ( item . name ) === - 1 ) {
612622 let value = calculateCP ( object , item . name , errorsForObject ) ;
613623 if ( ! value || ! ( value instanceof CalculateCPError ) ) {
614- item . value = inspectValue ( value ) ;
624+ item . value = inspectValue ( object , item . name , value ) ;
615625 item . value . computed = true ;
616626 }
617627 }
@@ -778,7 +788,6 @@ function getDebugInfo(object) {
778788
779789 let meta = Ember . meta ( object ) ;
780790 for ( let prop in object ) {
781-
782791 // when in Ember 3.1
783792 if ( compareVersion ( VERSION , '3.1.0' ) !== - 1 ) {
784793 // in Ember 3.1+ CP's are eagerly invoked via a normal
@@ -798,9 +807,7 @@ function getDebugInfo(object) {
798807 return debugInfo ;
799808}
800809
801- function isComputed ( value ) {
802- return value instanceof ComputedProperty ;
803- }
810+
804811
805812function toArray ( errors ) {
806813 return keys ( errors ) . map ( key => errors [ key ] ) ;
@@ -810,7 +817,7 @@ function calculateCP(object, property, errorsForObject) {
810817 delete errorsForObject [ property ] ;
811818 try {
812819 return get ( object , property ) ;
813- } catch ( error ) {
820+ } catch ( error ) {
814821 errorsForObject [ property ] = { property, error } ;
815822 return new CalculateCPError ( ) ;
816823 }
0 commit comments