-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…) (#45437)
- Loading branch information
1 parent
883b844
commit 50a760e
Showing
12 changed files
with
401 additions
and
121 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export interface StorageManager { | ||
(options: { key: string; storageWindow?: Window | null }): { | ||
/** | ||
* Function to get the value from the storage | ||
* @param defaultValue The default value to be returned if the key is not found | ||
* @returns The value from the storage or the default value | ||
*/ | ||
get(defaultValue: any): any; | ||
/** | ||
* Function to set the value in the storage | ||
* @param value The value to be set | ||
* @returns void | ||
*/ | ||
set(value: any): void; | ||
/** | ||
* Function to subscribe to the value of the specified key triggered by external events | ||
* @param handler The function to be called when the value changes | ||
* @returns A function to unsubscribe the handler | ||
* @example | ||
* React.useEffect(() => { | ||
* const unsubscribe = storageManager.subscribe((value) => { | ||
* console.log(value); | ||
* }); | ||
* return unsubscribe; | ||
* }, []); | ||
*/ | ||
subscribe(handler: (value: any) => void): () => void; | ||
}; | ||
} | ||
|
||
function noop() {} | ||
|
||
const localStorageManager: StorageManager = ({ key, storageWindow }) => { | ||
if (!storageWindow && typeof window !== 'undefined') { | ||
storageWindow = window; | ||
} | ||
return { | ||
get(defaultValue) { | ||
if (typeof window === 'undefined') { | ||
return undefined; | ||
} | ||
if (!storageWindow) { | ||
return defaultValue; | ||
} | ||
let value; | ||
try { | ||
value = storageWindow.localStorage.getItem(key); | ||
} catch { | ||
// Unsupported | ||
} | ||
return value || defaultValue; | ||
}, | ||
set: (value) => { | ||
if (storageWindow) { | ||
try { | ||
storageWindow.localStorage.setItem(key, value); | ||
} catch { | ||
// Unsupported | ||
} | ||
} | ||
}, | ||
subscribe: (handler) => { | ||
if (!storageWindow) { | ||
return noop; | ||
} | ||
const listener = (event: StorageEvent) => { | ||
const value = event.newValue; | ||
if (event.key === key) { | ||
handler(value); | ||
} | ||
}; | ||
storageWindow.addEventListener('storage', listener); | ||
return () => { | ||
storageWindow.removeEventListener('storage', listener); | ||
}; | ||
}, | ||
}; | ||
}; | ||
|
||
export default localStorageManager; |
Oops, something went wrong.