@dune2/tools 0.7.0 → 0.7.2
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/package.json +1 -1
- package/src/storage/index.ts +23 -18
- package/src/store/index.ts +10 -2
package/package.json
CHANGED
package/src/storage/index.ts
CHANGED
|
@@ -69,39 +69,44 @@ class StorageHelper<V = any> {
|
|
|
69
69
|
v === undefined
|
|
70
70
|
? this.store.remove(this.baseKey)
|
|
71
71
|
: this.store.set(this.baseKey, v);
|
|
72
|
-
|
|
73
|
-
// On localStorage.setItem, the storage event is only triggered on other tabs and windows.
|
|
74
|
-
// So we manually dispatch a storage event to trigger the subscribe function on the current window as well.
|
|
75
|
-
window.dispatchEvent(
|
|
76
|
-
new StorageEvent("storage", {
|
|
77
|
-
key: this.key,
|
|
78
|
-
// 这里 value 不重要,在内部会使用 get 重新获取值
|
|
79
|
-
newValue: null,
|
|
80
|
-
}),
|
|
81
|
-
);
|
|
82
|
-
}
|
|
72
|
+
this.notifyListeners();
|
|
83
73
|
}
|
|
84
74
|
|
|
85
75
|
remove(): void {
|
|
86
76
|
this.set(undefined);
|
|
87
77
|
}
|
|
88
78
|
|
|
79
|
+
private listeners = new Set<() => void>();
|
|
80
|
+
private notifyListeners = () => {
|
|
81
|
+
this.listeners.forEach((listener) => listener());
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* 订阅 storage 事件
|
|
85
|
+
* @param listener
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
subscribe = (listener: () => void) => {
|
|
89
|
+
// for current window
|
|
90
|
+
this.listeners.add(listener);
|
|
91
|
+
// for other windows
|
|
92
|
+
window.addEventListener("storage", listener);
|
|
93
|
+
|
|
94
|
+
return () => {
|
|
95
|
+
this.listeners.delete(listener);
|
|
96
|
+
window.removeEventListener("storage", listener);
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
89
100
|
/**
|
|
90
101
|
* 这是 react hooks 的 useValue 的实现
|
|
91
102
|
*/
|
|
92
103
|
useValue() {
|
|
93
104
|
return useSyncExternalStore(
|
|
94
|
-
this.
|
|
105
|
+
this.subscribe,
|
|
95
106
|
this.useSyncExternalStoreGetSnapshot,
|
|
96
107
|
this.useSyncExternalStoreGetSnapshot,
|
|
97
108
|
);
|
|
98
109
|
}
|
|
99
|
-
private useSyncExternalStoreSubscribe(listener: () => void) {
|
|
100
|
-
window.addEventListener("storage", listener);
|
|
101
|
-
return () => {
|
|
102
|
-
window.removeEventListener("storage", listener);
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
110
|
private useSyncExternalStoreGetSnapshot = this.get.bind(this);
|
|
106
111
|
}
|
|
107
112
|
|
package/src/store/index.ts
CHANGED
|
@@ -7,8 +7,14 @@ const stores: any = {};
|
|
|
7
7
|
if (typeof window !== "undefined") {
|
|
8
8
|
Object.defineProperty(window, "__stores2", {
|
|
9
9
|
get() {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
let s: any = {};
|
|
11
|
+
Object.keys(stores).forEach((key) => {
|
|
12
|
+
const store = stores[key];
|
|
13
|
+
const state = store.getState();
|
|
14
|
+
s[key] = state;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return { ...s, __raw: stores };
|
|
12
18
|
},
|
|
13
19
|
});
|
|
14
20
|
}
|
|
@@ -82,6 +88,8 @@ export function createStore<
|
|
|
82
88
|
},
|
|
83
89
|
};
|
|
84
90
|
|
|
91
|
+
stores[config.name] = api;
|
|
92
|
+
|
|
85
93
|
return api;
|
|
86
94
|
}
|
|
87
95
|
|