@stimulus-library/mixins 1.0.0-alpha.8 → 1.0.1
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/use_localstorage.js +17 -2
- package/package.json +3 -3
package/dist/use_localstorage.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { reactive } from "@stimulus-library/utilities";
|
|
2
|
+
import { useEventListener } from "./use_event_listener";
|
|
2
3
|
export const StorageSerializers = {
|
|
3
4
|
boolean: {
|
|
4
5
|
deserialize: (v) => v === "true",
|
|
@@ -7,7 +8,15 @@ export const StorageSerializers = {
|
|
|
7
8
|
},
|
|
8
9
|
object: {
|
|
9
10
|
deserialize: (v) => JSON.parse(v),
|
|
10
|
-
serialize: (v) =>
|
|
11
|
+
serialize: (v) => {
|
|
12
|
+
// Change events are triggered with a string value
|
|
13
|
+
if (typeof v === "string") {
|
|
14
|
+
return v;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return JSON.stringify(v);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
11
20
|
isEmpty: (v) => {
|
|
12
21
|
const values = Object.values(JSON.parse(v));
|
|
13
22
|
return values.length === 0 || values.every(v => v === "" || v === null);
|
|
@@ -90,7 +99,7 @@ export function useLocalStorage(controller, key, defaultValue, opts) {
|
|
|
90
99
|
const rawValue = storage.getItem(key);
|
|
91
100
|
if (rawValue == null) {
|
|
92
101
|
data.value = defaultValue;
|
|
93
|
-
if (writeDefaults && defaultValue !== null) {
|
|
102
|
+
if (writeDefaults && defaultValue !== null && defaultValue !== undefined) {
|
|
94
103
|
storage.setItem(key, serializer.serialize(defaultValue));
|
|
95
104
|
}
|
|
96
105
|
}
|
|
@@ -116,6 +125,12 @@ export function useLocalStorage(controller, key, defaultValue, opts) {
|
|
|
116
125
|
return serializer.isEmpty(rawValue);
|
|
117
126
|
};
|
|
118
127
|
read();
|
|
128
|
+
useEventListener(controller, window, "storage", (event) => {
|
|
129
|
+
if (event.key === key) {
|
|
130
|
+
console.log(event.newValue, event.oldValue);
|
|
131
|
+
write(event.newValue);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
119
134
|
return {
|
|
120
135
|
get value() {
|
|
121
136
|
return read();
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"ruby on rails",
|
|
10
10
|
"ruby-on-rails"
|
|
11
11
|
],
|
|
12
|
-
"version": "1.0.
|
|
12
|
+
"version": "1.0.1",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": {
|
|
15
15
|
"name": "Sub-Xaero",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@hotwired/stimulus": "^3.0.0",
|
|
38
|
-
"@stimulus-library/utilities": "^1.0.
|
|
38
|
+
"@stimulus-library/utilities": "^1.0.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/chai": "^4.3.5",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"typescript": "^5.1.3",
|
|
56
56
|
"vite": "^4.1.1"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "26a7da01746a2bdf8a2a731b79d37256b789bdc0"
|
|
59
59
|
}
|