[lib.*] set 'export default' [lib.local-storage-interface] new [lib.authentication] empty

This commit is contained in:
xdrm-brackets 2018-03-24 16:49:00 +01:00
parent a1a5610f2c
commit 6ec30fa1e9
8 changed files with 196 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/* classe API */ /* classe API */
export class APIClient{ export default class APIClient{

View File

@ -0,0 +1,17 @@
export default class Authentication{
/* (1) Constructs an Authentication object
*
*
---------------------------------------------------------*/
constructor(){
/* (1) Default attributes */
this._authkey = 'auth.default';
/* (2) Section Title */
}
}

View File

@ -1,4 +1,4 @@
export class ChannelController{ export default class ChannelController{
/* (1) Construct default attributes /* (1) Construct default attributes
* *

View File

@ -1,4 +1,4 @@
export class ContentController{ export default class ContentController{
/* (1) Construct default attributes /* (1) Construct default attributes
* *

View File

@ -1,5 +1,5 @@
/* GlobalStore for VueJS */ /* GlobalStore for VueJS */
export class GlobalStore{ export default class GlobalStore{
constructor(){ constructor(){

View File

@ -0,0 +1,173 @@
export default class LocalStorageInterface{
/* (1) Constructs a localStorage Interface
*
* @_prefix<String> localStorage Interface Prefix (prefix)
* @_ttl<int> Seconds default valid time for stored data
*
---------------------------------------------------------*/
constructor(_prefix, _ttl){
/* (1) Initialise private attributes */
this._prefix = 'root';
this._ttl = null;
/* (2) Initialise public attributes */
this.keys = [];
/* (3) Set given _prefix if valid */
if( typeof _prefix === 'string' )
this._prefix = _prefix;
/* (4) Set given _ttl if valid */
if( !isNaN(_ttl) )
this._ttl = _ttl;
/* (5) Synchronize keys */
this.synchronise();
}
/* (1) Synchronise keys from localStorage
*
---------------------------------------------------------*/
synchronise(){
// 1. Get localStorage key list
let keys = Object.keys(localStorage);
// 2. Local copy of prefix
let prefix = `${this._prefix}.`;
// 3. Only keep prefixed keys
let prefixed = keys.filter( (k) => ( k.substr(0, prefix.length) === prefix ) );
// 4. Remove prefix
let unprefixed = prefixed.map( (k) => k.substr(prefix.length) );
// X. Update keys
this.keys = unprefixed;
}
/* (2) Store data
*
* @_name<String> Object key
* @_data<String> Object key
* @_ttl<int> [OPT] Seconds valid time for this data
*
---------------------------------------------------------*/
push(_name, _data, _ttl=null){
/* (1) Manage argument
---------------------------------------------------------*/
/* (1) Invalid _name type */
if( typeof _name !== 'string' )
return false;
/* (2) Default value for _ttl */
_ttl = isNaN(_ttl) ? this._ttl : _ttl;
/* (2) Store data
---------------------------------------------------------*/
/* (1) Build storage object */
let storage_object = {
expires: _ttl ? new Date().getTime() + _ttl*1000 : null,
data: _data
};
/* (2) Store storage object */
localStorage.setItem(`${this._prefix}.${_name}`, JSON.stringify(storage_object));
/* (3) Synchronize keys */
this.keys.push(_name);
/* (4) Return status */
return true;
}
/* (3) Fetch data
*
* @_name<String> Object key
*
* @return outName<Object> Object data (NULL on error or not found)
*
---------------------------------------------------------*/
fetch(_name){
/* (1) Manage argument
---------------------------------------------------------*/
/* (1) Invalid _name type */
if( typeof _name !== 'string' )
return null;
/* (2) invalid key */
if( this.keys.indexOf(_name) < 0 )
return null;
/* (2) Fetch data
---------------------------------------------------------*/
/* (1) Try to get data */
let fetched = localStorage.getItem(`${this._prefix}.${_name}`);
/* (2) Try to parse */
try{
var storage_object = JSON.parse(fetched);
/* (3) If cannot parse -> remove */
}catch(e){
this.pop(_name);
return null;
}
/* (4) If not valid anymore -> delete + return NULL */
if( storage_object.expires < new Date().getTime() ){
this.pop(_name);
return null;
}
/* (5) Return data */
return storage_object.data;
}
/* (4) Remove data
*
* @_name<String> Object key
*
---------------------------------------------------------*/
pop(_name){
/* (1) Invalid _name type */
if( typeof _name !== 'string' )
return null;
/* (2) invalid key */
if( this.keys.indexOf(_name) < 0 )
return null;
/* (3) Remove from localStorage */
localStorage.removeItem(`${this._prefix}.${_name}`);
/* (4) Update keys */
this.keys = this.keys.filter( (k) => (k!==_name) );
}
}

View File

@ -1,4 +1,4 @@
export class PopupController{ export default class PopupController{
/* (1) Construct default attributes /* (1) Construct default attributes
* *

View File

@ -1,4 +1,4 @@
export class RoomController{ export default class RoomController{
/* (1) Construct default attributes /* (1) Construct default attributes