@@ -8,8 +8,9 @@ import { every, isEqual } from 'lodash';
8
8
*/
9
9
import { Component } from '@wordpress/element' ;
10
10
import { createHigherOrderComponent } from '@wordpress/compose' ;
11
+ import isShallowEqual from '@wordpress/is-shallow-equal' ;
11
12
12
- export default ( mapNodeToProps ) => createHigherOrderComponent (
13
+ export default ( mapNodeToProps , computeWhenChange ) => createHigherOrderComponent (
13
14
( WrappedComponent ) => {
14
15
return class extends Component {
15
16
constructor ( ) {
@@ -19,6 +20,7 @@ export default ( mapNodeToProps ) => createHigherOrderComponent(
19
20
fallbackStyles : undefined ,
20
21
grabStylesCompleted : false ,
21
22
} ;
23
+ this . shouldRecomputeValues = this . shouldRecomputeValues ( ) . bind ( this ) ;
22
24
23
25
this . bindRef = this . bindRef . bind ( this ) ;
24
26
}
@@ -38,9 +40,24 @@ export default ( mapNodeToProps ) => createHigherOrderComponent(
38
40
this . grabFallbackStyles ( ) ;
39
41
}
40
42
43
+ shouldRecomputeValues ( ) {
44
+ let lastRecomputeValues = [ ] ;
45
+ return ( ) => {
46
+ if ( ! computeWhenChange ) {
47
+ return ! this . state . grabStylesCompleted ;
48
+ }
49
+ const newRecomputeValues = computeWhenChange ( this . props ) ;
50
+ if ( isShallowEqual ( lastRecomputeValues , newRecomputeValues ) ) {
51
+ return false ;
52
+ }
53
+ lastRecomputeValues = newRecomputeValues ;
54
+ return true ;
55
+ } ;
56
+ }
57
+
41
58
grabFallbackStyles ( ) {
42
- const { grabStylesCompleted , fallbackStyles } = this . state ;
43
- if ( this . nodeRef && ! grabStylesCompleted ) {
59
+ const { fallbackStyles } = this . state ;
60
+ if ( this . nodeRef && this . shouldRecomputeValues ( ) ) {
44
61
const newFallbackStyles = mapNodeToProps ( this . nodeRef , this . props ) ;
45
62
if ( ! isEqual ( newFallbackStyles , fallbackStyles ) ) {
46
63
this . setState ( {
0 commit comments