Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
Loading
from
Open
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
feat: 馃殌 add the deep and immediate parameters
  • Loading branch information
17359898647 committed Jan 11, 2024
commit f2678283bba80bcee8efa91808c93a89b5f46f42
17 changes: 16 additions & 1 deletion 17 packages/shared/refThrottled/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('refThrottled', () => {
let error = null

try {
refThrottled(objRef, 100, true, true, () => {
refThrottled(objRef, 100, true, true, true, true, () => {
throw new Error('cloneHandler error')
})
}
Expand All @@ -56,4 +56,19 @@ describe('refThrottled', () => {
expect(error).toBeInstanceOf(Error)
expect(error?.message).toBe('cloneHandler error')
})

it('should handle default cloneHandler error', async () => {
const objRef = ref({}) as any
objRef.value.self = objRef.value
let error = null
let throttledRef = null
try {
throttledRef = refThrottled(objRef, 100, true, true, true)
}
catch (e) {
error = e as Error
}
expect(throttledRef?.value).toEqual(objRef.value)
expect(error).toBeNull()
})
})
50 changes: 29 additions & 21 deletions 50 packages/shared/refThrottled/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Ref } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import { cloneFnJSON } from '../../core/useCloned'
import { watch } from 'vue-demi'
import { cloneFnJSON, useCloned } from '../../core/useCloned'
import { useThrottleFn } from '../useThrottleFn'

function isReferenceType(value: any) {
Expand All @@ -14,32 +14,40 @@ function isReferenceType(value: any) {
* @param delay A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param [trailing] if true, update the value again after the delay time is up
* @param [leading] if true, update the value on the leading edge of the ms timeout
* @param [deep] default true, Depth monitoring, if false, only monitor the first level
* @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, cloneHandler = cloneFnJSON) {
export function refThrottled<T>(value: Ref<T>, delay = 200, trailing = true, leading = true, deep = true, immediate = true, cloneHandler = cloneFnJSON) {
17359898647 marked this conversation as resolved.
Show resolved Hide resolved
if (delay <= 0)
return value
const throttled = ref() as Ref<T>
const isEqualityClone = cloneHandler === cloneFnJSON
const setThrottled = () => {
try {
throttled.value = isReferenceType(value.value) ? cloneHandler(value.value) : value.value
}
catch (error) {
if (isEqualityClone) {
throttled.value = value.value
console.error('Some error occurred in cloneHandler, used uncloned value', error)
}
else {
throw error
}
}
}
setThrottled()
const updater = useThrottleFn(setThrottled, delay, trailing, leading)
const { sync, cloned: throttled } = useCloned(
value,
{
manual: true,
clone: (cloneValue) => {
try {
return isReferenceType(cloneValue) ? cloneHandler(cloneValue) : cloneValue
}
catch (error) {
if (isEqualityClone) {
console.error('Some error occurred in cloneHandler, used uncloned value', error)
return cloneValue
}
else {
throw error
}
}
},
deep,
immediate,
},
)
const updater = useThrottleFn(sync, delay, trailing, leading)

watch(value, updater, {
deep: isReferenceType(value.value),
deep,
})

return throttled
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.