ag-common 0.0.414 → 0.0.415
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,11 +1,12 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
import { AxiosWrapper } from '../jwt';
|
|
2
3
|
import { CacheItems } from '../routes';
|
|
3
4
|
import { ICallOpenApi } from './types';
|
|
4
|
-
export type TUseCallOpenApiDispatch<A> = (value: A) => A;
|
|
5
5
|
export type TUseCallOpenApi<T> = AxiosWrapper<T> & {
|
|
6
6
|
loaded: boolean;
|
|
7
7
|
loadcount: number;
|
|
8
|
-
|
|
8
|
+
/** internally mutate state, but do not refetch. will automatically bump datetime */
|
|
9
|
+
setData: React.Dispatch<React.SetStateAction<Omit<TUseCallOpenApi<T>, 'reFetch' | 'setData'>>>;
|
|
9
10
|
};
|
|
10
11
|
type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
11
12
|
cacheKey: string;
|
|
@@ -23,5 +24,5 @@ type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
|
23
24
|
* @param p
|
|
24
25
|
* @returns
|
|
25
26
|
*/
|
|
26
|
-
export declare const useCallOpenApi: <T, TDefaultApi>(
|
|
27
|
+
export declare const useCallOpenApi: <T, TDefaultApi>(inConfig: TUseCallOpenApiInt<T, TDefaultApi>) => TUseCallOpenApi<T>;
|
|
27
28
|
export {};
|
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.useCallOpenApi = void 0;
|
|
13
13
|
const react_1 = require("react");
|
|
14
|
+
const useGranularHook_1 = require("../useGranularHook");
|
|
14
15
|
const cached_1 = require("./cached");
|
|
15
16
|
const direct_1 = require("./direct");
|
|
16
17
|
const defaultState = (p,
|
|
@@ -22,42 +23,55 @@ noSsr = false) => {
|
|
|
22
23
|
const cachedData = noSsr
|
|
23
24
|
? undefined
|
|
24
25
|
: (_a = (0, cached_1.callOpenApiCachedRaw)(Object.assign(Object.assign({}, p), { onlyCached: true }))) === null || _a === void 0 ? void 0 : _a.data;
|
|
25
|
-
return Object.assign(Object.assign({ data: undefined,
|
|
26
|
+
return Object.assign(Object.assign({ data: undefined, datetime: 0, loadcount: 0, loading: false }, (cachedData && { data: cachedData })), { loaded: !!cachedData });
|
|
26
27
|
};
|
|
27
28
|
/**
|
|
28
29
|
* hooks+cached call to callOpenApi
|
|
29
30
|
* @param p
|
|
30
31
|
* @returns
|
|
31
32
|
*/
|
|
32
|
-
const useCallOpenApi = (
|
|
33
|
-
|
|
34
|
-
(0, react_1.
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
const useCallOpenApi = (inConfig) => {
|
|
34
|
+
/** response from hook */
|
|
35
|
+
const [resp, setResp] = (0, react_1.useState)(defaultState(inConfig));
|
|
36
|
+
/** config about hook*/
|
|
37
|
+
const [config, setConfig] = (0, react_1.useState)(inConfig);
|
|
38
|
+
(0, useGranularHook_1.useGranularEffect)(() => {
|
|
39
|
+
if (JSON.stringify(config) !== JSON.stringify(inConfig)) {
|
|
40
|
+
setConfig(inConfig);
|
|
41
|
+
setResp(defaultState(inConfig, true));
|
|
37
42
|
}
|
|
38
|
-
}, [
|
|
43
|
+
}, [inConfig], [resp, setResp, config, setConfig]);
|
|
39
44
|
const reFetch = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Object.assign(Object.assign({},
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
const newdate = new Date().getTime();
|
|
46
|
+
const newresp = yield (0, direct_1.callOpenApi)(config);
|
|
47
|
+
setResp((d) => {
|
|
48
|
+
let newState = Object.assign(Object.assign({}, d), { loaded: true, loading: false });
|
|
49
|
+
if (newdate > d.datetime) {
|
|
50
|
+
newState = Object.assign(Object.assign({}, newresp), { loaded: true, loading: false, loadcount: d.loadcount + 1, datetime: newdate });
|
|
51
|
+
}
|
|
52
|
+
if (JSON.stringify(d) !== JSON.stringify(newState)) {
|
|
53
|
+
return newState;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return d;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}), [config]);
|
|
46
60
|
(0, react_1.useEffect)(() => {
|
|
47
|
-
const { error, loaded, loading, loadcount } =
|
|
48
|
-
const ng =
|
|
61
|
+
const { error, loaded, loading, loadcount } = resp;
|
|
62
|
+
const ng = config.disabled || loaded || loading || (error && loadcount > 2);
|
|
49
63
|
if (ng) {
|
|
50
64
|
return;
|
|
51
65
|
}
|
|
52
|
-
|
|
66
|
+
setResp((d) => (Object.assign(Object.assign({}, d), { loading: true })));
|
|
53
67
|
void reFetch();
|
|
54
|
-
}, [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
}, [config.disabled, reFetch, resp]);
|
|
69
|
+
return Object.assign(Object.assign({}, resp), { reFetch, setData: (p) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
//wipe cache, or might revert
|
|
71
|
+
yield (0, cached_1.setOpenApiCacheRaw)(config, undefined);
|
|
72
|
+
setResp(p);
|
|
73
|
+
//ensure datetime is changed, or might get overwritten
|
|
74
|
+
setResp((x) => (Object.assign(Object.assign({}, x), { datetime: new Date().getTime() })));
|
|
75
|
+
}) });
|
|
62
76
|
};
|
|
63
77
|
exports.useCallOpenApi = useCallOpenApi;
|
package/dist/ui/helpers/jwt.d.ts
CHANGED