Skip to content

Commit 609d77f

Browse files
committed
Loader - added means to add synchronization points
- Added `withSyncPoint` and `addSyncPoint` methods to allow explicit adding of synchronization points (synchronization points are explained in `withSyncPoint`. - Changed the file/asset `sync` attribute to `syncPoint` to reflect terminology.
1 parent a6116b3 commit 609d77f

1 file changed

Lines changed: 58 additions & 4 deletions

File tree

src/loader/Loader.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ Phaser.Loader = function (game) {
131131
*/
132132
this.maxParallelDownloads = 5;
133133

134+
/**
135+
* A counter: if more than zero files will be automatically added as a synchronization point.
136+
* @property {integer} _withSyncPointDepth;
137+
*/
138+
this._withSyncPointDepth = 0;
139+
134140
/**
135141
* Contains all the information for asset files (including packs) to load.
136142
*
@@ -395,6 +401,7 @@ Phaser.Loader.prototype = {
395401
type: type,
396402
key: key,
397403
url: url,
404+
syncPoint: this._withSyncPointDepth > 0,
398405
data: null,
399406
loading: false,
400407
loaded: false,
@@ -477,9 +484,9 @@ Phaser.Loader.prototype = {
477484

478485
var pack = {
479486
type: 'packfile',
480-
sync: true,
481487
key: key,
482488
url: url,
489+
syncPoint: true,
483490
data: null,
484491
loading: false,
485492
loaded: false,
@@ -613,7 +620,7 @@ Phaser.Loader.prototype = {
613620
// Why is the default callback context the ..callback?
614621
if (callback !== false && typeof callbackContext === 'undefined') { callbackContext = callback; }
615622

616-
this.addToFileList('script', key, url, { sync: true, callback: callback, callbackContext: callbackContext });
623+
this.addToFileList('script', key, url, { syncPoint: true, callback: callback, callbackContext: callbackContext });
617624

618625
return this;
619626

@@ -962,6 +969,53 @@ Phaser.Loader.prototype = {
962969

963970
},
964971

972+
/**
973+
* Add a synchronization point to the assets/files added within the supplied callback.
974+
*
975+
* A synchronization point denotes that an asset _must_ be completely loaded before
976+
* subsequent assets can be loaded. An asset marked as a sync-point does not need to wait
977+
* for previous assets to load (unless they are sync-points). Resources, such as packs, may still
978+
* be downloaded around sync-points, as long as they do not finalize loading.
979+
*
980+
* @method Phader.Loader#withSyncPoints
981+
* @param {function} callback - The callback is invoked and is supplied with a single argument: the loader.
982+
* @param {object} [callbackContext=(loader)] - Context for the callback.
983+
* @return {Phaser.Loader} This Loader instance.
984+
*/
985+
withSyncPoint: function (callback, callbackContext) {
986+
987+
this._withSyncPointDepth++;
988+
try {
989+
callback.call(callbackContext || this, this);
990+
} finally {
991+
this._withSyncPointDepth--;
992+
}
993+
994+
return this;
995+
},
996+
997+
/**
998+
* Add a synchronization point to a specific file/asset in the load queue.
999+
*
1000+
* This has no effect on already loaded assets.
1001+
*
1002+
* @method Phader.Loader#withSyncPoints
1003+
* @param {function} callback - The callback is invoked and is supplied with a single argument: the loader.
1004+
* @param {object} [callbackContext=(loader)] - Context for the callback.
1005+
* @return {Phaser.Loader} This Loader instance.
1006+
* @see {@link Phaser.Loader#withSyncPoint withSyncPoint}
1007+
*/
1008+
addSyncPoint: function (type, key) {
1009+
1010+
var asset = this.getAsset(type, key);
1011+
if (asset)
1012+
{
1013+
asset.file.syncPoint = true;
1014+
}
1015+
1016+
return this;
1017+
},
1018+
9651019
/**
9661020
* Remove a file/asset from the loading queue.
9671021
*
@@ -1138,7 +1192,7 @@ Phaser.Loader.prototype = {
11381192
}
11391193
}
11401194

1141-
if (!file.loaded && file.sync)
1195+
if (!file.loaded && file.syncPoint)
11421196
{
11431197
syncblock = true;
11441198
}
@@ -1316,7 +1370,7 @@ Phaser.Loader.prototype = {
13161370
* @method Phaser.Loader#transformUrl
13171371
* @protected
13181372
*/
1319-
transformUrl: function (url, file) {
1373+
transformUrl: function (url /*, file */) {
13201374
return this.baseURL + url;
13211375
},
13221376

0 commit comments

Comments
 (0)