web-browser-storage
v0.1.14
Published
Web browser storage
Downloads
56
Maintainers
Readme
web-browser-storage
A wrapper around localStorage for better API interface and features like:
- Working with JSON values rather than strings.
- Caching.
Install
npm install web-browser-storage --saveUse
Storage
A Storage interface provides the following methods:
get(key) => valueset(key, value)has(key) => booleandelete(key)keys() => string[]onExternalChange(handlerFunction: ({ key, value, prevValue }) => {}) => stopListeningFunctionvalueandprevValueare 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.
Browser
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:
import { LocalStorage } from 'web-browser-storage'
if (!LocalStorage.isAvailable()) {
alert('You seem to have disabled `localStorage`')
}Available LocalStorage constructor options:
onFull({ error })— Gets called onQuotaExceedederrors.
Stub
MemoryStorage could be used in place of LocalStorage in tests.
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:
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,MemoryStorageperforms forced data serialization/deserialization on write/read. That is to emulatelocalStorage's behavior which "serializes" everything to string on write. For example, by default, when writingDateobjects,MemoryStorage(as well aslocalStorage) writesDateobjects to ISO strings, and when reading those back they'll become strings rather thanDateobjects. To disable such forced stringification for whatever reason, passstringifyStoredValues: falseoption, and suchDateobjects will stayDateobjects when re-reading them from storage.
Cache
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.
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 aMemoryStorageinstance.tabStatusWatcher: TabStatusWatcher— An instance ofTabStatusWatcher. Tests could use aTestTabStatusWatcherinstance.timer: Timer— An instance ofTimer. Tests could use aTestTimerinstance.flushDelay: number— Flush delay, in milliseconds. This is the timeCachedStoragewaits 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 passconsole.logas thelogparameter.merge: (key, cachedValue, newValue) => mergedValue— If storage data has been changed "externally" for a currently cached key, themerge()function will be used to resolve the conflict by merging the currently cached value and the externally updated value. Ifmerge()function is not specified, the cached value gets discarded and overwritten by the externally updated one.
Test
npm testGitHub Ban
On March 9th, 2020, GitHub, Inc. silently banned my account (erasing all my repos, issues and comments) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The GitHub repo is now only used as a backup (you can star the repo there too), and the primary repo is now the GitLab one. Issues can be reported in any repo.
