ziko 1.1.1 → 1.3.0
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/ziko.cjs +196 -114
- package/dist/ziko.js +196 -114
- package/dist/ziko.min.js +2 -2
- package/dist/ziko.mjs +194 -114
- package/package.json +1 -1
- package/src/__ziko__/__state__.js +10 -10
- package/src/dom/components/index.js +1 -0
- package/src/dom/components/swap/index.d.ts +80 -0
- package/src/dom/components/swap/index.js +59 -0
- package/src/dom/index.js +3 -1
- package/src/hooks/use-state.js +30 -28
- package/src/hooks/use-storage.js +99 -80
package/src/hooks/use-storage.js
CHANGED
|
@@ -1,99 +1,118 @@
|
|
|
1
1
|
import { useIPC } from "./use-ipc.js";
|
|
2
2
|
|
|
3
3
|
class UseStorage {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
globalKey,
|
|
8
|
-
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
9
|
-
oldItemKeys: new Set()
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
this.#init(initialValue, use_channel);
|
|
13
|
-
}
|
|
4
|
+
static RESERVED_KEYS = new Set([
|
|
5
|
+
"cache", "items", "set", "add", "remove", "get", "clear", "onStorageUpdated"
|
|
6
|
+
]);
|
|
14
7
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}
|
|
8
|
+
constructor(storage, globalKey, initialValue, use_channel = true) {
|
|
9
|
+
this.cache = {
|
|
10
|
+
storage,
|
|
11
|
+
globalKey,
|
|
12
|
+
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
13
|
+
oldItemKeys: new Set()
|
|
14
|
+
};
|
|
24
15
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.cache.oldItemKeys.forEach(k => delete this[k]);
|
|
28
|
-
for (const key in items) {
|
|
29
|
-
this[key] = items[key];
|
|
30
|
-
this.cache.oldItemKeys.add(key);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
#init(initialValue, use_channel) {
|
|
34
|
-
if (use_channel && this.cache.channel) this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
35
|
-
if (!initialValue) {
|
|
36
|
-
this.#maintain();
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (this.cache.storage.getItem(this.cache.globalKey)) {
|
|
40
|
-
const existing = this.items;
|
|
41
|
-
Object.keys(existing).forEach(k => this.cache.oldItemKeys.add(k));
|
|
42
|
-
this.#maintain();
|
|
43
|
-
}
|
|
44
|
-
else this.set(initialValue);
|
|
45
|
-
}
|
|
16
|
+
this.#init(initialValue, use_channel);
|
|
17
|
+
}
|
|
46
18
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
19
|
+
get items() {
|
|
20
|
+
const raw = this.cache.storage.getItem(this.cache.globalKey);
|
|
21
|
+
if (!raw) return {};
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(raw);
|
|
24
|
+
} catch {
|
|
25
|
+
return {};
|
|
52
26
|
}
|
|
27
|
+
}
|
|
53
28
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
29
|
+
#maintain() {
|
|
30
|
+
const currentItems = this.items;
|
|
31
|
+
const currentKeys = new Set(Object.keys(currentItems));
|
|
32
|
+
|
|
33
|
+
// Cleanup keys that were removed
|
|
34
|
+
this.cache.oldItemKeys.forEach(key => {
|
|
35
|
+
if (!currentKeys.has(key)) {
|
|
36
|
+
delete this[key];
|
|
37
|
+
this.cache.oldItemKeys.delete(key);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Populate keys from storage onto `this`
|
|
42
|
+
for (const key in currentItems) {
|
|
43
|
+
if (!UseStorage.RESERVED_KEYS.has(key)) {
|
|
44
|
+
this[key] = currentItems[key];
|
|
45
|
+
this.cache.oldItemKeys.add(key);
|
|
46
|
+
}
|
|
60
47
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this.cache.oldItemKeys.delete(key);
|
|
67
|
-
});
|
|
68
|
-
this.set(items);
|
|
69
|
-
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#init(initialValue, use_channel) {
|
|
51
|
+
if (use_channel && this.cache.channel) {
|
|
52
|
+
this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
70
53
|
}
|
|
71
|
-
|
|
72
|
-
|
|
54
|
+
|
|
55
|
+
const hasStoredData = this.cache.storage.getItem(this.cache.globalKey) !== null;
|
|
56
|
+
|
|
57
|
+
if (hasStoredData) {
|
|
58
|
+
// Key already exists in storage -> Restore data to instance
|
|
59
|
+
this.#maintain();
|
|
60
|
+
} else if (initialValue !== undefined) {
|
|
61
|
+
// Key doesn't exist -> Seed with initialValue
|
|
62
|
+
this.set(initialValue);
|
|
63
|
+
} else {
|
|
64
|
+
// Key doesn't exist and no initialValue provided -> Default to empty
|
|
65
|
+
this.#maintain();
|
|
73
66
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
set(data) {
|
|
70
|
+
this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
|
|
71
|
+
if (this.cache.channel) {
|
|
72
|
+
this.cache.channel.emit("Ziko-Storage-Updated", data);
|
|
80
73
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
74
|
+
this.#maintain();
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
add(data) {
|
|
79
|
+
return this.set({
|
|
80
|
+
...this.items,
|
|
81
|
+
...data
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
remove(...keys) {
|
|
86
|
+
const items = { ...this.items };
|
|
87
|
+
keys.forEach(key => delete items[key]);
|
|
88
|
+
return this.set(items);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get(key) {
|
|
92
|
+
return this.items[key];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
clear() {
|
|
96
|
+
this.cache.storage.removeItem(this.cache.globalKey);
|
|
97
|
+
this.#maintain();
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
onStorageUpdated(callback) {
|
|
102
|
+
if (this.cache.channel) {
|
|
103
|
+
this.cache.channel.on("Ziko-Storage-Updated", callback);
|
|
86
104
|
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
87
107
|
}
|
|
88
108
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
109
|
+
const useLocalStorage = (key, initialValue, use_channel = true) =>
|
|
110
|
+
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
92
111
|
|
|
93
112
|
const useSessionStorage = (key, initialValue, use_channel = true) =>
|
|
94
|
-
|
|
113
|
+
new UseStorage(sessionStorage, key, initialValue, use_channel);
|
|
95
114
|
|
|
96
115
|
export {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
};
|
|
116
|
+
useLocalStorage,
|
|
117
|
+
useSessionStorage
|
|
118
|
+
};
|