Skip to content

Commit 8132bcb

Browse files
fix: cleanup html layer on component unmount
1 parent e71e981 commit 8132bcb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

packages/html/src/layers/htmlImageLayer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {getAnalyticsOptions} from "../utils/analytics";
66

77
export class HtmlImageLayer{
88
private imgElement: any;
9+
private destroyed = false;
910
htmlPluginState: HtmlPluginState;
1011
constructor(element: HTMLImageElement | null, userCloudinaryImage: CloudinaryImage, plugins?: Plugins, baseAnalyticsOptions?: BaseAnalyticsOptions){
1112
this.imgElement = element;
@@ -14,6 +15,9 @@ export class HtmlImageLayer{
1415

1516
render(element, pluginCloudinaryImage, plugins, this.htmlPluginState, baseAnalyticsOptions)
1617
.then((pluginResponse)=>{ // when resolved updates the src
18+
if (this.destroyed) {
19+
return;
20+
}
1721
this.htmlPluginState.pluginEventSubscription.forEach(fn=>{fn()});
1822
const analyticsOptions = getAnalyticsOptions(baseAnalyticsOptions, pluginResponse);
1923
this.imgElement.setAttribute('src', pluginCloudinaryImage.toURL(analyticsOptions));
@@ -30,8 +34,14 @@ export class HtmlImageLayer{
3034
const pluginCloudinaryImage = cloneDeep(userCloudinaryImage);
3135
render(this.imgElement, pluginCloudinaryImage, plugins, this.htmlPluginState)
3236
.then((pluginResponse)=>{
37+
if (this.destroyed) {
38+
return;
39+
}
3340
const featuredAnalyticsOptions = getAnalyticsOptions(baseAnalyticsOptions, pluginResponse);
3441
this.imgElement.setAttribute('src', pluginCloudinaryImage.toURL(featuredAnalyticsOptions));
3542
});
3643
}
44+
destroy() {
45+
this.destroyed = true;
46+
}
3747
}

packages/react/src/AdvancedImage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ class AdvancedImage extends React.Component <ImgProps> {
9797
}
9898

9999
/**
100-
* On unmount, we cancel the currently running plugins.
100+
* On unmount, we cancel the currently running plugins, and destroy the html layer instance
101101
*/
102102
componentWillUnmount() {
103103
// Safely cancel running events on unmount.
104104
cancelCurrentlyRunningPlugins(this.htmlLayerInstance.htmlPluginState);
105+
this.htmlLayerInstance.destroy();
105106
}
106107

107108
render() {

0 commit comments

Comments
 (0)