1
- import { types } from "mobx-state-tree" ;
1
+ import { types , getParent } from "mobx-state-tree" ;
2
2
import { FileLoader } from "../../../utils/FileLoader" ;
3
3
import { clamp } from "../../../utils/utilities" ;
4
+ import { FF_IMAGE_MEMORY_USAGE , isFF } from "../../../utils/feature-flags" ;
4
5
5
6
const fileLoader = new FileLoader ( ) ;
6
7
@@ -65,12 +66,45 @@ export const ImageEntity = types
65
66
/** Is image loaded using `<img/>` tag and cached by the browser */
66
67
imageLoaded : false ,
67
68
} ) )
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
+ } ) )
68
78
. actions ( ( self ) => ( {
69
79
preload ( ) {
70
80
if ( self . ensurePreloaded ( ) || ! self . src ) return ;
71
81
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
+ }
73
106
107
+ self . setDownloading ( true ) ;
74
108
fileLoader
75
109
. download ( self . src , ( _t , _l , progress ) => {
76
110
self . setProgress ( progress ) ;
@@ -87,6 +121,8 @@ export const ImageEntity = types
87
121
} ,
88
122
89
123
ensurePreloaded ( ) {
124
+ if ( isFF ( FF_IMAGE_MEMORY_USAGE ) ) return self . currentSrc !== undefined ;
125
+
90
126
if ( fileLoader . isError ( self . src ) ) {
91
127
self . setDownloading ( false ) ;
92
128
self . setError ( true ) ;
0 commit comments