HTML and CSS Reference
In-Depth Information
// Return the asset. This is null if the asset is still loading
return cachedAsset.asset;
}
return null;
};
AssetCache.prototype.request = function (key, params, callback) {
// Look for the asset in the cache
var cachedAsset = this.cache[key];
if (cachedAsset) {
// Set the current hitCounter for the asset
// This indicates it is the last requested asset
cachedAsset.cacheHit = this.hitCounter;
this.hitCounter += 1;
if (!callback) {
return;
}
if (cachedAsset.isLoading) {
// Subscribe the callback to be called when loading is complete
cachedAsset.observer.subscribe(callback);
} else {
// Call the callback asynchronously, like a request response
TurbulenzEngine.setTimeout(function requestCallbackFn() {
callback(key, cachedAsset.asset, params);
}, 0);
}
return;
}
var cacheArray = this.cacheArray;
var cacheArrayLength = cacheArray.length;
if (cacheArrayLength >= this.maxCacheSize) {
// If the cache exceeds the maximum cache size, remove an asset
var cache = this.cache;
var oldestCacheHit = this.hitCounter;
var oldestKey = null;
var oldestIndex;
var i;
// Find the oldest cache entry
for (i = 0; i < cacheArrayLength; i += 1) {
if (cacheArray[i].cacheHit < oldestCacheHit) {
oldestCacheHit = cacheArray[i].cacheHit;
oldestIndex = i;
}
}
// Reuse an existing cachedAsset object to avoid object re-creation
cachedAsset = cacheArray[oldestIndex];
oldestKey = cachedAsset.key;
Search WWH ::




Custom Search