@lemon-fe/hooks 1.4.27-alpha.0 → 1.6.6
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/es/use-request/index.d.ts +2 -2
- package/es/use-request/index.js +43 -30
- package/package.json +2 -2
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export default function useRequest<T extends (...args: any[]) => Promise<any> | any, K = T extends (...args: any) => Promise<infer U> | infer U ? U : never>(request: T, options?: {
|
|
9
9
|
/**
|
|
10
|
-
* loading
|
|
10
|
+
* loading状态变化监听函数,禁止在onLoading内调用当前useRequest返回的run
|
|
11
11
|
*/
|
|
12
12
|
onLoading?: (loading: boolean) => void;
|
|
13
13
|
/**
|
|
@@ -70,7 +70,7 @@ export default function useRequest<T extends (...args: any[]) => Promise<any> |
|
|
|
70
70
|
*/
|
|
71
71
|
loading: boolean;
|
|
72
72
|
/**
|
|
73
|
-
*
|
|
73
|
+
* 初始值为defaultData(默认null);自动模式请求成功后更新为请求结果,手动模式不会更新
|
|
74
74
|
*/
|
|
75
75
|
data: K;
|
|
76
76
|
};
|
package/es/use-request/index.js
CHANGED
|
@@ -81,6 +81,7 @@ export default function useRequest(request) {
|
|
|
81
81
|
}, []);
|
|
82
82
|
var runRef = useLastestRef( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
83
83
|
var id,
|
|
84
|
+
isCurrent,
|
|
84
85
|
_len,
|
|
85
86
|
args,
|
|
86
87
|
_key,
|
|
@@ -99,48 +100,60 @@ export default function useRequest(request) {
|
|
|
99
100
|
lockRef.current = true;
|
|
100
101
|
}
|
|
101
102
|
fetchId.current += 1;
|
|
102
|
-
start();
|
|
103
103
|
id = fetchId.current;
|
|
104
|
+
isCurrent = function isCurrent() {
|
|
105
|
+
return id === fetchId.current;
|
|
106
|
+
};
|
|
107
|
+
start();
|
|
104
108
|
for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
105
109
|
args[_key] = _args[_key];
|
|
106
110
|
}
|
|
107
|
-
_context.prev =
|
|
108
|
-
_context.next =
|
|
111
|
+
_context.prev = 8;
|
|
112
|
+
_context.next = 11;
|
|
109
113
|
return request.apply(void 0, args);
|
|
110
|
-
case
|
|
114
|
+
case 11:
|
|
111
115
|
_res = _context.sent;
|
|
112
|
-
if (
|
|
116
|
+
if (isCurrent()) {
|
|
113
117
|
end();
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
if (mounted.current) {
|
|
119
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(_res, args);
|
|
120
|
+
|
|
121
|
+
// 和onSuccess触发时机一致,所以可以忽略onSuccess里是否会重新发起调用
|
|
122
|
+
if (!manual) {
|
|
123
|
+
setData(_res);
|
|
124
|
+
}
|
|
117
125
|
}
|
|
118
|
-
} else if (!mounted.current) {
|
|
119
|
-
end();
|
|
120
126
|
}
|
|
121
127
|
_context.next = 18;
|
|
122
128
|
break;
|
|
123
|
-
case
|
|
124
|
-
_context.prev =
|
|
125
|
-
_context.t0 = _context["catch"](
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
case 15:
|
|
130
|
+
_context.prev = 15;
|
|
131
|
+
_context.t0 = _context["catch"](8);
|
|
132
|
+
if (isCurrent()) {
|
|
133
|
+
end();
|
|
134
|
+
if (mounted.current) {
|
|
135
|
+
if (onError) {
|
|
136
|
+
onError(_context.t0, args, function () {
|
|
137
|
+
// 这里是作为辅助函数给onError内展示异常,所以可以认为不需要isCurrent();
|
|
138
|
+
hooksOpts.showError(_context.t0);
|
|
139
|
+
});
|
|
140
|
+
} else {
|
|
141
|
+
hooksOpts.showError(_context.t0);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
133
144
|
}
|
|
134
145
|
case 18:
|
|
135
146
|
_context.prev = 18;
|
|
136
|
-
|
|
137
|
-
|
|
147
|
+
if (isCurrent()) {
|
|
148
|
+
lockRef.current = false;
|
|
149
|
+
onFinally === null || onFinally === void 0 || onFinally();
|
|
150
|
+
}
|
|
138
151
|
return _context.finish(18);
|
|
139
|
-
case
|
|
152
|
+
case 21:
|
|
140
153
|
case "end":
|
|
141
154
|
return _context.stop();
|
|
142
155
|
}
|
|
143
|
-
}, _callee, null, [[
|
|
156
|
+
}, _callee, null, [[8, 15, 18, 21]]);
|
|
144
157
|
})));
|
|
145
158
|
var formatedRun = useMemo(function () {
|
|
146
159
|
var cb = function cb() {
|
|
@@ -156,11 +169,6 @@ export default function useRequest(request) {
|
|
|
156
169
|
}
|
|
157
170
|
return cb;
|
|
158
171
|
}, []);
|
|
159
|
-
useEffect(function () {
|
|
160
|
-
if (!manual) {
|
|
161
|
-
runRef.current();
|
|
162
|
-
}
|
|
163
|
-
}, refreshDeps);
|
|
164
172
|
useEffect(function () {
|
|
165
173
|
mounted.current = true;
|
|
166
174
|
return function () {
|
|
@@ -168,6 +176,11 @@ export default function useRequest(request) {
|
|
|
168
176
|
end();
|
|
169
177
|
};
|
|
170
178
|
}, []);
|
|
179
|
+
useEffect(function () {
|
|
180
|
+
if (!manual) {
|
|
181
|
+
runRef.current();
|
|
182
|
+
}
|
|
183
|
+
}, refreshDeps);
|
|
171
184
|
return {
|
|
172
185
|
/**
|
|
173
186
|
* 手动执行函数
|
|
@@ -178,7 +191,7 @@ export default function useRequest(request) {
|
|
|
178
191
|
*/
|
|
179
192
|
loading: loading,
|
|
180
193
|
/**
|
|
181
|
-
*
|
|
194
|
+
* 初始值为defaultData(默认null);自动模式请求成功后更新为请求结果,手动模式不会更新
|
|
182
195
|
*/
|
|
183
196
|
data: data
|
|
184
197
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lemon-fe/hooks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.6",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "lusj",
|
|
6
6
|
"homepage": "",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"antd": "^4.24.8",
|
|
31
31
|
"react": "^17.0.2"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "e188887c76431607140aec1982f3d50bf682761f"
|
|
34
34
|
}
|