Skip to content

Instantly share code, notes, and snippets.

@sirdlx
Created November 30, 2017 02:23
Show Gist options
  • Save sirdlx/9a1743cdb59157d7c2c39c4717b0a090 to your computer and use it in GitHub Desktop.
Save sirdlx/9a1743cdb59157d7c2c39c4717b0a090 to your computer and use it in GitHub Desktop.
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