11import React , { Component , PropTypes } from 'react' ;
22import cloudinary , { Util } from 'cloudinary-core' ;
33import CloudinaryComponent from '../CloudinaryComponent' ;
4- import { debounce , firstDefined , closestAbove } from '../../Util' ;
4+ import { debounce , firstDefined , closestAbove , requestAnimationFrame } from '../../Util' ;
55
66function compareObjects ( o , p ) {
77 let i ,
@@ -74,14 +74,20 @@ export default class Image extends CloudinaryComponent {
7474 return state ;
7575 }
7676
77+ handleResize ( e ) {
78+ if ( this . rqf ) return ;
79+ this . rqf = requestAnimationFrame ( ( ) => {
80+ this . rqf = null ;
81+ let newState = this . prepareState ( ) ;
82+ if ( ! Util . isEmpty ( newState . url ) ) {
83+ this . setState ( newState ) ;
84+ }
85+ } ) ;
86+ }
87+
7788 componentDidMount ( ) {
7889 // now that we have a this.element, we need to calculate the URL
79- setTimeout ( ( ) => {
80- let state = this . prepareState ( ) ;
81- if ( state . url !== undefined ) {
82- this . setState ( state ) ;
83- }
84- } , 0 ) ;
90+ this . handleResize ( ) ;
8591 }
8692
8793 componentWillUnmount ( ) {
@@ -104,18 +110,6 @@ export default class Image extends CloudinaryComponent {
104110 }
105111 }
106112
107- handleResize ( e ) {
108- let options = CloudinaryComponent . normalizeOptions ( this . context , this . props ) ;
109- let url = this . getUrl ( options ) ;
110- if ( this . state . responsive ) {
111- url = this . cloudinary_update ( url ) ;
112- let partialState = { url : url } ;
113- if ( this . element ) {
114- this . setState ( partialState ) ;
115- }
116- }
117- }
118-
119113 render ( ) {
120114 var { public_id, responsive, responsive_debounce, children, ...options } = CloudinaryComponent . normalizeOptions ( this . props ,
121115 this . context ) ;
0 commit comments