Skip to content

Commit 17092eb

Browse files
committed
More Loader updates, lots to do though.
1 parent 104d7a1 commit 17092eb

4 files changed

Lines changed: 217 additions & 7 deletions

File tree

v3/src/loader/File.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import XHRLoader from 'loader/XHRLoader.js';
2+
import XHRSettings from 'loader/XHRSettings.js';
3+
4+
// File level consts
5+
6+
export const PENDING = 0; // file is in the load queue but not yet started
7+
export const LOADING = 1; // file has been started to load by the loader (onLoad called)
8+
export const LOADED = 2; // file has loaded successfully, awaiting processing
9+
export const FAILED = 3; // file failed to load
10+
export const PROCESSING = 4; // file is being processed (onProcess callback)
11+
export const COMPLETE = 5; // file has finished processing
12+
export const DESTROYED = 6; // file has been destroyed
13+
14+
// Our base File object (from which all File Types extend)
15+
16+
// key = user level file key (can be filename or other string based value)
17+
// url = the URL to load the file from, doesn't include baseURL or Path (which are both set by the Loader)
18+
// type = a user-level value that can control which cache the file is added to
19+
20+
var File = function (key, url, type)
21+
{
22+
if (!key)
23+
{
24+
console.warn('Invalid File key');
25+
return;
26+
}
27+
28+
return {
29+
30+
key: key,
31+
32+
url: url,
33+
34+
// Both of these are overridden by the BaseLoader (if being used)
35+
path: '',
36+
src: url,
37+
38+
tag: '', // Tag this file, this is a non-unique string. For example you could tag a collection of files as 'level1', or 'mainmenu'.
39+
40+
type: type, // the file type, i.e. 'image', 'json', etc which can be used to control which cache it gets added to
41+
42+
state: PENDING,
43+
44+
// Multipart file? (i.e. an atlas and its json)
45+
multipart: undefined,
46+
linkFile: undefined,
47+
48+
// The actual processed file data
49+
data: undefined,
50+
51+
// For CORs based loading.
52+
// If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
53+
crossOrigin: undefined,
54+
55+
// Optionally set by the Promise returned from BaseLoader.addFile.
56+
resolve: undefined,
57+
reject: undefined,
58+
59+
// maybe you have to set it in the Promise?
60+
processCallback: undefined,
61+
62+
// xhr specific settings (ignored by TagLoaded files)
63+
xhr: XHRSettings('text'),
64+
65+
onStateChange: function (value) {
66+
67+
// console.log('onStateChange', this.url, 'from', this.state, 'to', value);
68+
69+
if (this.state !== value)
70+
{
71+
this.state = value;
72+
73+
// Loaded AND Processed
74+
if (value === COMPLETE)
75+
{
76+
// Part of a multipart load?
77+
if (this.multipart)
78+
{
79+
// Has the linked file loaded too?
80+
if (this.linkFile.state === COMPLETE && this.multipart.resolve)
81+
{
82+
// Send the Promise for the multipart file
83+
this.multipart.resolve(this.multipart);
84+
}
85+
}
86+
87+
// Send the Promise for this file
88+
if (this.resolve)
89+
{
90+
this.resolve(this);
91+
}
92+
}
93+
else if (value === FAILED)
94+
{
95+
// Part of a multipart load?
96+
if (this.multipart)
97+
{
98+
if (this.multipart.reject)
99+
{
100+
// Send the Promise for the multipart file
101+
this.multipart.reject(this.multipart, error);
102+
}
103+
}
104+
105+
// Send the Promise for this file
106+
if (this.reject)
107+
{
108+
this.reject(this);
109+
}
110+
}
111+
}
112+
113+
},
114+
115+
// These functions are usually overridden by the custom file types
116+
117+
load: function (globalXHRSettings) {
118+
119+
this.onStateChange(LOADING);
120+
121+
// Returns a Promise from the XHRLoader
122+
return XHRLoader(this, globalXHRSettings);
123+
124+
},
125+
126+
onLoad: function () {
127+
128+
// If overridden it must set `state` to LOADED
129+
this.onStateChange(LOADED);
130+
131+
},
132+
133+
onError: function () {
134+
135+
// If overridden it must set `state` to FAILED
136+
this.onStateChange(FAILED);
137+
138+
},
139+
140+
onProcess: function () {
141+
142+
// If overridden it must set `state` to PROCESSING
143+
this.onStateChange(PROCESSING);
144+
145+
},
146+
147+
onComplete: function () {
148+
149+
// If overridden it must set `state` to COMPLETE
150+
this.onStateChange(COMPLETE);
151+
152+
},
153+
154+
onDestroy: function () {
155+
156+
// If overridden it must set `state` to DESTROYED
157+
this.onStateChange(DESTROYED);
158+
159+
}
160+
161+
};
162+
163+
};
164+
165+
module.exports = File;

v3/src/loader/Loader.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
var Set = require('../structs/Set');
22
var XHRSettings = require('./XHRSettings');
3+
// var ImageLoader = require('./filetypes/Image');
34

45
var Loader = function ()
56
{
6-
// Move to a 'setURL' method
7+
// Move to a 'setURL' method?
78
this.baseURL = '';
89
this.path = '';
910

10-
this.tag = '';
11-
11+
// Read from Game Config
1212
this.enableParallel = true;
13-
14-
this.maxParallelDownloads = 4;
13+
this.maxParallelDownloads = 8;
1514

1615
// xhr specific global settings (can be overridden on a per-file basis)
1716
this.xhr = XHRSettings();
@@ -31,9 +30,23 @@ Loader.prototype.contructor = Loader;
3130

3231
Loader.prototype = {
3332

34-
add: function ()
33+
// The File Loaders
34+
35+
// Add a File direct to the queue. Must extend File base object.
36+
add: function (file)
37+
{
38+
39+
},
40+
41+
// Different images based on device-pixel ratio
42+
// And maybe on screen resolution breakpoints
43+
44+
image: function (key, url)
3545
{
36-
46+
47+
48+
49+
// return ImageLoader(this, key, url);
3750
}
3851

3952
};

v3/src/loader/filetypes/Image.js

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// var File = require('../File');
2+
3+
var onLoad = function ()
4+
{
5+
6+
};
7+
8+
var ImageFile = function (loader, key, url)
9+
{
10+
if (!key)
11+
{
12+
console.warn('Loader: You tried to load an Image, but no key was given');
13+
return;
14+
}
15+
16+
if (!url)
17+
{
18+
url = key + '.png';
19+
}
20+
21+
var file = {
22+
type: 'image',
23+
key: key,
24+
url: url,
25+
onload: onLoad
26+
};
27+
28+
loader.add(file);
29+
30+
};
31+
32+
module.exports = ImageFile;

0 commit comments

Comments
 (0)