Socket
Socket
Sign inDemoInstall

resource-loader

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resource-loader - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

2

package.json
{
"name": "resource-loader",
"version": "1.1.1",
"version": "1.1.2",
"main": "./src/index.js",

@@ -5,0 +5,0 @@ "description": "A generic asset loader, made with web games in mind.",

@@ -10,6 +10,9 @@ var async = require('async'),

* @param baseUrl {string} The base url for all resources loaded by this loader.
* @param [concurrency=10] {number} The number of resources to load concurrently.
*/
function Loader(baseUrl) {
function Loader(baseUrl, concurrency) {
EventEmitter2.call(this);
concurrency = concurrency || 10;
/**

@@ -23,9 +26,2 @@ * The base url for all resources loaded by this loader.

/**
* The resources waiting to be loaded.
*
* @member {Resource[]}
*/
this.queue = [];
/**
* The progress percent of the loader going through the queue.

@@ -82,2 +78,24 @@ *

/**
* The resource buffer that fills until `load` is called to start loading resources.
*
* @private
* @member {Resource[]}
*/
this._buffer = [];
/**
* The resources waiting to be loaded.
*
* @member {Resource[]}
*/
this.queue = async.queue(this._boundLoadResource, concurrency);
/**
* All the resources for this loader keyed by name.
*
* @member {object<string, Resource>}
*/
this.resources = {};
/**
* Emitted once per loaded or errored resource.

@@ -128,13 +146,33 @@ *

* loaded be interpreted when using XHR?
* @param [callback] {function} Function to call when this specific resource completes loading.
* @return {Loader}
*/
Loader.prototype.add = Loader.prototype.enqueue = function (name, url, options) {
var resource = new Resource(name, this.baseUrl + url, options);
Loader.prototype.add = Loader.prototype.enqueue = function (name, url, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
this.queue.push(resource);
if (this.resources[name]) {
throw new Error('Resource with name "' + name + '" already exists.');
}
if (this.loading) {
this.loadResource(resource);
// create the store the resource
this.resources[name] = new Resource(name, this.baseUrl + url, options);
if (typeof cb === 'function') {
this.resources[name].once('afterMiddleware', cb);
}
// if already loading add it to the worker queue
if (this.queue.started) {
this.queue.push(this.resources[name]);
this._progressChunk = (100 - this.progress) / (this.queue.length() + this.queue.running());
}
// otherwise buffer it to be added to the queue later
else {
this._buffer.push(this.resources[name]);
this._progressChunk = 100 / this._buffer.length;
}
return this;

@@ -178,4 +216,9 @@ };

Loader.prototype.reset = function () {
this.queue.length = 0;
this._buffer.length = 0;
this.queue.kill();
this.queue.started = false;
this.progress = 0;
this._progressChunk = 0;
this.loading = false;

@@ -188,11 +231,7 @@ };

* @fires start
* @param [parallel=true] {boolean} Should the queue be downloaded in parallel?
* @param [callback] {function} Optional callback that will be bound to the `complete` event.
* @return {Loader}
*/
Loader.prototype.load = function (parallel, cb) {
if (typeof parallel === 'function') {
cb = parallel;
}
Loader.prototype.load = function (cb) {
// register complete callback if they pass one
if (typeof cb === 'function') {

@@ -202,14 +241,21 @@ this.once('complete', cb);

this._progressChunk = 100 / this.queue.length;
// if the queue has already started we are done here
if (this.queue.started) {
return this;
}
this.emit('start');
// set drain event callback
this.queue.drain = this._boundOnComplete;
// only disable parallel if they explicitly pass `false`
if (parallel !== false) {
async.each(this.queue, this._boundLoadResource, this._boundOnComplete);
// notify of start
this.emit('start', this);
// start the internal queue
for (var i = 0; i < this._buffer.length; ++i) {
this.queue.push(this._buffer[i]);
}
else {
async.eachSeries(this.queue, this._boundLoadResource, this._boundOnComplete);
}
// empty the buffer
this._buffer.length = 0;
return this;

@@ -223,8 +269,8 @@ };

*/
Loader.prototype.loadResource = function (resource, next) {
Loader.prototype.loadResource = function (resource, cb) {
var self = this;
this._runMiddleware(resource, this._beforeMiddleware, function () {
resource.on('progress', self.emit.bind(self, 'progress'));
resource.on('complete', self._onLoad.bind(self, resource, next));
// resource.on('progress', self.emit.bind(self, 'progress'));
resource.once('complete', self._onLoad.bind(self, resource, cb));

@@ -242,3 +288,3 @@ resource.load();

Loader.prototype._onComplete = function () {
this.emit('complete', this.queue.reduce(_mapQueue, {}));
this.emit('complete', this, this.resources);
};

@@ -260,15 +306,19 @@

*/
Loader.prototype._onLoad = function (resource, next) {
Loader.prototype._onLoad = function (resource, cb) {
this.progress += this._progressChunk;
this.emit('progress', resource);
this.emit('progress', this, resource);
if (resource.error) {
this.emit('error', resource.error, resource);
this.emit('error', resource.error, this, resource);
}
else {
this.emit('load', resource);
this.emit('load', this, resource);
}
this._runMiddleware(resource, this._afterMiddleware, next);
this._runMiddleware(resource, this._afterMiddleware, function () {
resource.emit('afterMiddleware', resource);
cb && cb();
});
};

@@ -275,0 +325,0 @@

@@ -340,3 +340,3 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2;

if (event.lengthComputable) {
this.emit('progress', event.loaded / event.total);
this.emit('progress', this, event.loaded / event.total);
}

@@ -343,0 +343,0 @@ };

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc