|
23 | 23 | // MODULES // |
24 | 24 |
|
25 | 25 | var logger = require( 'debug' ); |
| 26 | +var hasSameValues = require( '@stdlib/array/base/assert/has-same-values' ); |
26 | 27 | var isCollection = require( '@stdlib/assert/is-collection' ); |
27 | 28 | var isUndefined = require( '@stdlib/assert/is-undefined' ); |
28 | 29 | var copy = require( '@stdlib/utils/copy' ); |
@@ -51,16 +52,34 @@ var debug = logger( 'vega:scale:set:'+prop.name ); |
51 | 52 | * @returns {void} |
52 | 53 | */ |
53 | 54 | function set( value ) { |
54 | | - if ( !isCollection( value ) && !isUndefined( value ) ) { |
| 55 | + var curr; |
| 56 | + var flg; |
| 57 | + |
| 58 | + flg = isCollection( value ); |
| 59 | + if ( !flg && !isUndefined( value ) ) { |
55 | 60 | throw new TypeError( format( 'invalid assignment. `%s` must be an array-like object. Value: `%s`.', prop.name, value ) ); |
56 | 61 | } |
| 62 | + curr = this[ prop.private ]; |
57 | 63 |
|
58 | | - // FIXME: should we perform a deep equal check here in order to avoid a potential false positive change event? |
59 | | - |
60 | | - value = copy( value ); |
61 | | - debug( 'Current value: %s. New value: %s.', JSON.stringify( this[ prop.private ] ), JSON.stringify( value ) ); |
62 | | - this[ prop.private ] = value; |
63 | | - this.emit( 'change', changeEvent( prop.name ) ); |
| 64 | + // Case: collection ?= current |
| 65 | + if ( flg ) { |
| 66 | + // Check for element-wise equality... |
| 67 | + if ( isCollection( curr ) && hasSameValues( value, curr ) ) { |
| 68 | + return; |
| 69 | + } |
| 70 | + value = copy( value ); // TODO: is deep copying necessary here? If the domain can include, e.g., objects, it is. If only primitives, it is less necessary. |
| 71 | + debug( 'Current value: %s. New value: %s.', JSON.stringify( this[ prop.private ] ), JSON.stringify( value ) ); |
| 72 | + this[ prop.private ] = value; |
| 73 | + this.emit( 'change', changeEvent( prop.name ) ); |
| 74 | + return; |
| 75 | + } |
| 76 | + // Case: void ?= current |
| 77 | + if ( curr !== value ) { |
| 78 | + value = copy( value ); |
| 79 | + debug( 'Current value: %s. New value: %s.', JSON.stringify( this[ prop.private ] ), JSON.stringify( value ) ); |
| 80 | + this[ prop.private ] = value; |
| 81 | + this.emit( 'change', changeEvent( prop.name ) ); |
| 82 | + } |
64 | 83 | } |
65 | 84 |
|
66 | 85 |
|
|
0 commit comments