You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
716 B
37 lines
716 B
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
|
|
|