Skip to content

Commit 472c982

Browse files
authored
Merge pull request #169 from InhiblabCore/dev
perf: onScopeDispose instead onUnmounted
2 parents f81ab37 + fa2606e commit 472c982

36 files changed

+1997
-3977
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"tsx": "^3.11.0",
5959
"typeit": "^8.7.0",
6060
"typescript": "^5.0.4",
61-
"vite": "^4.2.3",
61+
"vite": "3.0.2",
6262
"vite-plugin-build": "0.7.1",
6363
"vite-plugin-dts": "^2.1.0",
6464
"vitepress": "1.0.0-alpha.60",

packages/hooks/src/useRequest/Fetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Ref } from 'vue'
2-
import { isFunction, isBoolean } from '../utils'
2+
import { isFunction, isBoolean } from './utils'
33
import {
44
UseRequestFetchState,
55
UseRequestOptions,
@@ -116,7 +116,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
116116
)
117117
// Do you want to stop the request
118118
if (stopNow) {
119-
return new Promise(() => {})
119+
return new Promise(() => { })
120120
}
121121

122122
this.setState({
@@ -140,7 +140,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
140140
const requestReturnResponse = (res: any) => {
141141
// The request has been cancelled, and the count will be inconsistent with the currentCount
142142
if (currentCount !== this.count) {
143-
return new Promise(() => {})
143+
return new Promise(() => { })
144144
}
145145
// Format data
146146
const formattedResult = this.options.formatResult ? this.options.formatResult(res) : res
@@ -174,7 +174,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
174174
return requestReturnResponse(servicePromiseResult)
175175
} catch (error) {
176176
if (currentCount !== this.count) {
177-
return new Promise(() => {})
177+
return new Promise(() => { })
178178
}
179179

180180
this.setState({

packages/hooks/src/useRequest/plugins/useCachePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, watchEffect, onUnmounted } from 'vue'
1+
import { ref, watchEffect, onScopeDispose } from 'vue'
22
import { UseRequestPlugin } from '../types'
33
import * as cache from '../utils/cache'
44
import { CachedData } from '../utils/cache'
@@ -56,7 +56,7 @@ const useCachePlugin: UseRequestPlugin<unknown, unknown[]> = (
5656
})
5757
})
5858

59-
onUnmounted(() => {
59+
onScopeDispose(() => {
6060
unSubscribeRef.value?.()
6161
})
6262

packages/hooks/src/useRequest/plugins/useDevtoolsPlugin.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, onMounted, unref, watchEffect } from 'vue'
1+
import { computed, unref, watchEffect } from 'vue'
22

33
import { UseRequestPlugin } from '../types'
44
import { getArrowFunctionName, getFunctionName } from '../devtools/utils'
@@ -25,10 +25,6 @@ const useDevtoolsPlugin: UseRequestPlugin<
2525
}
2626
}
2727

28-
onMounted(() => {
29-
createDevTarget()
30-
})
31-
3228
const processObj = computed(() =>
3329
Object.fromEntries(
3430
Object.entries({ ready, ...rest }).map(([key, value]) => [key, unref(value)]),

packages/hooks/src/useRequest/plugins/useRefreshOnWindowFocusPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, watchEffect, onUnmounted, unref } from "vue";
1+
import { ref, watchEffect, onScopeDispose, unref } from "vue";
22
import type { UseRequestPlugin } from "../types";
33
import limit from "../utils/limit";
44
import subscribeFocus from "../utils/subscribeFocus";
@@ -28,7 +28,7 @@ const useRefreshOnWindowFocusPlugin: UseRequestPlugin<unknown, unknown[]> = (
2828
});
2929
});
3030

31-
onUnmounted(() => {
31+
onScopeDispose(() => {
3232
stopSubscribe();
3333
});
3434

packages/hooks/src/useRequest/useRequestImplement.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ref, reactive, toRefs, onUnmounted, inject, UnwrapRef, watchEffect, computed, isRef, unref } from 'vue'
2-
1+
import { ref, reactive, toRefs, onScopeDispose, inject, UnwrapRef, watchEffect, computed, isRef, unref } from 'vue'
32
import Fetch from './Fetch'
43
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
54
import {
@@ -99,7 +98,7 @@ function useRequestImplement<TData, TParams extends any[]>(
9998
}
10099

101100
// onUnmounted cancel request
102-
onUnmounted(() => {
101+
onScopeDispose(() => {
103102
fetchInstance.cancel()
104103
})
105104

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const isObject = (value: unknown): value is Record<any, any> =>
2+
value !== null && typeof value === 'object'
3+
// eslint-disable-next-line @typescript-eslint/ban-types
4+
export const isFunction = (value: unknown): value is Function => typeof value === 'function'
5+
export const isString = (value: unknown): value is string => typeof value === 'string'
6+
export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean'
7+
export const isNumber = (value: unknown): value is number => typeof value === 'number'
8+
export const isUndef = (value: unknown): value is undefined => typeof value === 'undefined'

packages/hooks/src/useRequest/utils/subscribeFocus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// from swr
2-
import isBrowser from '../../utils/isBrowser'
2+
import isBrowser from './utils'
33
import isDocumentVisible from './isDocumentVisible'
44
import isOnline from './isOnline'
55

6+
67
type Listener = () => void
78

89
const listeners: Listener[] = []

packages/hooks/src/useRequest/utils/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export function canUseDom() {
66
);
77
}
88

9+
const isBrowser = !!(
10+
typeof window !== "undefined" &&
11+
window.document &&
12+
window.document.createElement
13+
);
14+
15+
export default isBrowser;

packages/use-request/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
"module": "./dist/useRequest.es.js",
1313
"types":"./dist/types/index.d.ts" ,
1414
"scripts": {
15+
"sync":"rimraf src & tsx scripts/sync.ts",
1516
"build": "npm run clean && vite build",
1617
"clean": "rimraf dist"
1718
},
1819
"keywords": [],
1920
"dependencies": {
21+
"@vue/devtools-api": "^6.5.0",
2022
"lodash": "^4.17.21"
2123
},
2224
"repository": "https://github.com/InhiblabCore/vue-hooks-plus",

0 commit comments

Comments
 (0)