Skip to content

Commit a626c0a

Browse files
authored
feat: OPTIC-1479: Improve memory usage of Image tag (#6930)
1 parent cc26109 commit a626c0a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

web/libs/editor/src/tags/object/Image/ImageEntity.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { types } from "mobx-state-tree";
1+
import { types, getParent } from "mobx-state-tree";
22
import { FileLoader } from "../../../utils/FileLoader";
33
import { clamp } from "../../../utils/utilities";
4+
import { FF_IMAGE_MEMORY_USAGE, isFF } from "../../../utils/feature-flags";
45

56
const fileLoader = new FileLoader();
67

@@ -65,12 +66,45 @@ export const ImageEntity = types
6566
/** Is image loaded using `<img/>` tag and cached by the browser */
6667
imageLoaded: false,
6768
}))
69+
.views((self) => ({
70+
get parent() {
71+
// Get the ImageEntityMixin
72+
return getParent(self, 2);
73+
},
74+
get imageCrossOrigin() {
75+
return self.parent?.imageCrossOrigin ?? "anonymous";
76+
},
77+
}))
6878
.actions((self) => ({
6979
preload() {
7080
if (self.ensurePreloaded() || !self.src) return;
7181

72-
self.setDownloading(true);
82+
if (isFF(FF_IMAGE_MEMORY_USAGE)) {
83+
self.setDownloading(true);
84+
new Promise((resolve) => {
85+
const img = new Image();
86+
// Get from the image tag
87+
const crossOrigin = self.imageCrossOrigin;
88+
if (crossOrigin) img.crossOrigin = crossOrigin;
89+
img.onload = () => {
90+
self.setCurrentSrc(self.src);
91+
self.setDownloaded(true);
92+
self.setProgress(1);
93+
self.setDownloading(false);
94+
self.setImageLoaded(true);
95+
resolve();
96+
};
97+
img.onerror = () => {
98+
self.setError(true);
99+
self.setDownloading(false);
100+
resolve();
101+
};
102+
img.src = self.src;
103+
});
104+
return;
105+
}
73106

107+
self.setDownloading(true);
74108
fileLoader
75109
.download(self.src, (_t, _l, progress) => {
76110
self.setProgress(progress);
@@ -87,6 +121,8 @@ export const ImageEntity = types
87121
},
88122

89123
ensurePreloaded() {
124+
if (isFF(FF_IMAGE_MEMORY_USAGE)) return self.currentSrc !== undefined;
125+
90126
if (fileLoader.isError(self.src)) {
91127
self.setDownloading(false);
92128
self.setError(true);

web/libs/editor/src/utils/feature-flags.ts

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ export const FF_LEAP_1173 = "fflag_feat_front_leap_1173_disable_postpone_skip_sh
224224

225225
export const FF_PER_FIELD_COMMENTS = "fflag_feat_all_leap_1430_per_field_comments_100924_short";
226226

227+
export const FF_IMAGE_MEMORY_USAGE = "fflag_feat_front_optic_1479_improve_image_tag_memory_usage_short";
228+
227229
Object.assign(window, {
228230
APP_SETTINGS: {
229231
...(window.APP_SETTINGS ?? {}),

0 commit comments

Comments
 (0)