@dune2/tools 0.6.0 → 0.7.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/package.json +2 -4
- package/src/storage/index.ts +23 -18
- package/src/store/index.ts +88 -0
- package/src/valtio/index.ts +3 -0
- package/src/zustand/index.ts +0 -61
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dune2/tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n"
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"./valtio": "./src/valtio/index.ts",
|
|
33
33
|
"./valtio/*": "./src/valtio/*.ts",
|
|
34
34
|
"./niceModal": "./src/niceModal/index.tsx",
|
|
35
|
-
"./
|
|
35
|
+
"./store": "./src/store/index.ts",
|
|
36
36
|
"./package.json": "./package.json"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
@@ -54,10 +54,8 @@
|
|
|
54
54
|
"@types/js-cookie": "3.0.3",
|
|
55
55
|
"@types/lodash": "^4.17.0",
|
|
56
56
|
"axios": "^1.6.8",
|
|
57
|
-
"immer": "^10",
|
|
58
57
|
"react": "^19",
|
|
59
58
|
"valtio": "^2",
|
|
60
|
-
"zustand": "^5.0.1",
|
|
61
59
|
"zx": "^7.0.8"
|
|
62
60
|
},
|
|
63
61
|
"publishConfig": {
|
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
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import { useDebugValue, useRef, useSyncExternalStore } from "react";
|
|
3
|
+
import { proxy, snapshot, subscribe, useSnapshot } from "valtio";
|
|
4
|
+
|
|
5
|
+
const stores: any = {};
|
|
6
|
+
|
|
7
|
+
if (typeof window !== "undefined") {
|
|
8
|
+
Object.defineProperty(window, "__stores2", {
|
|
9
|
+
get() {
|
|
10
|
+
const r = snapshot(proxy(stores));
|
|
11
|
+
return { ...r, __raw: stores };
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Config<S extends object, A extends Record<string, Action<S, any>>> = {
|
|
17
|
+
name: string;
|
|
18
|
+
state: S;
|
|
19
|
+
actionsCreator: (state: S) => A;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function createStore<
|
|
23
|
+
S extends object,
|
|
24
|
+
A extends Record<string, Action<S, any>>,
|
|
25
|
+
>(config: Config<S, A>) {
|
|
26
|
+
const store = proxy(config.state);
|
|
27
|
+
const initialState = snapshot(store) as S;
|
|
28
|
+
|
|
29
|
+
const actions = config.actionsCreator(store);
|
|
30
|
+
|
|
31
|
+
const api = {
|
|
32
|
+
/**
|
|
33
|
+
* 状态 可以直接修改,会触发状态变化
|
|
34
|
+
*/
|
|
35
|
+
state: store,
|
|
36
|
+
/**
|
|
37
|
+
* 初始状态 快照
|
|
38
|
+
*/
|
|
39
|
+
initialState,
|
|
40
|
+
/**
|
|
41
|
+
* 获取当前状态 快照
|
|
42
|
+
*/
|
|
43
|
+
getState() {
|
|
44
|
+
return snapshot(store) as S;
|
|
45
|
+
},
|
|
46
|
+
actions,
|
|
47
|
+
/**
|
|
48
|
+
* 监听状态变化
|
|
49
|
+
*/
|
|
50
|
+
subscribe(fn: (state: S) => void) {
|
|
51
|
+
return subscribe(store, (ops) => {
|
|
52
|
+
const state = api.getState();
|
|
53
|
+
fn(state);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
/**
|
|
57
|
+
* 获取当前状态 快照
|
|
58
|
+
*/
|
|
59
|
+
useSnapshot() {
|
|
60
|
+
return useSnapshot(store) as S;
|
|
61
|
+
},
|
|
62
|
+
useShallowSnapshot<T>(selector: (state: S) => T) {
|
|
63
|
+
const prev = useRef<T>(undefined);
|
|
64
|
+
const combinedSelector = (state: S) => {
|
|
65
|
+
const next = selector(state);
|
|
66
|
+
if (!_.isEqual(prev.current, next)) {
|
|
67
|
+
prev.current = next;
|
|
68
|
+
}
|
|
69
|
+
return prev.current;
|
|
70
|
+
};
|
|
71
|
+
const slice = useSyncExternalStore(
|
|
72
|
+
api.subscribe,
|
|
73
|
+
() => {
|
|
74
|
+
return combinedSelector(api.getState());
|
|
75
|
+
},
|
|
76
|
+
() => {
|
|
77
|
+
return combinedSelector(api.initialState);
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
useDebugValue(slice);
|
|
81
|
+
return slice;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return api;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
type Action<S, T> = (payload: T) => any;
|
package/src/valtio/index.ts
CHANGED
package/src/zustand/index.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { produce } from "immer";
|
|
2
|
-
import { create } from "zustand";
|
|
3
|
-
import { useShallow } from "zustand/shallow";
|
|
4
|
-
type State<S extends object, A extends Record<string, Action<S, any>>> = {
|
|
5
|
-
state: S;
|
|
6
|
-
actions: A;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export function createStore<
|
|
10
|
-
S extends object,
|
|
11
|
-
A extends Record<string, Action<S, any>>,
|
|
12
|
-
>(config: State<S, A>) {
|
|
13
|
-
const useStore = create<S>((set) => {
|
|
14
|
-
return config.state;
|
|
15
|
-
});
|
|
16
|
-
type NormalActions = {
|
|
17
|
-
[key in keyof A]: (payload: Parameters<A[key]>[1]) => void;
|
|
18
|
-
};
|
|
19
|
-
const normalActions: NormalActions = {} as NormalActions;
|
|
20
|
-
Object.keys(config.actions).forEach((key) => {
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
normalActions[key] = (payload) => {
|
|
23
|
-
useStore.setState(
|
|
24
|
-
produce((draft) => {
|
|
25
|
-
config.actions[key](draft, payload);
|
|
26
|
-
}),
|
|
27
|
-
// 尽可能复用 immer 更新的数据
|
|
28
|
-
true,
|
|
29
|
-
);
|
|
30
|
-
};
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
/**
|
|
35
|
-
* 在组件内获取状态
|
|
36
|
-
*/
|
|
37
|
-
useSnapshot: useStore,
|
|
38
|
-
/**
|
|
39
|
-
* 一般用于传入select,并且 selector 返回的值是对象,
|
|
40
|
-
* 这样就可以浅比较,减少不必要的更新
|
|
41
|
-
*/
|
|
42
|
-
useShallowSnapshot: <U extends object>(select: (state: S) => U) => {
|
|
43
|
-
return useStore(useShallow(select));
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 普通方法,设置状态
|
|
48
|
-
*/
|
|
49
|
-
actions: normalActions,
|
|
50
|
-
/**
|
|
51
|
-
* 监听状态变化
|
|
52
|
-
*/
|
|
53
|
-
subscribe: useStore.subscribe,
|
|
54
|
-
/**
|
|
55
|
-
* 获取状态
|
|
56
|
-
*/
|
|
57
|
-
getState: useStore.getState,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
type Action<S, T> = (state: S, payload: T) => any;
|