Skip to content

Commit 88ed1a9

Browse files
committed
refactor(platform): optimize api of http request
1 parent 453f13b commit 88ed1a9

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ export function useHttp() {
2828
const location = useLocation();
2929
const { t } = useTranslation();
3030

31-
useUnmount(() => {
32-
for (const abort of dataRef.current.abortFns) {
33-
abort();
34-
}
35-
});
36-
37-
return useEventCallback(
31+
const http = useEventCallback(
3832
<T = any, D = any>(
3933
config: AxiosRequestConfig<D>,
4034
options?: { unmount?: boolean; authorization?: boolean }
@@ -106,4 +100,16 @@ export function useHttp() {
106100
return req as any;
107101
}
108102
);
103+
104+
http['abortAll'] = () => {
105+
for (const abort of dataRef.current.abortFns) {
106+
abort();
107+
}
108+
};
109+
110+
useUnmount(() => {
111+
http['abortAll']();
112+
});
113+
114+
return http as typeof http & { abortAll: () => void };
109115
}

packages/platform/src/app/hooks/useAPI.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://cloud.google.com/apis/design
2-
import { useEventCallback } from '@react-devui/hooks';
2+
import type { useHttp } from '../core';
33

4-
import { useHttp } from '../core';
4+
import { useEventCallback } from '@react-devui/hooks';
55

66
interface StandardListRes<T> {
77
resources: T[];
@@ -12,9 +12,7 @@ interface StandardListRes<T> {
1212
};
1313
}
1414

15-
export function useAPI(url: string) {
16-
const http = useHttp();
17-
15+
export function useAPI(http: ReturnType<typeof useHttp>, url: string) {
1816
return {
1917
list: useEventCallback(<T = any, P = any>(params?: P) =>
2018
http<StandardListRes<T>>({

packages/platform/src/app/routes/list/standard-table/DeviceModal.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import type { DSelectItem } from '@react-devui/ui/components/select';
55
import { isUndefined } from 'lodash';
66
import React, { useImperativeHandle, useState } from 'react';
77

8-
import { useAsync, useEventCallback } from '@react-devui/hooks';
8+
import { useEventCallback } from '@react-devui/hooks';
99
import { FormControl, FormGroup, useForm, Validators } from '@react-devui/ui';
1010
import { DForm, DInput, DModal, DSelect } from '@react-devui/ui';
1111

1212
import { AppResponsiveForm } from '../../../components';
13+
import { useHttp } from '../../../core';
1314
import { useAPI } from '../../../hooks';
1415

1516
export interface AppDeviceModalProps {
@@ -19,8 +20,10 @@ export interface AppDeviceModalProps {
1920
function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSettingFn<DeviceData>>): JSX.Element | null {
2021
const { onSuccess } = props;
2122

22-
const modelApi = useAPI('/device/model');
23-
const async = useAsync();
23+
const http = useHttp();
24+
const httpOfInit = useHttp();
25+
const modelApi = useAPI(http, '/device/model');
26+
const modelApiOfInit = useAPI(httpOfInit, '/device/model');
2427

2528
const [visible, setVisible] = useState(false);
2629
const [device, setDevice] = useState<DeviceData>();
@@ -42,7 +45,7 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
4245
updateForm();
4346

4447
setModelList(undefined);
45-
modelApi.list().subscribe({
48+
modelApiOfInit.list().subscribe({
4649
next: (res) => {
4750
setModelList(
4851
res.resources.map((model) => ({
@@ -70,16 +73,19 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
7073
dOkProps={{ disabled: !form.valid }}
7174
onOkClick={() =>
7275
new Promise((r) => {
73-
async.setTimeout(() => {
74-
onSuccess();
75-
r(true);
76-
}, 500);
76+
modelApi.list().subscribe({
77+
next: () => {
78+
onSuccess();
79+
r(true);
80+
},
81+
});
7782
})
7883
}
7984
></DModal.Footer>
8085
}
8186
dMaskClosable={false}
8287
onClose={() => {
88+
httpOfInit.abortAll();
8389
setVisible(false);
8490
}}
8591
>

packages/platform/src/app/routes/list/standard-table/StandardTable.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import { useEffect, useRef, useState } from 'react';
66
import { useTranslation } from 'react-i18next';
77
import { Link } from 'react-router-dom';
88

9-
import { useAsync, useImmer, useMount } from '@react-devui/hooks';
9+
import { useImmer, useMount } from '@react-devui/hooks';
1010
import { DownOutlined, PlusOutlined } from '@react-devui/icons';
1111
import { DButton, DCard, DCheckbox, DDropdown, DModal, DPagination, DSelect, DSeparator, DSpinner, DTable } from '@react-devui/ui';
1212

1313
import { AppRouteHeader, AppStatusDot, AppTableFilter } from '../../../components';
14+
import { useHttp } from '../../../core';
1415
import { useAPI, useQueryParams } from '../../../hooks';
1516
import { AppRoute } from '../../../utils';
1617
import { AppDeviceModal } from './DeviceModal';
@@ -31,9 +32,9 @@ export default AppRoute(() => {
3132
const deviceModalRef = useRef<OpenSettingFn<DeviceData>>(null);
3233

3334
const { t } = useTranslation();
34-
const async = useAsync();
35-
const modelApi = useAPI('/device/model');
36-
const deviceApi = useAPI('/device');
35+
const http = useHttp();
36+
const modelApi = useAPI(http, '/device/model');
37+
const deviceApi = useAPI(http, '/device');
3738

3839
const [deviceQuerySaved, setDeviceQuerySaved] = useQueryParams<DeviceQueryParams>({
3940
keyword: '',
@@ -126,9 +127,11 @@ export default AppRoute(() => {
126127
<DModal.Footer
127128
onOkClick={() =>
128129
new Promise((r) => {
129-
async.setTimeout(() => {
130-
r(true);
131-
}, 500);
130+
modelApi.list().subscribe({
131+
next: () => {
132+
r(true);
133+
},
134+
});
132135
})
133136
}
134137
/>

packages/platform/src/app/routes/list/standard-table/detail/Detail.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { EditOutlined } from '@react-devui/icons';
1010
import { DButton, DCard, DSeparator, DSpinner, DTable } from '@react-devui/ui';
1111

1212
import { AppDetailView, AppRouteHeader } from '../../../../components';
13+
import { useHttp } from '../../../../core';
1314
import { useAPI } from '../../../../hooks';
1415
import { AppRoute } from '../../../../utils';
1516
import { AppDeviceModal } from '../DeviceModal';
@@ -20,7 +21,8 @@ export default AppRoute(() => {
2021

2122
const { t } = useTranslation();
2223

23-
const deviceApi = useAPI('/device');
24+
const http = useHttp();
25+
const deviceApi = useAPI(http, '/device');
2426

2527
const { id: _id } = useParams();
2628
const id = Number(_id!);

0 commit comments

Comments
 (0)