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

feat: OPTIC-1479: Improve memory usage of Image tag #6930

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 38 additions & 2 deletions web/libs/editor/src/tags/object/Image/ImageEntity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { types } from "mobx-state-tree";
import { types, getParent } from "mobx-state-tree";
import { FileLoader } from "../../../utils/FileLoader";
import { clamp } from "../../../utils/utilities";
import { FF_IMAGE_MEMORY_USAGE, isFF } from "../../../utils/feature-flags";

const fileLoader = new FileLoader();

Expand Down Expand Up @@ -65,12 +66,45 @@ export const ImageEntity = types
/** Is image loaded using `<img/>` tag and cached by the browser */
imageLoaded: false,
}))
.views((self) => ({
get parent() {
// Get the ImageEntityMixin
return getParent(self, 2);
},
get imageCrossOrigin() {
return self.parent?.imageCrossOrigin ?? "anonymous";
},
}))
.actions((self) => ({
preload() {
if (self.ensurePreloaded() || !self.src) return;

self.setDownloading(true);
if (isFF(FF_IMAGE_MEMORY_USAGE)) {
self.setDownloading(true);
new Promise((resolve) => {
const img = new Image();
// Get from the image tag
const crossOrigin = self.imageCrossOrigin;
if (crossOrigin) img.crossOrigin = crossOrigin;
img.onload = () => {
self.setCurrentSrc(self.src);
self.setDownloaded(true);
self.setProgress(1);
self.setDownloading(false);
self.setImageLoaded(true);
resolve();
};
img.onerror = () => {
self.setError(true);
self.setDownloading(false);
resolve();
};
img.src = self.src;
});
return;
}

self.setDownloading(true);
fileLoader
.download(self.src, (_t, _l, progress) => {
self.setProgress(progress);
Expand All @@ -87,6 +121,8 @@ export const ImageEntity = types
},

ensurePreloaded() {
if (isFF(FF_IMAGE_MEMORY_USAGE)) return self.currentSrc !== undefined;

if (fileLoader.isError(self.src)) {
self.setDownloading(false);
self.setError(true);
Expand Down
2 changes: 2 additions & 0 deletions web/libs/editor/src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export const FF_LEAP_1173 = "fflag_feat_front_leap_1173_disable_postpone_skip_sh

export const FF_PER_FIELD_COMMENTS = "fflag_feat_all_leap_1430_per_field_comments_100924_short";

export const FF_IMAGE_MEMORY_USAGE = "fflag_feat_front_optic_1479_improve_image_tag_memory_usage_short";

Object.assign(window, {
APP_SETTINGS: {
...(window.APP_SETTINGS ?? {}),
Expand Down
Loading