Skip to content

Commit a0c1d12

Browse files
moved url generating logic into File class static methods
applied it to image and svg file classes fixed issue with unnecessary calls to revokeObjectURL method in SVGFile class
1 parent a7cf674 commit a0c1d12

3 files changed

Lines changed: 40 additions & 37 deletions

File tree

v3/src/loader/File.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,34 @@ var File = new Class({
162162

163163
});
164164

165+
File.createObjectURL = function (data, response, defaultType)
166+
{
167+
if(URL)
168+
{
169+
data.src = URL.createObjectURL(response);
170+
}
171+
else
172+
{
173+
var reader = new FileReader();
174+
175+
reader.onload = function()
176+
{
177+
delete data.crossOrigin;
178+
data.src = 'data:' + (response.type || defaultType) + ';base64,' + reader.result.split(',')[1];
179+
};
180+
181+
reader.onerror = data.onerror;
182+
183+
reader.readAsDataURL(response);
184+
}
185+
};
186+
187+
File.revokeObjectURL = function (data)
188+
{
189+
if(URL)
190+
{
191+
URL.revokeObjectURL(data.src);
192+
}
193+
};
194+
165195
module.exports = File;

v3/src/loader/filetypes/ImageFile.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ var ImageFile = new Class({
5656

5757
this.data.onload = function ()
5858
{
59-
if(URL)
60-
{
61-
URL.revokeObjectURL(_this.data.src);
62-
}
59+
File.revokeObjectURL(_this.data);
6360

6461
_this.onComplete();
6562

@@ -68,39 +65,14 @@ var ImageFile = new Class({
6865

6966
this.data.onerror = function ()
7067
{
71-
if(URL)
72-
{
73-
URL.revokeObjectURL(_this.data.src);
74-
}
68+
File.revokeObjectURL(_this.data);
7569

7670
_this.state = CONST.FILE_ERRORED;
7771

7872
callback(_this);
7973
};
8074

81-
if(URL)
82-
{
83-
this.data.src = URL.createObjectURL(this.xhrLoader.response);
84-
}
85-
else
86-
{
87-
var reader = new FileReader();
88-
89-
reader.onload = function()
90-
{
91-
delete _this.data.crossOrigin;
92-
_this.data.src = 'data:' + (_this.xhrLoader.response.type || 'image/png') + ';base64,' + reader.result.split(',')[1];
93-
};
94-
95-
reader.onerror = function ()
96-
{
97-
_this.state = CONST.FILE_ERRORED;
98-
99-
callback(_this);
100-
};
101-
102-
reader.readAsDataURL(this.xhrLoader.response);
103-
}
75+
File.createObjectURL(this.data, this.xhrLoader.response, 'image/png');
10476

10577
}
10678

v3/src/loader/filetypes/SVGFile.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ var SVGFile = new Class({
5656

5757
this.data.onload = function ()
5858
{
59-
URL.revokeObjectURL(_this.data.src);
59+
if(!retry)
60+
{
61+
File.revokeObjectURL(_this.data);
62+
}
6063

6164
_this.onComplete();
6265

@@ -65,16 +68,14 @@ var SVGFile = new Class({
6568

6669
this.data.onerror = function ()
6770
{
68-
URL.revokeObjectURL(_this.data.src);
69-
7071
// Safari 8 re-try
7172
if (!retry)
7273
{
7374
retry = true;
7475

75-
var url = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''));
76+
File.revokeObjectURL(_this.data);
7677

77-
_this.data.src = URL.createObjectURL(url);
78+
_this.data.src = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''));
7879
}
7980
else
8081
{
@@ -84,7 +85,7 @@ var SVGFile = new Class({
8485
}
8586
};
8687

87-
this.data.src = URL.createObjectURL(blob);
88+
File.createObjectURL(this.data, blob, 'image/svg+xml');
8889
}
8990

9091
});

0 commit comments

Comments
 (0)