@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.
@@ -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
- * 仅在manual=true会返回,否则返回null
73
+ * 初始值为defaultData(默认null);自动模式请求成功后更新为请求结果,手动模式不会更新
74
74
  */
75
75
  data: K;
76
76
  };
@@ -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 = 7;
108
- _context.next = 10;
111
+ _context.prev = 8;
112
+ _context.next = 11;
109
113
  return request.apply(void 0, args);
110
- case 10:
114
+ case 11:
111
115
  _res = _context.sent;
112
- if (mounted.current && id === fetchId.current) {
116
+ if (isCurrent()) {
113
117
  end();
114
- onSuccess === null || onSuccess === void 0 || onSuccess(_res, args);
115
- if (!manual) {
116
- setData(_res);
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 14:
124
- _context.prev = 14;
125
- _context.t0 = _context["catch"](7);
126
- end();
127
- if (onError) {
128
- onError(_context.t0, args, function () {
129
- hooksOpts.showError(_context.t0);
130
- });
131
- } else {
132
- hooksOpts.showError(_context.t0);
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
- onFinally === null || onFinally === void 0 || onFinally();
137
- lockRef.current = false;
147
+ if (isCurrent()) {
148
+ lockRef.current = false;
149
+ onFinally === null || onFinally === void 0 || onFinally();
150
+ }
138
151
  return _context.finish(18);
139
- case 22:
152
+ case 21:
140
153
  case "end":
141
154
  return _context.stop();
142
155
  }
143
- }, _callee, null, [[7, 14, 18, 22]]);
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
- * 仅在manual=true会返回,否则返回null
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.4.27-alpha.0",
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": "c5d28b728f3a559fd51e738b5c490323e6551786"
33
+ "gitHead": "e188887c76431607140aec1982f3d50bf682761f"
34
34
  }