Added start of the new Loader.

This commit is contained in:
Richard Davey 2016-11-30 00:18:34 +00:00
parent e34d53a969
commit 104d7a1341
5 changed files with 140 additions and 0 deletions

41
v3/src/loader/Loader.js Normal file
View file

@ -0,0 +1,41 @@
var Set = require('../structs/Set');
var XHRSettings = require('./XHRSettings');
var Loader = function ()
{
// Move to a 'setURL' method
this.baseURL = '';
this.path = '';
this.tag = '';
this.enableParallel = true;
this.maxParallelDownloads = 4;
// xhr specific global settings (can be overridden on a per-file basis)
this.xhr = XHRSettings();
this.crossOrigin = undefined;
this.list = new Set();
this.inflight = new Set();
this.failed = new Set();
this.queue = new Set();
this.storage = new Set();
this._state = 'PENDING';
};
Loader.prototype.contructor = Loader;
Loader.prototype = {
add: function ()
{
}
};
module.exports = Loader;

View file

@ -0,0 +1,27 @@
var XHRSettings = require('./XHRSettings');
// Takes two XHR Objects and creates a new object
// The new object is based on global initially, but any setting in
// local overrides the global value.
var MergeXHRSettings = function (global, local)
{
var output = (global === undefined) ? XHRSettings() : Object.assign(global);
if (local)
{
for (var setting in local)
{
if (local[setting] !== undefined)
{
output[setting] = local[setting];
}
}
}
return output;
};
module.exports = MergeXHRSettings;

View file

@ -0,0 +1,34 @@
var MergeXHRSettings = require('./MergeXHRSettings');
var XHRLoader = function (file, globalXHRSettings)
{
var config = MergeXHRSettings(globalXHRSettings, file.xhr);
var xhr = new XMLHttpRequest();
xhr.open('GET', file.src, config.async, config.user, config.password);
xhr.responseType = file.xhr.responseType;
xhr.timeout = config.timeout;
if (config.header && config.headerValue)
{
xhr.setRequestHeader(config.header, config.headerValue);
}
if (config.overrideMimeType)
{
xhr.overrideMimeType(config.overrideMimeType);
}
// After a successful request, the xhr.response property will contain the requested data as a DOMString, ArrayBuffer, Blob, or Document (depending on what was set for responseType.)
xhr.onload = file.onLoad(xhr);
xhr.onerror = file.onError(xhr);
xhr.send();
return xhr;
};
module.exports = XHRLoader;

View file

@ -0,0 +1,38 @@
// Creates an XHRSettings Object with default values
var XHRSettings = function (responseType, async, user, password, timeout)
{
if (responseType === undefined) { responseType = ''; }
if (async === undefined) { async = true; }
if (user === undefined) { user = ''; }
if (password === undefined) { password = ''; }
if (timeout === undefined) { timeout = 0; }
// Before sending a request, set the xhr.responseType to "text", "arraybuffer", "blob", or "document", depending on your data needs. Note, setting xhr.responseType = '' (or omitting) will default the response to "text".
return {
// Ignored by the Loader, only used by File.
responseType: responseType,
async: async,
// credentials
user: user,
password: password,
// timeout in ms (0 = no timeout)
timeout: timeout,
// setRequestHeader
header: undefined,
headerValue: undefined,
// overrideMimeType
overrideMimeType: undefined
};
};
module.exports = XHRSettings;

View file