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

Adding a new helper to create a wrapper for MutationObserver #85

Open
franpb14 opened this issue Mar 6, 2024 · 1 comment
Open

Adding a new helper to create a wrapper for MutationObserver #85

franpb14 opened this issue Mar 6, 2024 · 1 comment

Comments

@franpb14
Copy link
Contributor

franpb14 commented Mar 6, 2024

Let's imagine we have a list of menus that we want to observe and do something if that menu is opened, right now we should something like this:

findAll('.menu').forEach(menu => {
  const observerMenu = new MutationObserver(() => {
    if (hasClass(menu, 'opened')) ...
  })
  observerMenu.observe(menu, { attributes: true })
})

so my suggestion is to create a wrapper
the helper would be something like:

observer(query, callback, options = { attributes: true }) {
  findAll(query).forEach(element => {
    const observerMenu = new MutationObserver((mutationList, observer) => {
      callback(element, mutationList)
    })
    observerMenu.observe(element, options)
  })
}

so we can just write

observer('.menu', currentTarget => { if (hasClass(currentTarget, 'opened')) ...})

other option for the helper could be:

observer(query, callback, options = { attributes: true }) {
    const observerMenu = new MutationObserver((mutationList, observer) => {
      callback(mutationList)
    })
    findAll(query).forEach(element => { observerMenu.observe(element, options) })
  })
}

In this case we are not creating a MutationObserver for each element but we don't have the currentTarget so we should use finds or something inside the callback

@franpb14 franpb14 changed the title Adding a new helper to create a wrapper for MutationObserver Adding a new helper to create a wrapper for MutationObserver Mar 6, 2024
@markets
Copy link
Member

markets commented May 7, 2024

@franpb14 I have very little experience with MutationObserver API, so at this point it's hard for me to decide between both proposals or even if that's enough of common complexity to justify a new helper.

@jfelip937 @araluce @lautarocastillo Could you please guys give your feedback (if any)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants