From 88f1848304e1bb95fea1fb2e6ee771a83093df74 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 19 Apr 2022 20:07:51 +0800 Subject: [PATCH] fix(app): pass timer to debounce --- .../framework/webview/init/event/resize.ts | 5 ++++- .../uni-components/src/helpers/useField.ts | 10 +++++++--- packages/uni-h5/src/framework/setup/index.ts | 5 ++++- .../chooseLocation/LoctaionPicker.tsx | 16 ++++++++++------ packages/uni-shared/src/debounce.ts | 19 +++++++++++++++---- packages/vite-plugin-uni/src/utils/easycom.ts | 2 +- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/uni-app-plus/src/service/framework/webview/init/event/resize.ts b/packages/uni-app-plus/src/service/framework/webview/init/event/resize.ts index 9df1dc69c..931b3cf25 100644 --- a/packages/uni-app-plus/src/service/framework/webview/init/event/resize.ts +++ b/packages/uni-app-plus/src/service/framework/webview/init/event/resize.ts @@ -19,5 +19,8 @@ export function onWebviewResize(webview: PlusWebviewWebviewObject) { } emit(ON_RESIZE, res, parseInt(webview.id!)) // Page lifecycle } - webview.addEventListener('resize' as any, debounce(onResize, 50)) + webview.addEventListener( + 'resize' as any, + debounce(onResize, 50, { setTimeout, clearTimeout }) + ) } diff --git a/packages/uni-components/src/helpers/useField.ts b/packages/uni-components/src/helpers/useField.ts index 505c63a31..d14713e73 100644 --- a/packages/uni-components/src/helpers/useField.ts +++ b/packages/uni-components/src/helpers/useField.ts @@ -229,9 +229,13 @@ function useValueSync( emit: SetupContext['emit'], trigger: CustomEventTrigger ) { - const valueChangeFn = debounce((val: any) => { - state.value = getValueString(val, props.type) - }, 100) + const valueChangeFn = debounce( + (val: any) => { + state.value = getValueString(val, props.type) + }, + 100, + { setTimeout, clearTimeout } + ) watch(() => props.modelValue, valueChangeFn) watch(() => props.value, valueChangeFn) const triggerInputFn = throttle((event: Event, detail: InputEventDetail) => { diff --git a/packages/uni-h5/src/framework/setup/index.ts b/packages/uni-h5/src/framework/setup/index.ts index 28eaac14e..d253849c1 100644 --- a/packages/uni-h5/src/framework/setup/index.ts +++ b/packages/uni-h5/src/framework/setup/index.ts @@ -172,7 +172,10 @@ export function setupApp(comp: any) { onBeforeMount(onLaunch) } onMounted(() => { - window.addEventListener('resize', debounce(onResize, 50)) + window.addEventListener( + 'resize', + debounce(onResize, 50, { setTimeout, clearTimeout }) + ) window.addEventListener('message', onMessage) document.addEventListener('visibilitychange', onVisibilityChange) }) diff --git a/packages/uni-h5/src/service/api/location/chooseLocation/LoctaionPicker.tsx b/packages/uni-h5/src/service/api/location/chooseLocation/LoctaionPicker.tsx index 9bc444dea..e296eeaea 100644 --- a/packages/uni-h5/src/service/api/location/chooseLocation/LoctaionPicker.tsx +++ b/packages/uni-h5/src/service/api/location/chooseLocation/LoctaionPicker.tsx @@ -220,12 +220,16 @@ export default /*#__PURE__*/ defineSystemComponent({ const { t } = useI18n() const state = useState(props) const { list, listState, loadMore, reset, getList } = useList(state) - const search = debounce(() => { - reset() - if (state.keyword) { - getList() - } - }, 1000) + const search = debounce( + () => { + reset() + if (state.keyword) { + getList() + } + }, + 1000, + { setTimeout, clearTimeout } + ) watch( () => state.searching, (val) => { diff --git a/packages/uni-shared/src/debounce.ts b/packages/uni-shared/src/debounce.ts index 5f2970a2b..b42b37743 100644 --- a/packages/uni-shared/src/debounce.ts +++ b/packages/uni-shared/src/debounce.ts @@ -1,12 +1,23 @@ -export function debounce(fn: Function, delay: number) { +interface Timer { + setTimeout: Function + clearTimeout: Function +} +/** + * 需要手动传入 timer,主要是解决 App 平台的定制 timer + * @param fn + * @param delay + * @param timer + * @returns + */ +export function debounce(fn: Function, delay: number, timer: Timer) { let timeout: any const newFn = function (this: any) { - clearTimeout(timeout) + timer.clearTimeout(timeout) const timerFn = () => fn.apply(this, arguments) - timeout = setTimeout(timerFn, delay) + timeout = timer.setTimeout(timerFn, delay) } newFn.cancel = function () { - clearTimeout(timeout) + timer.clearTimeout(timeout) } return newFn } diff --git a/packages/vite-plugin-uni/src/utils/easycom.ts b/packages/vite-plugin-uni/src/utils/easycom.ts index 6a23d6288..d655956ae 100644 --- a/packages/vite-plugin-uni/src/utils/easycom.ts +++ b/packages/vite-plugin-uni/src/utils/easycom.ts @@ -26,7 +26,7 @@ export const initEasycom = (watcher?: FSWatcher) => { disableGlobbing: true, }) } - const refreshEasycom = debounce(refresh, 100) + const refreshEasycom = debounce(refresh, 100, { setTimeout, clearTimeout }) watcher.on('all', (eventName, path) => { if (!['add', 'unlink'].includes(eventName)) { return -- GitLab