Skip to content
On this page

watchDebounced

防抖watch

用法

类似于watch,但提供了将应用于回调函数的额外选项debouckmaxWait

ts
import { watchDebounced } from '@mpxjs/mpxuse-core'

watchDebounced(
  source,
  () => { console.log('changed!') },
  { debounce: 500, maxWait: 1000 },
)
import { watchDebounced } from '@mpxjs/mpxuse-core'

watchDebounced(
  source,
  () => { console.log('changed!') },
  { debounce: 500, maxWait: 1000 },
)

它本质上是以下代码的简写:

ts
import { debounceFilter, watchWithFilter } from '@mpxjs/mpxuse-core'

watchWithFilter(
  source,
  () => { console.log('changed!') },
  {
    eventFilter: debounceFilter(500, { maxWait: 1000 }),
  },
)
import { debounceFilter, watchWithFilter } from '@mpxjs/mpxuse-core'

watchWithFilter(
  source,
  () => { console.log('changed!') },
  {
    eventFilter: debounceFilter(500, { maxWait: 1000 }),
  },
)

类型声明

typescript
export interface WatchDebouncedOptions
  extends WatchOptions,
    DebounceFilterOptions {
  debounce?: MaybeComputedRef<number>
}
export declare function watchDebounced<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export declare function watchDebounced<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export declare function watchDebounced<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export { watchDebounced as debouncedWatch }
export interface WatchDebouncedOptions
  extends WatchOptions,
    DebounceFilterOptions {
  debounce?: MaybeComputedRef<number>
}
export declare function watchDebounced<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export declare function watchDebounced<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export declare function watchDebounced<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchDebouncedOptions
): WatchStopHandle
export { watchDebounced as debouncedWatch }

源码

源码文档

贡献者

pagnkelly

更新日志

No recent changes

根据MIT许可证发布。