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(watchArray): new function #1705

Merged
merged 6 commits into from Jul 6, 2022
Merged

Conversation

w1ndy
Copy link
Contributor

@w1ndy w1ndy commented Jun 19, 2022

Description

This PR adds a new watcher helper watchListChanges. This function monitors the changes made to a list and notifies the callback with the added and removed elements.

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.

@w1ndy
Copy link
Contributor Author

w1ndy commented Jun 19, 2022

This function is handy when using hooks with v-for:

<div v-for="k in num" :ref="refs.set">{{ k }}</div>
const refs = useTemplateRefsList<HTMLDivElement>()
watchListChanges(refs, (_new, _old, added) => {
    for (let el of added) {
        useEventListener(el, 'click', () => console.log('clicked!'))
    }
}, { deep: true })

w1ndy and others added 2 commits June 19, 2022 20:28
@w1ndy
Copy link
Contributor Author

w1ndy commented Jun 19, 2022

@antfu Thanks for your feedback, I've renamed this function to watchArray and added two tests for the reactive and functional watch sources. Could you help review again?

@antfu antfu changed the title feat: implement watchListChanges feat(watchArray): new function Jun 19, 2022
Comment on lines +21 to +22
console.log(added) // [4]
console.log(removed) // []
Copy link
Member

Choose a reason for hiding this comment

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

Can added and removed also have meta data like [{ value: 4, index: 3 }]?

antfu
antfu previously approved these changes Jul 6, 2022
@antfu antfu enabled auto-merge (squash) July 6, 2022 18:39
antfu
antfu previously approved these changes Jul 6, 2022
@antfu antfu merged commit 2ba83d1 into vueuse:main Jul 6, 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.

None yet

2 participants