From 6ec30fa1e91db402d68bef1fbd7cfa7e730f59bf Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 24 Mar 2018 16:49:00 +0100 Subject: [PATCH] [lib.*] set 'export default' [lib.local-storage-interface] new [lib.authentication] empty --- webpack/lib/api-client.js | 2 +- webpack/lib/authentication.js | 17 +++ webpack/lib/channel-controller.js | 2 +- webpack/lib/content-controller.js | 2 +- webpack/lib/gstore.js | 2 +- webpack/lib/local-storage-interface.js | 173 +++++++++++++++++++++++++ webpack/lib/popup-controller.js | 2 +- webpack/lib/room-controller.js | 2 +- 8 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 webpack/lib/authentication.js create mode 100644 webpack/lib/local-storage-interface.js diff --git a/webpack/lib/api-client.js b/webpack/lib/api-client.js index 65abd2f..2f3c85b 100644 --- a/webpack/lib/api-client.js +++ b/webpack/lib/api-client.js @@ -1,5 +1,5 @@ /* classe API */ -export class APIClient{ +export default class APIClient{ diff --git a/webpack/lib/authentication.js b/webpack/lib/authentication.js new file mode 100644 index 0000000..3769d84 --- /dev/null +++ b/webpack/lib/authentication.js @@ -0,0 +1,17 @@ +export default class Authentication{ + + + /* (1) Constructs an Authentication object + * + * + ---------------------------------------------------------*/ + constructor(){ + + /* (1) Default attributes */ + this._authkey = 'auth.default'; + + /* (2) Section Title */ + + } + +} \ No newline at end of file diff --git a/webpack/lib/channel-controller.js b/webpack/lib/channel-controller.js index 323be7d..faba422 100644 --- a/webpack/lib/channel-controller.js +++ b/webpack/lib/channel-controller.js @@ -1,4 +1,4 @@ -export class ChannelController{ +export default class ChannelController{ /* (1) Construct default attributes * diff --git a/webpack/lib/content-controller.js b/webpack/lib/content-controller.js index 700df76..fcea67f 100644 --- a/webpack/lib/content-controller.js +++ b/webpack/lib/content-controller.js @@ -1,4 +1,4 @@ -export class ContentController{ +export default class ContentController{ /* (1) Construct default attributes * diff --git a/webpack/lib/gstore.js b/webpack/lib/gstore.js index d6bc681..240f219 100644 --- a/webpack/lib/gstore.js +++ b/webpack/lib/gstore.js @@ -1,5 +1,5 @@ /* GlobalStore for VueJS */ -export class GlobalStore{ +export default class GlobalStore{ constructor(){ diff --git a/webpack/lib/local-storage-interface.js b/webpack/lib/local-storage-interface.js new file mode 100644 index 0000000..d773726 --- /dev/null +++ b/webpack/lib/local-storage-interface.js @@ -0,0 +1,173 @@ +export default class LocalStorageInterface{ + + + /* (1) Constructs a localStorage Interface + * + * @_prefix localStorage Interface Prefix (prefix) + * @_ttl 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 Object key + * @_data Object key + * @_ttl [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 Object key + * + * @return outName 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 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) ); + + } + +} \ No newline at end of file diff --git a/webpack/lib/popup-controller.js b/webpack/lib/popup-controller.js index 4f05f2f..c4a5a0b 100644 --- a/webpack/lib/popup-controller.js +++ b/webpack/lib/popup-controller.js @@ -1,4 +1,4 @@ -export class PopupController{ +export default class PopupController{ /* (1) Construct default attributes * diff --git a/webpack/lib/room-controller.js b/webpack/lib/room-controller.js index b5c6356..62b4f3e 100644 --- a/webpack/lib/room-controller.js +++ b/webpack/lib/room-controller.js @@ -1,4 +1,4 @@ -export class RoomController{ +export default class RoomController{ /* (1) Construct default attributes