[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-core] 18/19: feature: useLocalStorage also update when the
From: |
gnunet |
Subject: |
[taler-wallet-core] 18/19: feature: useLocalStorage also update when the localStorage has been updated from other window |
Date: |
Wed, 07 Dec 2022 20:08:46 +0100 |
This is an automated email from the git hooks/post-receive script.
sebasjm pushed a commit to branch master
in repository wallet-core.
commit 1c6369677ab272196da314d95825273c6fff8d5f
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Wed Dec 7 15:44:43 2022 -0300
feature: useLocalStorage also update when the localStorage has been updated
from other window
---
packages/web-util/src/hooks/useLocalStorage.ts | 30 +++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/packages/web-util/src/hooks/useLocalStorage.ts
b/packages/web-util/src/hooks/useLocalStorage.ts
index ed5b491f2..25df9dfcd 100644
--- a/packages/web-util/src/hooks/useLocalStorage.ts
+++ b/packages/web-util/src/hooks/useLocalStorage.ts
@@ -19,7 +19,7 @@
* @author Sebastian Javier Marchano (sebasjm)
*/
-import { StateUpdater, useState } from "preact/hooks";
+import { StateUpdater, useEffect, useState } from "preact/hooks";
export function useLocalStorage(
key: string,
@@ -33,6 +33,16 @@ export function useLocalStorage(
},
);
+ useEffect(() => {
+ const listener = buildListenerForKey(key, (newValue) => {
+ setStoredValue(newValue ?? initialValue)
+ })
+ window.addEventListener('storage', listener)
+ return () => {
+ window.removeEventListener('storage', listener)
+ }
+ }, [])
+
const setValue = (
value?: string | ((val?: string) => string | undefined),
): void => {
@@ -52,6 +62,13 @@ export function useLocalStorage(
return [storedValue, setValue];
}
+function buildListenerForKey(key: string, onUpdate: (newValue: string |
undefined) => void): () => void {
+ return function listenKeyChange() {
+ const value = window.localStorage.getItem(key)
+ onUpdate(value ?? undefined)
+ }
+}
+
//TODO: merge with the above function
export function useNotNullLocalStorage(
key: string,
@@ -63,6 +80,17 @@ export function useNotNullLocalStorage(
: initialValue;
});
+
+ useEffect(() => {
+ const listener = buildListenerForKey(key, (newValue) => {
+ setStoredValue(newValue ?? initialValue)
+ })
+ window.localStorage.addEventListener('storage', listener)
+ return () => {
+ window.localStorage.removeEventListener('storage', listener)
+ }
+ })
+
const setValue = (value: string | ((val: string) => string)): void => {
const valueToStore = value instanceof Function ? value(storedValue) :
value;
setStoredValue(valueToStore);
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-wallet-core] 01/19: pretty, (continued)
- [taler-wallet-core] 01/19: pretty, gnunet, 2022/12/07
- [taler-wallet-core] 05/19: fix: empty form should not allow the "send" button, gnunet, 2022/12/07
- [taler-wallet-core] 19/19: no-fix: user logger instead of console.log, gnunet, 2022/12/07
- [taler-wallet-core] 11/19: no-fix: removing unused showPublicHistories, gnunet, 2022/12/07
- [taler-wallet-core] 15/19: fix jsx config, gnunet, 2022/12/07
- [taler-wallet-core] 14/19: fix: updating the title is better that location.hash, gnunet, 2022/12/07
- [taler-wallet-core] 08/19: no-fix: move out routing, gnunet, 2022/12/07
- [taler-wallet-core] 16/19: no-fix: remove unused, gnunet, 2022/12/07
- [taler-wallet-core] 12/19: no-fix: moved out AccountPage, gnunet, 2022/12/07
- [taler-wallet-core] 07/19: fix: show error message on login and registration form, prevent saving password on localstorage, gnunet, 2022/12/07
- [taler-wallet-core] 18/19: feature: useLocalStorage also update when the localStorage has been updated from other window,
gnunet <=
- [taler-wallet-core] 06/19: fix: better loading page while waiting for server response, gnunet, 2022/12/07
- [taler-wallet-core] 17/19: no-fix: remove 'any' and login status is taken from backend, gnunet, 2022/12/07
- [taler-wallet-core] 09/19: no-fix: moving out public histories page, gnunet, 2022/12/07
- [taler-wallet-core] 10/19: no-fix: moving out registration page, gnunet, 2022/12/07