Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: wait one tick before setNewOriginalImage #334

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions packages/react-filerobot-image-editor/src/components/App/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,40 @@ const App = () => {

imageBeingLoadedSrc.current = imgSrc;

if (typeof imgToLoad === 'string') {
loadImage(imgToLoad, defaultSavedImageName)
.then(setNewOriginalImage)
.catch(setError)
.finally(triggerResolve);
} else if (imgToLoad instanceof HTMLImageElement) {
if (!imgToLoad.name && defaultSavedImageName) {
// eslint-disable-next-line no-param-reassign
imgToLoad.name = defaultSavedImageName;
}
if (!imgToLoad.complete) {
imgToLoad.addEventListener('load', () => {
setNewOriginalImage(imgToLoad);
triggerResolve();
});
return;
}
// This timeout is a workaround when re-initializing
// the react app from vanilla JS. Due to a bug in react
// the dispatch method that is called in setNewOriginalImage
// still points to the old dispatch method after re-init,
// so we need to wait for one tick to make sure it's updated.
//
// This applies to both URLs and HTMLImageElement, since URLs
// may resolve immediately in some cases, e.g. memory cache.
setTimeout(() => {
if (typeof imgToLoad === 'string') {
loadImage(imgToLoad, defaultSavedImageName)
.then(setNewOriginalImage)
.catch(setError)
.finally(triggerResolve);
} else if (imgToLoad instanceof HTMLImageElement) {
if (!imgToLoad.name && defaultSavedImageName) {
// eslint-disable-next-line no-param-reassign
imgToLoad.name = defaultSavedImageName;
}
if (!imgToLoad.complete) {
imgToLoad.addEventListener('load', () => {
setNewOriginalImage(imgToLoad);
triggerResolve();
});
return;
}

setNewOriginalImage(imgToLoad);
triggerResolve();
} else {
setError(t('invalidImageError'));
triggerResolve();
}
setNewOriginalImage(imgToLoad);
triggerResolve();
} else {
setError(t('invalidImageError'));
triggerResolve();
}
}, 0);
});

const promptDialogIfHasChangeNotSaved = (e) => {
Expand Down