2323// MODULES //
2424
2525var setNonEnumerableReadOnly = require ( '@stdlib/utils/define-nonenumerable-read-only-property' ) ;
26+ var setNonEnumerable = require ( '@stdlib/utils/define-nonenumerable-property' ) ;
2627var setReadWriteAccessor = require ( '@stdlib/utils/define-read-write-accessor' ) ;
2728var resolveGetter = require ( './../../../base/resolve-getter' ) ;
2829var accessors = require ( './../../../base/accessors' ) ;
@@ -99,13 +100,15 @@ function nested2views( arr, fields ) {
99100 * @private
100101 * @constructor
101102 * @param {Collection } arr - nested array
103+ * @param {NonNegativeInteger } i - element index
102104 * @returns {Datum } datum instance
103105 */
104- function Datum ( arr ) {
106+ function Datum ( arr , i ) {
105107 var acc = accessors ( arr ) . accessors ;
106- setNonEnumerableReadOnly ( this , '_arr' , arr ) ;
107- setNonEnumerableReadOnly ( this , '_get' , acc [ 0 ] ) ;
108- setNonEnumerableReadOnly ( this , '_set' , acc [ 1 ] ) ;
108+ setNonEnumerable ( this , '_arr' , arr ) ;
109+ setNonEnumerable ( this , '_get' , acc [ 0 ] ) ;
110+ setNonEnumerable ( this , '_set' , acc [ 1 ] ) ;
111+ setNonEnumerableReadOnly ( this , '_i' , i ) ;
109112 return this ;
110113 }
111114
@@ -114,16 +117,37 @@ function nested2views( arr, fields ) {
114117 setReadWriteAccessor ( Datum . prototype , keys [ i ] , getValue ( i ) , setValue ( i ) ) ; // eslint-disable-line max-len
115118 }
116119
120+ // Define a method for ensuring that cached references are up-to-date:
121+ setNonEnumerableReadOnly ( Datum . prototype , '_updateCache' , updateCache ) ;
122+
117123 // Ensure that the returned array correctly serializes to JSON:
118124 setNonEnumerableReadOnly ( Datum . prototype , 'toJSON' , toJSON ) ;
119125
120126 // Create a list of composite views...
121127 out = [ ] ;
122128 for ( i = 0 ; i < M ; i ++ ) {
123- out . push ( new Datum ( oget ( arr , i ) ) ) ;
129+ out . push ( new Datum ( oget ( arr , i ) , i ) ) ;
124130 }
125131 return out ;
126132
133+ /**
134+ * Updates cached references, if necessary.
135+ *
136+ * @private
137+ */
138+ function updateCache ( ) {
139+ var acc ;
140+ var ref ;
141+
142+ ref = oget ( arr , this . _i ) ;
143+ if ( ref !== this . _arr ) {
144+ acc = accessors ( ref ) . accessors ;
145+ this . _arr = ref ;
146+ this . _get = acc [ 0 ] ;
147+ this . _set = acc [ 1 ] ;
148+ }
149+ }
150+
127151 /**
128152 * Returns an accessor for returning the value associated with a field.
129153 *
@@ -141,6 +165,7 @@ function nested2views( arr, fields ) {
141165 * @returns {* } result
142166 */
143167 function get ( ) {
168+ this . _updateCache ( ) ;
144169 return this . _get ( this . _arr , idx ) ;
145170 }
146171 }
@@ -162,6 +187,7 @@ function nested2views( arr, fields ) {
162187 * @param {* } value - value to set
163188 */
164189 function set ( value ) {
190+ this . _updateCache ( ) ;
165191 this . _set ( this . _arr , idx , value ) ;
166192 }
167193 }
0 commit comments