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.
@@ -1,99 +1,118 @@
1
1
  import { useIPC } from "./use-ipc.js";
2
2
 
3
3
  class UseStorage {
4
- constructor(storage, globalKey, initialValue, use_channel = true) {
5
- this.cache = {
6
- storage,
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
- get items() {
16
- const raw = this.cache.storage.getItem(this.cache.globalKey);
17
- if (!raw) return {};
18
- try {
19
- return JSON.parse(raw);
20
- } catch {
21
- return {};
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
- #maintain() {
26
- const items = this.items;
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
- set(data) {
48
- this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
49
- if (this.cache.channel) this.cache.channel.emit("Ziko-Storage-Updated", data);
50
- this.#maintain();
51
- return this;
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
- add(data) {
55
- this.set({
56
- ...this.items,
57
- ...data
58
- });
59
- return this;
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
- remove(...keys) {
62
- const items = { ...this.items };
63
- keys.forEach(key => {
64
- delete items[key];
65
- delete this[key];
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
- get(key) {
72
- return this.items[key];
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
- clear() {
75
- this.cache.storage.removeItem(this.cache.globalKey);
76
- this.cache.oldItemKeys.forEach(k => delete this[k]);
77
- this.cache.oldItemKeys.clear();
78
- this.#maintain();
79
- return this;
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
- onStorageUpdated(callback) {
82
- if (this.cache.channel) {
83
- this.cache.channel.on("Ziko-Storage-Updated", callback);
84
- }
85
- return this;
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
- // factory functions
90
- const useLocaleStorage = (key, initialValue, use_channel = true) =>
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
- new UseStorage(sessionStorage, key, initialValue, use_channel);
113
+ new UseStorage(sessionStorage, key, initialValue, use_channel);
95
114
 
96
115
  export {
97
- useLocaleStorage,
98
- useSessionStorage
99
- };
116
+ useLocalStorage,
117
+ useSessionStorage
118
+ };