Web browser storage
web-browser-storagelocalStorage for better API interface and features like:
npm install web-browser-storage --save
`
Use
$3
A Storage interface provides the following methods:
* get(key) => value
* set(key, value)
* has(key) => boolean
* delete(key)
* keys() => string[]
* onExternalChange(handlerFunction: ({ key, value, prevValue }) => {}) => stopListeningFunction
* value and prevValue are one of:
* null — When absent.
* any — When present. If stringified, then it is parsed from string.
* undefined — When present and stringified and can't be parsed from string.
$3
`js
import { LocalStorage } from 'web-browser-storage'
const storage = new LocalStorage()
storage.set('key', { a: 'b' })
storage.get('key') === { a: 'b' }
storage.has('key') === true
storage.delete('key')
storage.getRecordSize('key') === 24 // in bytes
storage.keys() === ['key']
const unlistenExternalChanges = storage.onExternalChange(({ key, value, prevValue }) => {
console.log(key, value)
})
`
Checking localStorage availability:
`js
import { LocalStorage } from 'web-browser-storage'
if (!LocalStorage.isAvailable()) {
alert('You seem to have disabled localStorage')
}
`
Available LocalStorage constructor options:
* onFull({ error }) — Gets called on QuotaExceeded errors.
$3
MemoryStorage could be used in place of LocalStorage in tests.
`js
import { MemoryStorage } from 'web-browser-storage'
const storage = new MemoryStorage()
storage.set('key', { a: 'b' })
storage.get('key') === { a: 'b' }
storage.getData() === { key: { a: 'b' } }
storage.setData({ key2: 'value2' })
storage.getData() === { key2: 'value2' }
`
Creating several MemoryStorage instances that share the same data:
`js
import { MemoryStorage } from 'web-browser-storage'
const sourceStorage = new MemoryStorage()
const storage1 = storageSource.createSharedInstance('1')
const storage2 = storageSource.createSharedInstance('2')
const unlistenExternalChanges1 = storage1.onExternalChange(
() => console.log('External change detected in storage 1')
)
const unlistenExternalChanges2 = storage2.onExternalChange(
() => console.log('External change detected in storage 2')
)
// Will print "External change detected in storage 2".
storage1.set('key', 'value')
`
Available MemoryStorage constructor options:
* stringifyStoredValues: boolean — By default, MemoryStorage performs forced data serialization/deserialization on write/read. That is to emulate localStorage's behavior which "serializes" everything to string on write. For example, by default, when writing Date objects, MemoryStorage (as well as localStorage) writes Date objects to ISO strings, and when reading those back they'll become strings rather than Date objects. To disable such forced stringification for whatever reason, pass stringifyStoredValues: false option, and such Date objects will stay Date objects when re-reading them from storage.
$3
CachedStorage is a wrapper around a storage that makes it "cache" the changes in memory and only "flush" them to disk after a delay or when the browser tab loses focus.
CachedStorage implements the standard Storage interface.
CachedStorage could be used in cases when there're a lot of frequent writes to localStorage.
`js
import { LocalStorage, CachedStorage } from 'web-browser-storage'
const localStorage = new LocalStorage()
const storage = new CachedStorage({
storage: localStorage,
flushDelay: 30 * 1000 // in milliseconds
})
storage.start()
// All keys matching pattern "key*" will be cached.
// But only when the tab is in foreground.
// When a tab is in background, cache is disabled.
storage.cacheKey('key*')
storage.set('key', { a: 'b' })
storage.get('key') === { a: 'b' }
localStorage.get('key') === null
storage.stop()
localStorage.get('key') === { a: 'b' }
// One could also call .flush() manually, if required:
// storage.flush()
`
Available constructor parameters:
* storage — An underlying storage. Tests could use a MemoryStorage instance.
* tabStatusWatcher: TabStatusWatcher — An instance of TabStatusWatcher. Tests could use a TestTabStatusWatcher instance.
* timer: Timer — An instance of Timer. Tests could use a TestTimer instance.
* flushDelay: number — Flush delay, in milliseconds. This is the time CachedStorage waits until flushing the changes to disk. It will also flush if the tab becomes "inactive" (loses focus or goes into background, etc).
* log: (...args) => {} — A logging function. For example, one could pass console.log as the log parameter.
* merge: (key, cachedValue, newValue) => mergedValue — If storage data has been changed "externally" for a currently cached key, the merge() function will be used to resolve the conflict by merging the currently cached value and the externally updated value. If merge() function is not specified, the cached value gets discarded and overwritten by the externally updated one.
Test
`
npm test
``