Skip to content

Commit 75a49d2

Browse files
committed
Added hasCacheConflict and addToCache methods/
1 parent 1b25755 commit 75a49d2

1 file changed

Lines changed: 54 additions & 18 deletions

File tree

src/loader/File.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,33 @@ var XHRSettings = require('./XHRSettings');
4040
* @constructor
4141
* @since 3.0.0
4242
*
43+
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that is going to load this File.
4344
* @param {FileConfig} fileConfig - [description]
4445
*/
4546
var File = new Class({
4647

4748
initialize:
4849

49-
function File (fileConfig)
50+
function File (loader, fileConfig)
5051
{
52+
/**
53+
* A reference to the Loader that is going to load this file.
54+
*
55+
* @name Phaser.Loader.File#loader
56+
* @type {Phaser.Loader.LoaderPlugin}
57+
* @since 3.0.0
58+
*/
59+
this.loader = loader;
60+
61+
/**
62+
* A reference to the Cache, or Texture Manager, that is going to store this file if it loads.
63+
*
64+
* @name Phaser.Loader.File#cache
65+
* @type {(Phaser.Cache.BaseCache|Phaser.Textures.TextureManager)}
66+
* @since 3.7.0
67+
*/
68+
this.cache = GetFastValue(fileConfig, 'cache');
69+
5170
/**
5271
* The file type string (image, json, etc) for sorting within the Loader.
5372
*
@@ -112,15 +131,6 @@ var File = new Class({
112131
this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', {}));
113132
}
114133

115-
/**
116-
* The LoaderPlugin instance that is loading this file.
117-
*
118-
* @name Phaser.Loader.File#loader
119-
* @type {?Phaser.Loader.LoaderPlugin}
120-
* @since 3.0.0
121-
*/
122-
this.loader = null;
123-
124134
/**
125135
* The XMLHttpRequest instance (as created by XHR Loader) that is loading this File.
126136
*
@@ -275,30 +285,26 @@ var File = new Class({
275285
*
276286
* @method Phaser.Loader.File#load
277287
* @since 3.0.0
278-
*
279-
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that will load this File.
280288
*/
281-
load: function (loader)
289+
load: function ()
282290
{
283-
this.loader = loader;
284-
285291
if (this.state === CONST.FILE_POPULATED)
286292
{
287293
this.onComplete();
288294

289-
loader.nextFile(this);
295+
this.loader.nextFile(this);
290296
}
291297
else
292298
{
293-
this.src = GetURL(this, loader.baseURL);
299+
this.src = GetURL(this, this.loader.baseURL);
294300

295301
if (this.src.indexOf('data:') === 0)
296302
{
297303
console.warn('Local data URIs are not supported: ' + this.key);
298304
}
299305
else
300306
{
301-
this.xhrLoader = XHRLoader(this, loader.xhr);
307+
this.xhrLoader = XHRLoader(this, this.loader.xhr);
302308
}
303309
}
304310
},
@@ -407,6 +413,36 @@ var File = new Class({
407413
{
408414
this.state = CONST.FILE_COMPLETE;
409415
}
416+
},
417+
418+
/**
419+
* Checks if a key matching the one used by this file exists in the target Cache or not.
420+
* This is called automatically by the LoaderPlugin to decide if the file can be safely
421+
* loaded or will conflict.
422+
*
423+
* @method Phaser.Loader.File#hasCacheConflict
424+
* @since 3.7.0
425+
*
426+
* @return {boolean} `true` if adding this file will cause a conflict, otherwise `false`.
427+
*/
428+
hasCacheConflict: function ()
429+
{
430+
return (this.cache.exists(this.key));
431+
},
432+
433+
/**
434+
* Adds this file to its target cache upon successful loading and processing.
435+
* It will emit a `filecomplete` event from the LoaderPlugin.
436+
* This method is often overridden by specific file types.
437+
*
438+
* @method Phaser.Loader.File#addToCache
439+
* @since 3.7.0
440+
*/
441+
addToCache: function ()
442+
{
443+
this.cache.add(this.key, this.data);
444+
445+
this.loader.emit('filecomplete', this.key, this);
410446
}
411447

412448
});

0 commit comments

Comments
 (0)