HTML and CSS Reference
In-Depth Information
// Call the onDestroy function if the cachedAsset is loaded
if (this.onDestroy && !cachedAsset.isLoading) {
this.onDestroy(oldestKey, cachedAsset.asset);
}
delete cache[oldestKey];
// Reset the cachedAsset for the new entry
cachedAsset.cacheHit = this.hitCounter;
cachedAsset.asset = null;
cachedAsset.isLoading = true;
cachedAsset.key = key;
cachedAsset.observer = Observer.create();
this.cache[key] = cachedAsset;
} else {
// Create a new entry (up to the maxCacheSize)
cachedAsset = this.cache[key] = cacheArray[cacheArrayLength] = {
cacheHit: this.hitCounter,
asset: null,
isLoading: true,
key: key,
observer: Observer.create()
};
}
this.hitCounter += 1;
var that = this;
var observer = cachedAsset.observer;
if (callback) {
// Subscribe the callback to be called when the asset is loaded
observer.subscribe(callback);
}
this.onLoad(key, params, function onLoadedAssetFn(asset) {
if (cachedAsset.key === key) {
// Check the cachedAsset hasn't be re-allocated during loading
cachedAsset.cacheHit = that.hitCounter;
cachedAsset.asset = asset;
cachedAsset.isLoading = false;
that.hitCounter += 1;
// Notify all callbacks that the asset has been loaded
cachedAsset.observer.notify(key, asset, params);
} else {
if (that.onDestroy) {
// Destroy assets that have been removed from the cache during loading
that.onDestroy(key, asset);
}
// Notify the original observer that asset was not saved in the cache
observer.notify(key, null, params);
}
});
};
Search WWH ::




Custom Search