this._needSeparateTextures = [];
this.loadbalancing = false;
this._loadbalancingServerURL = {};
+ this._textureAssetsCache = {};
}
FluidbookLoader.prototype = {
} else {
this.loadShades(callback);
}
+
+ setInterval(function () {
+ $this.cleanCaches();
+ }, 30000);
},
loadShades: function (callback) {
}
nb++;
var i = $(this).data('id');
- var url = $(this).data('image').replace(/^\.\./, 'data') + '?j=' + (new Date()).getTime();
- textures[i] = $this.loadImage($this.getURL(url), null, null, null, null, cb, 'i_' + i);
+ var url = $(this).data('image').replace(/^\.\./, 'data');
+
+ $this._getTextureLinkAsset(url, i, function (datauri) {
+ textures[i] = $this.loadImage(datauri, null, null, null, 'i_' + i, cb);
+ });
});
if (nb === 0) {
}
},
+ _getTextureLinkAsset: function (url, id, callback) {
+ let $this = this;
+ if (this._textureAssetsCache[url] === undefined || this._textureAssetsCache[url] === null) {
+ this.toDataURL($this.getURL(url), function (res) {
+ $this._textureAssetsCache[url] = {content: res, lastAccess: Date.now()};
+ callback(res);
+ });
+ return;
+ }
+ this._textureAssetsCache[url].lastAccess = Date.now();
+ callback(this._textureAssetsCache[url].content);
+ },
+
+ cleanCaches: function () {
+ let limit = Date.now() - 120000; // 2 min
+ let urlToDelete = [];
+ for (var url in this._textureAssetsCache) {
+ if (this._textureAssetsCache[url].lastAccess < limit) {
+ urlToDelete.push(url);
+ }
+ }
+
+ for (var i in urlToDelete) {
+ delete this._textureAssetsCache[urlToDelete[i]];
+ }
+ },
+
+ toDataURL: function (url, callback) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('get', url);
+ xhr.responseType = 'blob';
+ xhr.onload = function () {
+ var fr = new FileReader();
+
+ fr.onload = function () {
+ callback(this.result);
+ };
+
+ fr.readAsDataURL(xhr.response); // async call
+ };
+
+ xhr.send();
+ },
+
_loadTexture: function (page, callback, enter) {
if (enter === undefined) {
enter = true;