| 66 | export const localStorage = new LocalOnlyStorage(); |
| 67 | |
| 68 | class SessionThenLocalStorage implements Storage { |
| 69 | get<T>(key: string, ifNotPresent: T): string | T { |
| 70 | try { |
| 71 | const sessionValue = window.sessionStorage.getItem(prefix + key); |
| 72 | if (sessionValue !== null) return sessionValue; |
| 73 | } catch { |
| 74 | // Swallow up any security exceptions... |
| 75 | } |
| 76 | return localStorage.get<T>(key, ifNotPresent); |
| 77 | } |
| 78 | |
| 79 | remove(key: string) { |
| 80 | this.removeSession(key); |
| 81 | localStorage.remove(key); |
| 82 | } |
| 83 | |
| 84 | private removeSession(key: string) { |
| 85 | try { |
| 86 | window.sessionStorage.removeItem(prefix + key); |
| 87 | } catch { |
| 88 | // Swallow up any security exceptions... |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private setSession(key: string, value: string): boolean { |
| 93 | try { |
| 94 | window.sessionStorage.setItem(prefix + key, value); |
| 95 | return true; |
| 96 | } catch { |
| 97 | // Swallow up any security exceptions... |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | set(key: string, value: string): boolean { |
| 103 | const setBySession = this.setSession(key, value); |
| 104 | const setByLocal = localStorage.set(key, value); |
| 105 | return setBySession || setByLocal; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | export const sessionThenLocalStorage = new SessionThenLocalStorage(); |
nothing calls this directly
no outgoing calls
no test coverage detected