1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 14:15:06 +01:00
metamask-extension/app/scripts/lib/observable/local-storage.js

38 lines
716 B
JavaScript

const ObservableStore = require('./index')
//
// LocalStorageStore
//
// uses localStorage instead of a cache
//
class LocalStorageStore extends ObservableStore {
constructor (opts) {
super()
delete this._state
this._opts = opts || {}
if (!this._opts.storageKey) {
throw new Error('LocalStorageStore - no "storageKey" specified')
}
this._storageKey = this._opts.storageKey
}
get() {
try {
return JSON.parse(global.localStorage[this._storageKey])
} catch (err) {
return undefined
}
}
_put(newState) {
global.localStorage[this._storageKey] = JSON.stringify(newState)
this.emit('update', newState)
}
}
module.exports = LocalStorageStore