Created
November 30, 2017 02:23
-
-
Save sirdlx/9a1743cdb59157d7c2c39c4717b0a090 to your computer and use it in GitHub Desktop.
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
class StorageDriver { | |
constructor (conf = {}) { | |
this.conf = conf | |
if ( | |
typeof this.conf.storage.getItem !== 'function' || | |
typeof this.conf.storage.removeItem !== 'function' || | |
typeof this.conf.storage.setItem !== 'function' | |
) { | |
throw new Error('Given Storage doesn\'t have methods `getItem`, `setItem` and `removeItem`.') | |
} | |
} | |
setItem (key, value) { | |
return this.conf.storage.setItem(this.conf.name + ':' + key, JSON.stringify(value)) | |
} | |
getItem (key) { | |
return JSON.parse(this.conf.storage.getItem(this.conf.name + ':' + key)) | |
} | |
removeItem (key) { | |
return this.conf.storage.removeItem(this.conf.name + ':' + key) | |
} | |
} | |
export default StorageDriver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment