Skip to content

Commit f695966

Browse files
committed
perf: createGlobalState export set method without state
1 parent 098af66 commit f695966

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { ImmerHook } from './useImmer';
1+
import type { ImmerHook, Updater } from './useImmer';
22

33
import { freeze, produce } from 'immer';
44
import { useState } from 'react';
55

66
import { useUnmount } from './useUnmount';
77

8-
export function createGlobalState<S>(): () => ImmerHook<S | undefined>;
9-
export function createGlobalState<S>(initialValue: S): () => ImmerHook<S>;
10-
export function createGlobalState<S>(initialValue?: S): () => ImmerHook<S | undefined> {
8+
export function createGlobalState<S>(): { (): ImmerHook<S | undefined>; set: Updater<S | undefined> };
9+
export function createGlobalState<S>(initialValue: S): { (): ImmerHook<S>; set: Updater<S> };
10+
export function createGlobalState<S>(initialValue?: S): { (): ImmerHook<S | undefined>; set: Updater<S | undefined> } {
1111
const store = {
1212
state: freeze(typeof initialValue === 'function' ? initialValue() : initialValue, true),
1313
setState(updater: any) {
@@ -24,17 +24,22 @@ export function createGlobalState<S>(initialValue?: S): () => ImmerHook<S | unde
2424
updates: new Set<(...args: any[]) => any>(),
2525
};
2626

27-
return () => {
28-
const [state, setState] = useState(store.state);
27+
return Object.assign<any, any>(
28+
() => {
29+
const [state, setState] = useState(store.state);
2930

30-
if (!store.updates.has(setState)) {
31-
store.updates.add(setState);
32-
}
31+
if (!store.updates.has(setState)) {
32+
store.updates.add(setState);
33+
}
3334

34-
useUnmount(() => {
35-
store.updates.delete(setState);
36-
});
35+
useUnmount(() => {
36+
store.updates.delete(setState);
37+
});
3738

38-
return [state, store.setState];
39-
};
39+
return [state, store.setState];
40+
},
41+
{
42+
set: store.setState,
43+
}
44+
);
4045
}

packages/platform/src/app/core/http/useHttp.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export function useHttp() {
2828
const navigate = useNavigate();
2929
const location = useLocation();
3030
const { t } = useTranslation();
31-
const [, setToasts] = useToasts();
3231

3332
useUnmount(() => {
3433
for (const abort of dataRef.current.abortFns) {
@@ -70,18 +69,28 @@ export function useHttp() {
7069
if (error.response) {
7170
switch (error.response.status) {
7271
case 401:
73-
setToasts((draft) => {
72+
useToasts.set((draft) => {
7473
const key = getGlobalKey();
7574
draft.push({
7675
key,
7776
children: t('User not authorized'),
7877
dVisible: true,
7978
dType: 'error',
8079
onClose: () => {
81-
setToasts((draft) => {
80+
useToasts.set((draft) => {
8281
draft.find((n) => n.key === key)!.dVisible = false;
8382
});
8483
},
84+
afterVisibleChange: (visible) => {
85+
if (!visible) {
86+
useToasts.set((draft) => {
87+
draft.splice(
88+
draft.findIndex((n) => n.key === key),
89+
1
90+
);
91+
});
92+
}
93+
},
8594
});
8695
});
8796
navigate(LOGIN_PATH, { state: { [PREV_ROUTE_KEY]: location } });

packages/platform/src/app/core/useInit.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ export function useInit() {
1010
const http = useHttp();
1111
const acl = useACL();
1212

13-
const [, setUser] = useUserState();
14-
const [, setNotification] = useNotificationState();
15-
16-
const [, setMenu] = useMenu();
17-
1813
const refreshToken = useRefreshToken();
1914

2015
const handleUser = (user: UserState) => {
21-
setUser(user);
16+
useUserState.set(user);
2217

2318
//#region ACL
2419
acl.setFull(user.role === 'admin');
@@ -27,7 +22,7 @@ export function useInit() {
2722
};
2823

2924
const getNotification = () => {
30-
setNotification(undefined);
25+
useNotificationState.set(undefined);
3126
http<NotificationItem[]>(
3227
{
3328
url: '/notification',
@@ -36,13 +31,13 @@ export function useInit() {
3631
{ unmount: false }
3732
).subscribe({
3833
next: (res) => {
39-
setNotification(res);
34+
useNotificationState.set(res);
4035
},
4136
});
4237
};
4338

4439
const resetMenu = () => {
45-
setMenu((draft) => {
40+
useMenu.set((draft) => {
4641
draft.expands = undefined;
4742
});
4843
};

packages/platform/src/app/core/useMenu.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ export function useMenu() {
9595
setMenuState,
9696
] as const;
9797
}
98+
useMenu.set = useMenuState.set;

0 commit comments

Comments
 (0)