MediaWiki:Gadget-StorageUtils.js

Hello, you have come here looking for the meaning of the word MediaWiki:Gadget-StorageUtils.js. In DICTIOUS you will not only get to know all the dictionary meanings for the word MediaWiki:Gadget-StorageUtils.js, but we will also tell you about its etymology, its characteristics and you will know how to say MediaWiki:Gadget-StorageUtils.js in singular and plural. Everything you need to know about the word MediaWiki:Gadget-StorageUtils.js you have here. The definition of the word MediaWiki:Gadget-StorageUtils.js will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofMediaWiki:Gadget-StorageUtils.js, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.
// {{documentation}}
// implicit dependencies : mediawiki.cookie,mw.storage
/* jshint maxerr:1048576, strict:true, undef:true, latedef:true, es5:true, sub:true */

/* global mw, $ */

//lowest level wrapper. wraps localstorage and cookie
var StorageWrapper = window.StorageWrapper = function(){
	this.localStorageAvailable = mw.storage.get("localStorageTest") !== false;
	this.storageAvailable = this.localStorageAvailable || navigator.cookieEnabled;
	this.cookiePreferences = {
		expires: 30, path: '/'
	};
	
	this.set = function(name, value){
		if (this.localStorageAvailable) {
			localStorage = value;
		} else {
			mw.cookie.set(name, value, {});
		}
	};
	this.get = function(name) {
		if (this.localStorageAvailable){
			return localStorage;
		} else {
			return mw.cookie.get(name);
		}
	};
};

// expirable, lazy object storage. ideally should inherit(?) from ObjectStorage
window.CacheableStorage = function(productName, getProductCallback, expireInDays, version){
	this.storageWrapper = new StorageWrapper();
	this.StorageAvailable = this.storageWrapper.storageAvailable;
	this.expireInDays = expireInDays;
	this.version = version || 1;
	this.productName = productName;
	this.getProductCallback = getProductCallback;
	this._cachedData = null;
	
	this.itemAddressPrefix = "enwikt/CacheableStorage/" + this.productName + "?v=" + this.version;
	this.itemDataAddress = this.itemAddressPrefix + "&Type=Data";
	this.itemTypeAddress = this.itemAddressPrefix + "&Type=Type";
	this.itemExpirationAddress = this.itemAddressPrefix + "&Type=Expiration";
	
	this.refreshData = function() {
		var this1 = this;
		this.getProductCallback().then(function(d) {
			this1.storageWrapper.set(this1.itemTypeAddress, typeof(d));
			this1.storageWrapper.set(this1.itemExpirationAddress, new Date().toISOString());
			
			if (typeof(d) != "string") d = JSON.stringify(d);
			this1.storageWrapper.set(this1.itemDataAddress, d);
		});
	};
	this.GetItem = function(){
		if (this._cachedData)
			return this._cachedData;
		
		var data = this.storageWrapper.get(this.itemDataAddress);
		var type = this.storageWrapper.get(this.itemTypeAddress);
		var expiration = this.storageWrapper.get(this.itemExpirationAddress);
		if (data)
		{
			if (type != "string") data = JSON.parse(data);
			if ((new Date() - new Date(expiration))/1000 > this.expireInDays*24*3600){
				this.refreshData();
				return data;
			}
			else{
				this._cachedData=data;
				return this._cachedData;
			}
		}
		else
		{
			this.refreshData();
		}
		return null;
	};
};


//not expirable
window.ObjectStorage = function(contextName, version){
	this.storageWrapper = new StorageWrapper();
	this.StorageAvailable = this.storageWrapper.storageAvailable;
	this.version = version || 1;
	this.contextName = contextName;
	this._cachedData = null;
	
	this.itemAddressPrefix = "enwikt/ObjectStorage/" + this.contextName + "?v=" + this.version;
	this.itemDataAddress = this.itemAddressPrefix + "&Type=Data";
	this.itemTypeAddress = this.itemAddressPrefix + "&Type=Type";
	
	this.Set = function(obj) {
		var s = typeof(obj) == "string" ? obj : JSON.stringify(obj);
		this.storageWrapper.set(this.itemDataAddress, s);
		this.storageWrapper.set(this.itemTypeAddress, typeof(obj));
		this._cachedData = obj;
	};
	this.Get = function(){
		if (this._cachedData)
			return this._cachedData;
		
		var data = this.storageWrapper.get(this.itemDataAddress);
		var type = this.storageWrapper.get(this.itemTypeAddress);
		if (data)
		{
			if (type != "string") data = JSON.parse(data);
			this._cachedData=data;
			return this._cachedData;
		}
		return null;
	};
};