Skip to content

Commit e518974

Browse files
author
Amir Tocker
committed
Use requestAnimationFrame instead of setTimeout. Refactor componentDidMount to use handleResize.
1 parent 45c4e0c commit e518974

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/Util/Util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export debounce from './debounce';
22
export firstDefined from './firstDefined';
33
export closestAbove from './closestAbove';
4+
export {requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame';

src/components/Image/Image.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {Component, PropTypes} from 'react';
22
import cloudinary, {Util} from 'cloudinary-core';
33
import CloudinaryComponent from '../CloudinaryComponent';
4-
import {debounce, firstDefined, closestAbove} from '../../Util';
4+
import {debounce, firstDefined, closestAbove, requestAnimationFrame} from '../../Util';
55

66
function 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

Comments
 (0)