-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalStorageStore.js
More file actions
41 lines (38 loc) · 1 KB
/
LocalStorageStore.js
File metadata and controls
41 lines (38 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* LocalStorageStore — injectable abstraction over the localStorage API.
*
* Wraps any backend implementing { getItem, setItem, removeItem }.
* Defaults to globalThis.localStorage for browser use.
* Pass a LocalStorageShim (or any compatible object) for testing in Node.js.
*
* setItem() may throw QuotaExceededError — callers are responsible for catching.
*/
export default class LocalStorageStore {
/**
* @param {Object} [backend] — storage backend, defaults to globalThis.localStorage
*/
constructor(backend = globalThis.localStorage) {
this._backend = backend;
}
/**
* @param {string} key
* @returns {string|null}
*/
getItem(key) {
return this._backend.getItem(key);
}
/**
* @param {string} key
* @param {string} value
* @throws {DOMException|Error} QuotaExceededError if storage is full
*/
setItem(key, value) {
this._backend.setItem(key, value);
}
/**
* @param {string} key
*/
removeItem(key) {
this._backend.removeItem(key);
}
}