Skip to content

Commit

Permalink
Merge pull request #943 from alibaba/fix/use-request-hrm
Browse files Browse the repository at this point in the history
fix: useRequest support hrm
  • Loading branch information
brickspert authored May 8, 2021
2 parents f659b78 + bc901e2 commit 6ccad56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/use-request/src/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class Fetch<R, P extends any[]> {
// 请求时序
count = 0;

// 是否卸载
unmountedFlag = false;

// visible 后,是否继续轮询
pollingWhenVisibleFlag = false;

Expand Down Expand Up @@ -133,7 +130,7 @@ class Fetch<R, P extends any[]> {

return this.service(...args)
.then((res) => {
if (this.unmountedFlag || currentCount !== this.count) {
if (currentCount !== this.count) {
// prevent run.then when request is canceled
return new Promise(() => {});
}
Expand All @@ -152,7 +149,7 @@ class Fetch<R, P extends any[]> {
return formattedResult;
})
.catch((error) => {
if (this.unmountedFlag || currentCount !== this.count) {
if (currentCount !== this.count) {
// prevent run.then when request is canceled
return new Promise(() => {});
}
Expand All @@ -179,7 +176,7 @@ class Fetch<R, P extends any[]> {
);
})
.finally(() => {
if (!this.unmountedFlag && currentCount === this.count) {
if (currentCount === this.count) {
if (this.config.pollingInterval) {
// 如果屏幕隐藏,并且 !pollingWhenHidden, 则停止轮询,并记录 flag,等 visible 时,继续轮询
if (!isDocumentVisible() && !this.config.pollingWhenHidden) {
Expand Down Expand Up @@ -253,7 +250,6 @@ class Fetch<R, P extends any[]> {
}

unmount() {
this.unmountedFlag = true;
this.cancel();
this.unsubscribe.forEach((s) => {
s();
Expand Down
6 changes: 6 additions & 0 deletions packages/use-request/src/utils/useUpdateEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { useEffect, useRef } from 'react';
const useUpdateEffect: typeof useEffect = (effect, deps) => {
const isMounted = useRef(false);

useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);

useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
Expand Down

0 comments on commit 6ccad56

Please sign in to comment.