-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gernsdorfer
authored and
gernsdorfer
committed
Dec 18, 2023
1 parent
b4fa5f2
commit 7847d68
Showing
3 changed files
with
29 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
import { ClientStoragePlugin } from '../models'; | ||
|
||
class ClientStorage implements ClientStoragePlugin { | ||
constructor(private store: Storage) {} | ||
constructor(private store?: Storage) {} | ||
|
||
getDefaultState<STATE>(storeName: string): STATE | undefined { | ||
const stateFromStorage = this.store.getItem(storeName); | ||
const stateFromStorage = this.store?.getItem(storeName); | ||
if (!stateFromStorage) return; | ||
return JSON.parse(stateFromStorage); | ||
} | ||
|
||
setStateToStorage<STATE>(storeName: string, state: STATE) { | ||
this.store.setItem(storeName, JSON.stringify(state)); | ||
this.store?.setItem(storeName, JSON.stringify(state)); | ||
} | ||
} | ||
export const getWindow = (currentWindow?: Window): Window | undefined => | ||
typeof currentWindow === 'object' ? currentWindow : undefined; | ||
|
||
export const sessionStoragePlugin = new ClientStorage(window.sessionStorage); | ||
export const sessionStoragePlugin = new ClientStorage( | ||
getWindow(window)?.sessionStorage, | ||
); | ||
|
||
export const localStoragePlugin = new ClientStorage(window.localStorage); | ||
export const localStoragePlugin = new ClientStorage( | ||
getWindow(window)?.localStorage, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './client-storage.plugin'; | ||
export { | ||
localStoragePlugin, | ||
sessionStoragePlugin, | ||
} from './client-storage.plugin'; |