Skip to content
Browse files

JSHint fixes

  • Loading branch information...
1 parent 8f1c63a commit a7c88855d075eeef556fd72aa42f2f25dffcb866 @joelfillmore joelfillmore committed Feb 13, 2013
Showing with 324 additions and 310 deletions.
  1. +313 −293 PxLoader.js
  2. +3 −5 PxLoaderImage.js
  3. +3 −5 PxLoaderSound.js
  4. +5 −7 PxLoaderVideo.js
View
606 PxLoader.js
@@ -1,353 +1,369 @@
-/**
- * PixelLab Resource Loader
- * Loads resources while providing progress updates.
- */
+/*global define: true */
-function PxLoader(settings) {
+(function(global) {
- // merge settings with defaults
- settings = settings || {};
- this.settings = settings;
+ /*
+ * PixelLab Resource Loader
+ * Loads resources while providing progress updates.
+ */
+ function PxLoader(settings) {
- // how frequently we poll resources for progress
- if (settings.statusInterval == null) {
- settings.statusInterval = 5000; // every 5 seconds by default
- }
+ // merge settings with defaults
+ settings = settings || {};
+ this.settings = settings;
- // delay before logging since last progress change
- if (settings.loggingDelay == null) {
- settings.loggingDelay = 20 * 1000; // log stragglers after 20 secs
- }
+ // how frequently we poll resources for progress
+ if (settings.statusInterval == null) {
+ settings.statusInterval = 5000; // every 5 seconds by default
+ }
- // stop waiting if no progress has been made in the moving time window
- if (settings.noProgressTimeout == null) {
- settings.noProgressTimeout = Infinity; // do not stop waiting by default
- }
+ // delay before logging since last progress change
+ if (settings.loggingDelay == null) {
+ settings.loggingDelay = 20 * 1000; // log stragglers after 20 secs
+ }
- var entries = [],
- // holds resources to be loaded with their status
- progressListeners = [],
- timeStarted, progressChanged = Date.now();
+ // stop waiting if no progress has been made in the moving time window
+ if (settings.noProgressTimeout == null) {
+ settings.noProgressTimeout = Infinity; // do not stop waiting by default
+ }
- /**
- * The status of a resource
- * @enum {number}
- */
- var ResourceState = {
- QUEUED: 0,
- WAITING: 1,
- LOADED: 2,
- ERROR: 3,
- TIMEOUT: 4
- };
+ var entries = [],
+ // holds resources to be loaded with their status
+ progressListeners = [],
+ timeStarted, progressChanged = Date.now();
+
+ /**
+ * The status of a resource
+ * @enum {number}
+ */
+ var ResourceState = {
+ QUEUED: 0,
+ WAITING: 1,
+ LOADED: 2,
+ ERROR: 3,
+ TIMEOUT: 4
+ };
- // places non-array values into an array.
- var ensureArray = function(val) {
- if (val == null) {
- return [];
- }
+ // places non-array values into an array.
+ var ensureArray = function(val) {
+ if (val == null) {
+ return [];
+ }
- if (Array.isArray(val)) {
- return val;
- }
+ if (Array.isArray(val)) {
+ return val;
+ }
- return [val];
- };
+ return [val];
+ };
- // add an entry to the list of resources to be loaded
- this.add = function(resource) {
+ // add an entry to the list of resources to be loaded
+ this.add = function(resource) {
- // ensure tags are in an object
- resource.tags = new PxLoaderTags(resource.tags);
+ // ensure tags are in an object
+ resource.tags = new PxLoaderTags(resource.tags);
- // ensure priority is set
- if (resource.priority == null) {
- resource.priority = Infinity;
- }
+ // ensure priority is set
+ if (resource.priority == null) {
+ resource.priority = Infinity;
+ }
- entries.push({
- resource: resource,
- status: ResourceState.QUEUED
- });
- };
+ entries.push({
+ resource: resource,
+ status: ResourceState.QUEUED
+ });
+ };
- this.addProgressListener = function(callback, tags) {
- progressListeners.push({
- callback: callback,
- tags: new PxLoaderTags(tags)
- });
- };
+ this.addProgressListener = function(callback, tags) {
+ progressListeners.push({
+ callback: callback,
+ tags: new PxLoaderTags(tags)
+ });
+ };
- this.addCompletionListener = function(callback, tags) {
- progressListeners.push({
- tags: new PxLoaderTags(tags),
- callback: function(e) {
- if (e.completedCount === e.totalCount) {
- callback(e);
+ this.addCompletionListener = function(callback, tags) {
+ progressListeners.push({
+ tags: new PxLoaderTags(tags),
+ callback: function(e) {
+ if (e.completedCount === e.totalCount) {
+ callback(e);
+ }
}
- }
- });
- };
+ });
+ };
- // creates a comparison function for resources
- var getResourceSort = function(orderedTags) {
-
- // helper to get the top tag's order for a resource
- orderedTags = ensureArray(orderedTags);
- var getTagOrder = function(entry) {
- var resource = entry.resource,
- bestIndex = Infinity;
- for (var i = 0; i < resource.tags.length; i++) {
- for (var j = 0; j < Math.min(orderedTags.length, bestIndex); j++) {
- if (resource.tags[i] == orderedTags[j] && j < bestIndex) {
- bestIndex = j;
- if (bestIndex === 0) break;
+ // creates a comparison function for resources
+ var getResourceSort = function(orderedTags) {
+
+ // helper to get the top tag's order for a resource
+ orderedTags = ensureArray(orderedTags);
+ var getTagOrder = function(entry) {
+ var resource = entry.resource,
+ bestIndex = Infinity;
+ for (var i = 0; i < resource.tags.length; i++) {
+ for (var j = 0; j < Math.min(orderedTags.length, bestIndex); j++) {
+ if (resource.tags[i] === orderedTags[j] && j < bestIndex) {
+ bestIndex = j;
+ if (bestIndex === 0) {
+ break;
+ }
+ }
+ if (bestIndex === 0) {
+ break;
+ }
}
- if (bestIndex === 0) break;
}
- }
- return bestIndex;
+ return bestIndex;
+ };
+ return function(a, b) {
+ // check tag order first
+ var aOrder = getTagOrder(a),
+ bOrder = getTagOrder(b);
+ if (aOrder < bOrder) { return -1; }
+ if (aOrder > bOrder) { return 1; }
+
+ // now check priority
+ if (a.priority < b.priority) { return -1; }
+ if (a.priority > b.priority) { return 1; }
+ return 0;
+ };
};
- return function(a, b) {
- // check tag order first
- var aOrder = getTagOrder(a),
- bOrder = getTagOrder(b);
- if (aOrder < bOrder) return -1;
- if (aOrder > bOrder) return 1;
-
- // now check priority
- if (a.priority < b.priority) return -1;
- if (a.priority > b.priority) return 1;
- return 0;
- }
- };
- this.start = function(orderedTags) {
- timeStarted = Date.now();
+ this.start = function(orderedTags) {
+ timeStarted = Date.now();
- // first order the resources
- var compareResources = getResourceSort(orderedTags);
- entries.sort(compareResources);
+ // first order the resources
+ var compareResources = getResourceSort(orderedTags);
+ entries.sort(compareResources);
- // trigger requests for each resource
- for (var i = 0, len = entries.length; i < len; i++) {
- var entry = entries[i];
- entry.status = ResourceState.WAITING;
- entry.resource.start(this);
- }
+ // trigger requests for each resource
+ for (var i = 0, len = entries.length; i < len; i++) {
+ var entry = entries[i];
+ entry.status = ResourceState.WAITING;
+ entry.resource.start(this);
+ }
- // do an initial status check soon since items may be loaded from the cache
- setTimeout(statusCheck, 100);
- };
+ // do an initial status check soon since items may be loaded from the cache
+ setTimeout(statusCheck, 100);
+ };
- var statusCheck = function() {
- var checkAgain = false,
- noProgressTime = Date.now() - progressChanged,
- timedOut = (noProgressTime >= settings.noProgressTimeout),
- shouldLog = (noProgressTime >= settings.loggingDelay);
+ var statusCheck = function() {
+ var checkAgain = false,
+ noProgressTime = Date.now() - progressChanged,
+ timedOut = (noProgressTime >= settings.noProgressTimeout),
+ shouldLog = (noProgressTime >= settings.loggingDelay);
- for (var i = 0, len = entries.length; i < len; i++) {
- var entry = entries[i];
- if (entry.status !== ResourceState.WAITING) {
- continue;
- }
+ for (var i = 0, len = entries.length; i < len; i++) {
+ var entry = entries[i];
+ if (entry.status !== ResourceState.WAITING) {
+ continue;
+ }
- // see if the resource has loaded
- if (entry.resource.checkStatus) {
- entry.resource.checkStatus();
- }
+ // see if the resource has loaded
+ if (entry.resource.checkStatus) {
+ entry.resource.checkStatus();
+ }
- // if still waiting, mark as timed out or make sure we check again
- if (entry.status === ResourceState.WAITING) {
- if (timedOut) {
- entry.resource.onTimeout();
- } else {
- checkAgain = true;
+ // if still waiting, mark as timed out or make sure we check again
+ if (entry.status === ResourceState.WAITING) {
+ if (timedOut) {
+ entry.resource.onTimeout();
+ } else {
+ checkAgain = true;
+ }
}
}
- }
-
- // log any resources that are still pending
- if (shouldLog && checkAgain) {
- log();
- }
- if (checkAgain) {
- setTimeout(statusCheck, settings.statusInterval);
- }
- };
-
- this.isBusy = function() {
- for (var i = 0, len = entries.length; i < len; i++) {
- if (entries[i].status === ResourceState.QUEUED || entries[i].status === ResourceState.WAITING) {
- return true;
+ // log any resources that are still pending
+ if (shouldLog && checkAgain) {
+ log();
}
- }
- return false;
- };
- var onProgress = function(resource, statusType) {
- // find the entry for the resource
- var entry = null;
- for (var i = 0, len = entries.length; i < len; i++) {
- if (entries[i].resource === resource) {
- entry = entries[i];
- break;
+ if (checkAgain) {
+ setTimeout(statusCheck, settings.statusInterval);
}
- }
+ };
- // we have already updated the status of the resource
- if (entry == null || entry.status !== ResourceState.WAITING) {
- return;
- }
- entry.status = statusType;
- progressChanged = Date.now();
+ this.isBusy = function() {
+ for (var i = 0, len = entries.length; i < len; i++) {
+ if (entries[i].status === ResourceState.QUEUED || entries[i].status === ResourceState.WAITING) {
+ return true;
+ }
+ }
+ return false;
+ };
- var numResourceTags = resource.tags.length;
+ var onProgress = function(resource, statusType) {
+
+ var entry = null,
+ i, len, numResourceTags, listener, shouldCall;
- // fire callbacks for interested listeners
- for (var i = 0, numListeners = progressListeners.length; i < numListeners; i++) {
- var listener = progressListeners[i],
- shouldCall;
+ // find the entry for the resource
+ for (i = 0, len = entries.length; i < len; i++) {
+ if (entries[i].resource === resource) {
+ entry = entries[i];
+ break;
+ }
+ }
- if (listener.tags.length === 0) {
- // no tags specified so always tell the listener
- shouldCall = true;
- } else {
- // listener only wants to hear about certain tags
- shouldCall = resource.tags.contains(listener.tags);
+ // we have already updated the status of the resource
+ if (entry == null || entry.status !== ResourceState.WAITING) {
+ return;
}
+ entry.status = statusType;
+ progressChanged = Date.now();
+
+ numResourceTags = resource.tags.length;
+
+ // fire callbacks for interested listeners
+ for (i = 0, len = progressListeners.length; i < len; i++) {
+
+ listener = progressListeners[i];
+ if (listener.tags.length === 0) {
+ // no tags specified so always tell the listener
+ shouldCall = true;
+ } else {
+ // listener only wants to hear about certain tags
+ shouldCall = resource.tags.contains(listener.tags);
+ }
- if (shouldCall) {
- sendProgress(entry, listener);
+ if (shouldCall) {
+ sendProgress(entry, listener);
+ }
}
- }
- };
+ };
- this.onLoad = function(resource) {
- onProgress(resource, ResourceState.LOADED);
- };
- this.onError = function(resource) {
- onProgress(resource, ResourceState.ERROR);
- };
- this.onTimeout = function(resource) {
- onProgress(resource, ResourceState.TIMEOUT);
- };
+ this.onLoad = function(resource) {
+ onProgress(resource, ResourceState.LOADED);
+ };
+ this.onError = function(resource) {
+ onProgress(resource, ResourceState.ERROR);
+ };
+ this.onTimeout = function(resource) {
+ onProgress(resource, ResourceState.TIMEOUT);
+ };
- // sends a progress report to a listener
- var sendProgress = function(updatedEntry, listener) {
- // find stats for all the resources the caller is interested in
- var completed = 0,
- total = 0;
- for (var i = 0, len = entries.length; i < len; i++) {
- var entry = entries[i],
+ // sends a progress report to a listener
+ var sendProgress = function(updatedEntry, listener) {
+ // find stats for all the resources the caller is interested in
+ var completed = 0,
+ total = 0,
+ i, len, entry, includeResource;
+ for (i = 0, len = entries.length; i < len; i++) {
+
+ entry = entries[i];
includeResource = false;
- if (listener.tags.length === 0) {
- // no tags specified so always tell the listener
- includeResource = true;
- } else {
- includeResource = entry.resource.tags.contains(listener.tags);
- }
-
- if (includeResource) {
- total++;
- if (entry.status === ResourceState.LOADED || entry.status === ResourceState.ERROR || entry.status === ResourceState.TIMEOUT) {
- completed++;
+ if (listener.tags.length === 0) {
+ // no tags specified so always tell the listener
+ includeResource = true;
+ } else {
+ includeResource = entry.resource.tags.contains(listener.tags);
}
- }
- }
- listener.callback({
- // info about the resource that changed
- resource: updatedEntry.resource,
+ if (includeResource) {
+ total++;
+ if (entry.status === ResourceState.LOADED ||
+ entry.status === ResourceState.ERROR ||
+ entry.status === ResourceState.TIMEOUT) {
- // should we expose StatusType instead?
- loaded: (updatedEntry.status === ResourceState.LOADED),
- error: (updatedEntry.status === ResourceState.ERROR),
- timeout: (updatedEntry.status === ResourceState.TIMEOUT),
-
- // updated stats for all resources
- completedCount: completed,
- totalCount: total
- });
- };
-
- // prints the status of each resource to the console
- var log = this.log = function(showAll) {
- if (!window.console) {
- return;
- }
+ completed++;
+ }
+ }
+ }
- var elapsedSeconds = Math.round((Date.now() - timeStarted) / 1000);
- window.console.log('PxLoader elapsed: ' + elapsedSeconds + ' sec');
+ listener.callback({
+ // info about the resource that changed
+ resource: updatedEntry.resource,
- for (var i = 0, len = entries.length; i < len; i++) {
- var entry = entries[i];
- if (!showAll && entry.status !== ResourceState.WAITING) {
- continue;
- }
+ // should we expose StatusType instead?
+ loaded: (updatedEntry.status === ResourceState.LOADED),
+ error: (updatedEntry.status === ResourceState.ERROR),
+ timeout: (updatedEntry.status === ResourceState.TIMEOUT),
- var message = 'PxLoader: #' + i + ' ' + entry.resource.getName();
- switch(entry.status) {
- case ResourceState.QUEUED:
- message += ' (Not Started)';
- break;
- case ResourceState.WAITING:
- message += ' (Waiting)';
- break;
- case ResourceState.LOADED:
- message += ' (Loaded)';
- break;
- case ResourceState.ERROR:
- message += ' (Error)';
- break;
- case ResourceState.TIMEOUT:
- message += ' (Timeout)';
- break;
- }
+ // updated stats for all resources
+ completedCount: completed,
+ totalCount: total
+ });
+ };
- if (entry.resource.tags.length > 0) {
- message += ' Tags: [' + entry.resource.tags.array.join(',') + ']';
+ // prints the status of each resource to the console
+ var log = this.log = function(showAll) {
+ if (!window.console) {
+ return;
}
- window.console.log(message);
- }
- };
-}
+ var elapsedSeconds = Math.round((Date.now() - timeStarted) / 1000);
+ window.console.log('PxLoader elapsed: ' + elapsedSeconds + ' sec');
-// Tag object to handle tag intersection; once created not meant to be changed
-// Performance rationale: http://jsperf.com/lists-indexof-vs-in-operator/3
+ for (var i = 0, len = entries.length; i < len; i++) {
+ var entry = entries[i];
+ if (!showAll && entry.status !== ResourceState.WAITING) {
+ continue;
+ }
-function PxLoaderTags(values) {
+ var message = 'PxLoader: #' + i + ' ' + entry.resource.getName();
+ switch(entry.status) {
+ case ResourceState.QUEUED:
+ message += ' (Not Started)';
+ break;
+ case ResourceState.WAITING:
+ message += ' (Waiting)';
+ break;
+ case ResourceState.LOADED:
+ message += ' (Loaded)';
+ break;
+ case ResourceState.ERROR:
+ message += ' (Error)';
+ break;
+ case ResourceState.TIMEOUT:
+ message += ' (Timeout)';
+ break;
+ }
- this.array = [];
- this.object = {};
- this.value = null; // single value
- this.length = 0;
+ if (entry.resource.tags.length > 0) {
+ message += ' Tags: [' + entry.resource.tags.array.join(',') + ']';
+ }
- if (values !== null && values !== undefined) {
- if (Array.isArray(values)) {
- this.array = values;
- } else if (typeof values === 'object') {
- for (var key in values) {
- this.array.push(key);
+ window.console.log(message);
}
- } else {
- this.array.push(values);
- this.value = values;
- }
+ };
+ }
- this.length = this.array.length;
- // convert array values to object with truthy values, used by contains function below
- for (var i = 0; i < this.length; i++) {
- this.object[this.array[i]] = true;
+ // Tag object to handle tag intersection; once created not meant to be changed
+ // Performance rationale: http://jsperf.com/lists-indexof-vs-in-operator/3
+
+ function PxLoaderTags(values) {
+
+ this.array = [];
+ this.object = {};
+ this.value = null; // single value
+ this.length = 0;
+
+ if (values !== null && values !== undefined) {
+ if (Array.isArray(values)) {
+ this.array = values;
+ } else if (typeof values === 'object') {
+ for (var key in values) {
+ this.array.push(key);
+ }
+ } else {
+ this.array.push(values);
+ this.value = values;
+ }
+
+ this.length = this.array.length;
+
+ // convert array values to object with truthy values, used by contains function below
+ for (var i = 0; i < this.length; i++) {
+ this.object[this.array[i]] = true;
+ }
}
}
// compare this object with another; return true if they share at least one value
- this.contains = function(other) {
+ PxLoaderTags.prototype.contains = function(other) {
if (this.length === 0 || other.length === 0) {
return false;
} else if (this.length === 1 && this.value !== null) {
@@ -366,28 +382,32 @@ function PxLoaderTags(values) {
}
return false;
}
+ };
+
+ // AMD module support
+ if (typeof define === 'function' && define.amd) {
+ define('PxLoader', [], function() {
+ return PxLoader;
+ });
}
-}
-// AMD module support
-if (typeof define === 'function' && define.amd) {
- define('PxLoader', [], function() {
- return PxLoader;
- });
-}
+ // exports
+ global.PxLoader = PxLoader;
+
+}(this));
// Date.now() shim for older browsers
if (!Date.now) {
Date.now = function now() {
- return +(new Date);
+ return new Date().getTime();
};
}
// shims to ensure we have newer Array utility methods
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray) {
Array.isArray = function(arg) {
- return Object.prototype.toString.call(arg) == '[object Array]';
+ return Object.prototype.toString.call(arg) === '[object Array]';
};
}
View
8 PxLoaderImage.js
@@ -1,8 +1,6 @@
-// @depends PxLoader.js
-/**
- * PxLoader plugin to load images
- */
+/*global PxLoader: true, define: true */
+// PxLoader plugin to load images
function PxLoaderImage(url, tags, priority) {
var self = this,
loader = null;
@@ -12,7 +10,7 @@ function PxLoaderImage(url, tags, priority) {
this.priority = priority;
var onReadyStateChange = function() {
- if (self.img.readyState == 'complete') {
+ if (self.img.readyState === 'complete') {
removeEventHandlers();
loader.onLoad(self);
}
View
8 PxLoaderSound.js
@@ -1,8 +1,6 @@
-// @depends PxLoader.js
-/**
- * PxLoader plugin to load sound using SoundManager2
- */
+/*global PxLoader: true, define: true, soundManager: true */
+// PxLoader plugin to load sound using SoundManager2
function PxLoaderSound(id, url, tags, priority) {
var self = this,
loader = null;
@@ -80,7 +78,7 @@ function PxLoaderSound(id, url, tags, priority) {
this.getName = function() {
return url;
- }
+ };
}
// add a convenience method to PxLoader for adding a sound
View
12 PxLoaderVideo.js
@@ -1,8 +1,6 @@
-// @depends PxLoader.js
-/**
- * PxLoader plugin to load video elements
- */
+/*global PxLoader: true, define: true, Video: true */
+// PxLoader plugin to load video elements
function PxLoaderVideo(url, tags, priority) {
var self = this;
var loader = null;
@@ -17,7 +15,7 @@ function PxLoaderVideo(url, tags, priority) {
this.priority = priority;
var onReadyStateChange = function() {
- if (self.vid.readyState != 4) {
+ if (self.vid.readyState !== 4) {
return;
}
@@ -58,7 +56,7 @@ function PxLoaderVideo(url, tags, priority) {
// called by PxLoader to check status of video (fallback in case
// the event listeners are not triggered).
this.checkStatus = function() {
- if (self.vid.readyState != 4) {
+ if (self.vid.readyState !== 4) {
return;
}
@@ -69,7 +67,7 @@ function PxLoaderVideo(url, tags, priority) {
// called by PxLoader when it is no longer waiting
this.onTimeout = function() {
removeEventHandlers();
- if (self.vid.readyState != 4) {
+ if (self.vid.readyState !== 4) {
loader.onLoad(self);
} else {
loader.onTimeout(self);

0 comments on commit a7c8885

Please sign in to comment.
Something went wrong with that request. Please try again.