Skip to content
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

Merged
merged 12 commits into from Sep 5, 2022
Merged

feat(useCloned): new function #2045

merged 12 commits into from Sep 5, 2022

Conversation

chaii3
Copy link
Contributor

@chaii3 chaii3 commented Aug 4, 2022

closes #1859

Description

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

let stopWatcher: undefined | WatchStopHandle

if (!manual && isRef(source))
stopWatcher = watch(source, sync, { immediate: true, deep: true })
Copy link
Member

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.

Copy link
Contributor Author

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

/**
* Custom clone function should return new value for cloned data
*/
cloneFunction?: (source: Partial<T>, cloned: Partial<T>) => Partial<T> | T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))

@chaii3 chaii3 requested a review from antfu August 5, 2022 16:43
cloned.value = cloneFunction ? cloneFunction(unref(source), cloned.value) : defaultCloning()
}

return { cloned, sync, ...(stopWatcher && { stop: stopWatcher }) } as any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@chaii3 chaii3 requested a review from antfu August 6, 2022 08:53
else
sync()

function defaultCloning() {
Copy link
Contributor

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.

Copy link
Contributor Author

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!

Copy link
Member

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.

Copy link
Contributor Author

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

@chaii3 chaii3 requested a review from vaakian August 15, 2022 14:07
packages/core/useCloned/index.md Outdated Show resolved Hide resolved
})

const { cloned, sync } = useCloned(data, {
cloneFunction: (source, cloned) => ({ ...source, isCloned: true })
Copy link
Member

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)

else
sync()

function defaultCloning() {
Copy link
Member

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.

packages/core/useCloned/index.ts Outdated Show resolved Hide resolved
packages/core/useCloned/index.ts Outdated Show resolved Hide resolved
@chaii3 chaii3 requested review from antfu and vaakian and removed request for vaakian and antfu August 30, 2022 08:14
@antfu antfu mentioned this pull request Aug 31, 2022
9 tasks
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
@chaii3 chaii3 requested review from antfu and vaakian and removed request for antfu and vaakian August 31, 2022 12:30
@antfu antfu merged commit 0a0a1da into vueuse:main Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new composable: usePropClone
3 participants