提交 88f18483 编写于 作者: fxy060608's avatar fxy060608

fix(app): pass timer to debounce

上级 280e3a14
......@@ -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 })
)
}
......@@ -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) => {
......
......@@ -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)
})
......
......@@ -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) => {
......
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
}
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册