Skip to content

refThrottled fix: 🧩 fixed reference issues for complex data types #3705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Prev Previous commit
Next Next commit
feat: 🚀 adding a configuration object
  • Loading branch information
17359898647 committed Jan 11, 2024
commit 644fa28e79f6bf2b288fccddca29866243980646
21 changes: 17 additions & 4 deletions packages/shared/refThrottled/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Ref } from 'vue-demi'
import { watch } from 'vue-demi'
import type { Ref, WatchOptions } from 'vue-demi'
import { isRef, watch } from 'vue-demi'
import { cloneFnJSON, useCloned } from '../../core/useCloned'
import { useThrottleFn } from '../useThrottleFn'

Expand All @@ -18,7 +18,20 @@ function isReferenceType(value: any) {
* @param [immediate] default true, Whether to execute immediately
* @param [cloneHandler] By default, it use `JSON.parse(JSON.stringify(value))` to clone
*/
export function refThrottled<T>(value: Ref<T>, delay = 200, trailing = true, leading = true, deep = true, immediate = true, cloneHandler = cloneFnJSON) {
interface RefThrottledOptions<T> extends Omit<WatchOptions, 'immediate' | 'deep'> {
origin: Ref<T>
delay?: number
trailing?: boolean
leading?: boolean
cloneHandler?: (value: T) => T
}

export function refThrottled<T>(value: Ref<T>, delay?: number, trailing?: boolean, leading?: boolean, deep?: boolean, immediate?: boolean, cloneHandler?: (value: T) => T): Ref<T>
export function refThrottled<T>(options: RefThrottledOptions<T>): Ref<T>
export function refThrottled<T>(...args: any[]): Ref<T> {
const isFirstRef = isRef(args[0])
const value = isFirstRef ? args[0] : args[0].origin
const [delay = 200, trailing = true, leading = true, deep = true, immediate = true, cloneHandler = cloneFnJSON] = isFirstRef ? args.slice(1) : args[0]
if (delay <= 0)
return value
const isEqualityClone = cloneHandler === cloneFnJSON
Expand Down Expand Up @@ -50,7 +63,7 @@ export function refThrottled<T>(value: Ref<T>, delay = 200, trailing = true, lea
deep,
})

return throttled
return throttled as Ref<T>
}

// alias
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy