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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: 🧩 parameter processing
  • Loading branch information
17359898647 committed Jan 11, 2024
commit e40f51b7760abd75c1a4473696189f31bc76ad6c
19 changes: 18 additions & 1 deletion packages/shared/refThrottled/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('refThrottled', () => {
it('should handle string type', async () => {
const strRef = ref('Hello')
const throttledRef = refThrottled(strRef, 100)

expect(throttledRef.value).toBe('Hello')

strRef.value = 'World'
Expand Down Expand Up @@ -71,4 +70,22 @@ describe('refThrottled', () => {
expect(throttledRef?.value).toEqual(objRef.value)
expect(error).toBeNull()
})

it('should handle object options', async () => {
const strRef = ref('Hello')
const throttledRef = refThrottled(strRef, 100)
const objParamsThrottledRef = refThrottled({
value: strRef,
delay: 100,
})
expect(throttledRef.value).toBe('Hello')
expect(objParamsThrottledRef.value).toBe('Hello')
strRef.value = 'World'
expect(throttledRef.value).toBe('Hello')
expect(objParamsThrottledRef.value).toBe('Hello')
await new Promise(resolve => setTimeout(resolve, 200))

expect(throttledRef.value).toBe('World')
expect(objParamsThrottledRef.value).toBe('World')
})
})
28 changes: 24 additions & 4 deletions packages/shared/refThrottled/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { useThrottleFn } from '../useThrottleFn'
function isReferenceType(value: any) {
return value !== null && (typeof value === 'object' || typeof value === 'function')
}
interface RefThrottledOptions<T> extends Pick<WatchOptions, 'immediate' | 'deep'> {
interface RefThrottledOptions<T> extends WatchOptions {
/**
* Ref value to be watched with throttle effect.
*/
origin: Ref<T>
value: Ref<T>
/**
* A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
*
Expand Down Expand Up @@ -51,8 +51,27 @@ export function refThrottled<T>(value: Ref<T>, delay?: number, trailing?: boolea
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]
const value = isFirstRef ? args[0] : args[0].value
// const [delay = 200, trailing = true, leading = true, deep = true, immediate = true, cloneHandler = cloneFnJSON] = isFirstRef ? args.slice(1) : args[0]
let delay: number
let trailing: boolean
let leading: boolean
let deep: boolean
let immediate: boolean
let cloneHandler: (value: T) => T
if (isFirstRef) {
[delay = 200, trailing = true, leading = true, deep = true, immediate = true, cloneHandler = cloneFnJSON] = args.slice(1)
}
else {
const { delay: pDelay = 200, trailing: pTrailing = true, leading: pLeading = true, deep: pDeep = true, immediate: pImmediate = true, cloneHandler: pCloneHandler = cloneFnJSON } = args[0]
delay = pDelay
trailing = pTrailing
leading = pLeading
deep = pDeep
immediate = pImmediate
cloneHandler = pCloneHandler
}

if (delay <= 0)
return value
const isEqualityClone = cloneHandler === cloneFnJSON
Expand All @@ -74,6 +93,7 @@ export function refThrottled<T>(...args: any[]): Ref<T> {
}
}
},
...(isFirstRef ? {} : args[0]),
deep,
immediate,
},
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