Last active
June 21, 2016 19:53
-
-
Save iOnline247/3630ca63073bce5ffdfc9ee1221ab91e to your computer and use it in GitHub Desktop.
Local/Sessionstorage wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(window) { | |
| function isFunction (func) { | |
| return typeof func === 'function'; | |
| } | |
| function isStorageEnabled () { | |
| const str = 'test'; | |
| try { | |
| this.store.setItem(str, str); | |
| this.store.removeItem(str); | |
| return true; | |
| } catch (e) { | |
| return false; | |
| } | |
| } | |
| function timestampData (o, expire) { | |
| const now = new Date(); | |
| let timestamp; | |
| const minutesFromNow = (expire) ? | |
| expire : | |
| this.defaultTimeoutMinutes | |
| ; | |
| if (typeof minutesFromNow === 'number') { | |
| now.setTime(now.getTime() + minutesFromNow * 60000); | |
| } else if (Object.prototype.toString.call(expire) !== '[object Date]') { | |
| throw 'The "expires" parameter must be a number or Date object.'; | |
| } | |
| // Set timestamp in case a Date object is passed in. | |
| timestamp = now; | |
| return JSON.stringify({ expiration: timestamp, value: o }); | |
| } | |
| function Store (timeoutInMinutes) { | |
| if (!(this instanceof Store)) { | |
| return new Store(timeoutInMinutes); | |
| } | |
| this.local = typeof localStorage !== 'undefined' ? new StorageWrapper(localStorage, timeoutInMinutes) : null; | |
| this.session = typeof sessionStorage !== 'undefined' ? new StorageWrapper(sessionStorage, timeoutInMinutes) : null; | |
| } | |
| function StorageWrapper (store, timeoutInMinutes) { | |
| this.store = store; | |
| this.defaultTimeoutMinutes = timeoutInMinutes || 5; | |
| this.enabled = isStorageEnabled.call(this); | |
| } | |
| StorageWrapper.prototype = { | |
| delete: function (key) { | |
| if (this.enabled) { | |
| this.store.removeItem(key); | |
| } | |
| }, | |
| get: function (key) { | |
| if (!this.enabled) { | |
| return null; | |
| } | |
| let data = this.store.getItem(key); | |
| if (data == null) { | |
| return data; | |
| } | |
| data = JSON.parse(data); | |
| const timestamp = new Date(data.expiration); | |
| if (timestamp <= new Date()) { | |
| this.delete(key); | |
| return null; | |
| } else { | |
| return data.value; | |
| } | |
| }, | |
| getOrPut: function (key, getter, expire) { | |
| getter = getter || (_ => Promise.resolve(_)); | |
| expire = expire || this.defaultTimeoutMinutes; | |
| if (!isFunction(getter)) { | |
| throw 'Function expected for parameter "getter".'; | |
| } | |
| if (!this.enabled) { | |
| return getter(); | |
| } | |
| return new Promise((resolve, reject) => { | |
| const o = this.get(key); | |
| if (o == null) { | |
| let data = getter(); | |
| if (!isFunction(data.then)) { | |
| throw 'The parameter "getter" is not a Promise.'; | |
| } | |
| data.then((d) => { | |
| this.put(key, d, expire); | |
| resolve(d); | |
| }); | |
| } else { | |
| resolve(o); | |
| } | |
| }); | |
| }, | |
| put: function (key, o, expire) { | |
| if (this.enabled) { | |
| this.store.setItem(key, timestampData.call(this, o, expire)); | |
| } | |
| } | |
| }; | |
| window.store = Store; | |
| }(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try to integrate these notes:
https://jsfiddle.net/iOnline247/rnqs5/