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
feat(useCloned): new function #2045
Conversation
packages/core/useCloned/index.ts
Outdated
let stopWatcher: undefined | WatchStopHandle | ||
|
||
if (!manual && isRef(source)) | ||
stopWatcher = watch(source, sync, { immediate: true, deep: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make the watch options configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make the watch options configurable.
Should we set immediate: true and deep: true by defaults ? I think we should
packages/core/useCloned/index.ts
Outdated
/** | ||
* Custom clone function should return new value for cloned data | ||
*/ | ||
cloneFunction?: (source: Partial<T>, cloned: Partial<T>) => Partial<T> | T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cloneFunction?: (source: Partial<T>, cloned: Partial<T>) => Partial<T> | T | |
cloneFunction?: (source: T) => | T |
And we could default it to JSON.parse(JSON.stringify(source))
packages/core/useCloned/index.ts
Outdated
cloned.value = cloneFunction ? cloneFunction(unref(source), cloned.value) : defaultCloning() | ||
} | ||
|
||
return { cloned, sync, ...(stopWatcher && { stop: stopWatcher }) } as any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return { cloned, sync, ...(stopWatcher && { stop: stopWatcher }) } as any | |
return { cloned, sync } |
We don't expose the stop function, ppl could use effectScope
to capture it if needed.
packages/core/useCloned/index.ts
Outdated
else | ||
sync() | ||
|
||
function defaultCloning() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually there's a native deep clone api called: structuredClone, and supported quite well on different browsers. Use it if exists on window
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually there's a native deep clone api called: structuredClone, and supported quite well on different browsers. Use it if exists on
window
.
Hm, I like it, I'll add this one. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
structuredClone
is not available in all browsers. We should let users pass it explicitly instead of using the current fallback, which will lead to different behavior on different browsers implicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
structuredClone
is not available in all browsers. We should let users pass it explicitly instead of using the current fallback, which will lead to different behavior on different browsers implicitly.
removed structuredClone
packages/core/useCloned/index.md
Outdated
}) | ||
|
||
const { cloned, sync } = useCloned(data, { | ||
cloneFunction: (source, cloned) => ({ ...source, isCloned: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should show a case of using deepclone
or something instead of show the example for adding extra properties (which kinda violate the idea of cloneing
)
packages/core/useCloned/index.ts
Outdated
else | ||
sync() | ||
|
||
function defaultCloning() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
structuredClone
is not available in all browsers. We should let users pass it explicitly instead of using the current fallback, which will lead to different behavior on different browsers implicitly.
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
closes #1859
Description
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).