use-json-localstorage 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +15 -2
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +15 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -71,6 +71,10 @@ var getRuntimeLocalStorage = () => {
|
|
|
71
71
|
const value = globalThis.localStorage.getItem(key);
|
|
72
72
|
return value ? deserialize(value) : null;
|
|
73
73
|
},
|
|
74
|
+
remove: (key) => {
|
|
75
|
+
globalThis.localStorage.removeItem(key);
|
|
76
|
+
emitter.emit(`remove`, { key });
|
|
77
|
+
},
|
|
74
78
|
on: (type, callback) => {
|
|
75
79
|
return emitter.on(type, callback);
|
|
76
80
|
}
|
|
@@ -87,18 +91,27 @@ var useLocalStorageValue = (key, defaultValue) => {
|
|
|
87
91
|
const store = (0, import_react.useRef)(getRuntimeLocalStorage());
|
|
88
92
|
return (0, import_react.useSyncExternalStore)(
|
|
89
93
|
(onStoreChange) => {
|
|
90
|
-
|
|
94
|
+
const setUnsubscribe = store.current.on("set", (event) => {
|
|
95
|
+
if (event.key === key) {
|
|
96
|
+
onStoreChange();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const removeUnsubscribe = store.current.on("remove", (event) => {
|
|
91
100
|
if (event.key === key) {
|
|
92
101
|
onStoreChange();
|
|
93
102
|
}
|
|
94
103
|
});
|
|
104
|
+
return () => {
|
|
105
|
+
setUnsubscribe();
|
|
106
|
+
removeUnsubscribe();
|
|
107
|
+
};
|
|
95
108
|
},
|
|
96
109
|
() => {
|
|
97
110
|
const value = store.current.get(key);
|
|
98
111
|
return value != null ? value : defaultValue;
|
|
99
112
|
},
|
|
100
113
|
() => {
|
|
101
|
-
return
|
|
114
|
+
return void 0;
|
|
102
115
|
}
|
|
103
116
|
);
|
|
104
117
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -5,10 +5,10 @@ interface SerializeStorage {
|
|
|
5
5
|
}
|
|
6
6
|
type LocalStorageEvent = {
|
|
7
7
|
key: string;
|
|
8
|
-
value
|
|
8
|
+
value?: unknown;
|
|
9
9
|
};
|
|
10
10
|
declare const getRuntimeLocalStorage: () => SerializeStorage;
|
|
11
11
|
|
|
12
|
-
declare const useLocalStorageValue: <T>(key: string, defaultValue
|
|
12
|
+
declare const useLocalStorageValue: <T>(key: string, defaultValue?: T) => T | undefined;
|
|
13
13
|
|
|
14
14
|
export { getRuntimeLocalStorage, useLocalStorageValue };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,10 +5,10 @@ interface SerializeStorage {
|
|
|
5
5
|
}
|
|
6
6
|
type LocalStorageEvent = {
|
|
7
7
|
key: string;
|
|
8
|
-
value
|
|
8
|
+
value?: unknown;
|
|
9
9
|
};
|
|
10
10
|
declare const getRuntimeLocalStorage: () => SerializeStorage;
|
|
11
11
|
|
|
12
|
-
declare const useLocalStorageValue: <T>(key: string, defaultValue
|
|
12
|
+
declare const useLocalStorageValue: <T>(key: string, defaultValue?: T) => T | undefined;
|
|
13
13
|
|
|
14
14
|
export { getRuntimeLocalStorage, useLocalStorageValue };
|
package/dist/index.js
CHANGED
|
@@ -34,6 +34,10 @@ var getRuntimeLocalStorage = () => {
|
|
|
34
34
|
const value = globalThis.localStorage.getItem(key);
|
|
35
35
|
return value ? deserialize(value) : null;
|
|
36
36
|
},
|
|
37
|
+
remove: (key) => {
|
|
38
|
+
globalThis.localStorage.removeItem(key);
|
|
39
|
+
emitter.emit(`remove`, { key });
|
|
40
|
+
},
|
|
37
41
|
on: (type, callback) => {
|
|
38
42
|
return emitter.on(type, callback);
|
|
39
43
|
}
|
|
@@ -50,18 +54,27 @@ var useLocalStorageValue = (key, defaultValue) => {
|
|
|
50
54
|
const store = useRef(getRuntimeLocalStorage());
|
|
51
55
|
return useSyncExternalStore(
|
|
52
56
|
(onStoreChange) => {
|
|
53
|
-
|
|
57
|
+
const setUnsubscribe = store.current.on("set", (event) => {
|
|
58
|
+
if (event.key === key) {
|
|
59
|
+
onStoreChange();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const removeUnsubscribe = store.current.on("remove", (event) => {
|
|
54
63
|
if (event.key === key) {
|
|
55
64
|
onStoreChange();
|
|
56
65
|
}
|
|
57
66
|
});
|
|
67
|
+
return () => {
|
|
68
|
+
setUnsubscribe();
|
|
69
|
+
removeUnsubscribe();
|
|
70
|
+
};
|
|
58
71
|
},
|
|
59
72
|
() => {
|
|
60
73
|
const value = store.current.get(key);
|
|
61
74
|
return value != null ? value : defaultValue;
|
|
62
75
|
},
|
|
63
76
|
() => {
|
|
64
|
-
return
|
|
77
|
+
return void 0;
|
|
65
78
|
}
|
|
66
79
|
);
|
|
67
80
|
};
|