From 28cd140d9f23aeb3131359a12e669d192b2b60de Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Mon, 18 Mar 2019 12:58:25 -0700 Subject: [PATCH] Cancel ScratchImage load if unmounted --- src/components/scratch-image/scratch-image.jsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/components/scratch-image/scratch-image.jsx b/src/components/scratch-image/scratch-image.jsx index 0727fe5d8..15abe9d3f 100644 --- a/src/components/scratch-image/scratch-image.jsx +++ b/src/components/scratch-image/scratch-image.jsx @@ -39,10 +39,13 @@ class ScratchImage extends React.PureComponent { storage .load(imageSource.assetType, imageSource.assetId) .then(asset => { - const dataURI = asset.encodeDataURI(); - nextImage.setState({ - imageURI: dataURI - }); + if (!nextImage.wasUnmounted) { + const dataURI = asset.encodeDataURI(); + + nextImage.setState({ + imageURI: dataURI + }); + } --this._currentJobs; this.loadPendingImages(); }); @@ -58,6 +61,10 @@ class ScratchImage extends React.PureComponent { const newState = this._loadImageSource(nextProps.imageSource); this.setState(newState); } + componentWillUnmount () { + this.wasUnmounted = true; + ScratchImage._pendingImages.delete(this); + } /** * Calculate the state changes necessary to load the image specified in the provided source info. If the component * is mounted, call setState() with the return value of this function. If the component has not yet mounted, use