Skip to content
On this page

watchThrottled

节流 watch.

用法

类似于watch,但提供了一个额外的选项throttle,该选项将应用于回调函数。

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

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

watchThrottled(
  source,
  () => { console.log('changed!') },
  { throttle: 500 },
)

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

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

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

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

类型声明

typescript
export interface WatchThrottledOptions extends WatchOptions {
  throttle?: MaybeComputedRef<number>
  trailing?: boolean
  leading?: boolean
}
export declare function watchThrottled<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchThrottledOptions
): WatchStopHandle
export declare function watchThrottled<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions
): WatchStopHandle
export declare function watchThrottled<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions
): WatchStopHandle
export { watchThrottled as throttledWatch }
export interface WatchThrottledOptions extends WatchOptions {
  throttle?: MaybeComputedRef<number>
  trailing?: boolean
  leading?: boolean
}
export declare function watchThrottled<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchThrottledOptions
): WatchStopHandle
export declare function watchThrottled<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions
): WatchStopHandle
export declare function watchThrottled<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions
): WatchStopHandle
export { watchThrottled as throttledWatch }

源码

源码文档

贡献者

pagnkelly

更新日志

No recent changes

根据MIT许可证发布。