shama / on-load

On load/unload events for DOM elements using a MutationObserver
113 stars 19 forks source link

Use latest on/off callbacks from a particular caller #16

Closed greghuc closed 1 year ago

greghuc commented 7 years ago

This PR addresses an edge case of on-load that has unintuitive behaviour. Specifically:

This PR changes the behaviour to use the latest off function supplied instead, which seems more intuitive. It would also use the latest on function too, but I don't think this callback would ever called.

The PR changes

function eachAttr (mutation, on, off) {
  ..
  if (sameOrigin(mutation.oldValue, newValue)) {
    watch[newValue] = watch[mutation.oldValue]
    ..
  }

to

function eachAttr (mutation, on, off) {
  ..
  if (sameOrigin(mutation.oldValue, newValue)) {
    watch[newValue][2] = watch[mutation.oldValue][2]
    ..
  }

So previously the latest watch array for an element was overwritten with the earlier watch array; this means that the earlier on/off functions are always used in element callbacks. In this PR, we only overwrite watch[newValue][2] with watch[mutation.oldValue][2], which I believe indicates whether on or off was called last. So the later on/off functions are used as element callbacks instead.

greghuc commented 7 years ago

Also thinking about this, is there any reason we wouldn't delete watch[mutation.oldValue] in this case? Otherwise, there'll be a memory leak overtime as a caller makes multiple onload calls for the same element.

bcomnes commented 6 years ago

@shama or @lukeburns do you have any thoughts on this?

bcomnes commented 6 years ago

@greghuc seems like this is a good improvement, and covers some subtle characteristics about how on-load works for the better. Do you see any drawbacks from this? Nothing is coming to mind when looking at this.

bcomnes commented 6 years ago

We can cut this in as a patch or minor in 4.0.0 after we discuss further.

greghuc commented 6 years ago

@bcomnes I made this PR ages ago, and can't remember all the details. However, my former self did put serious work into understanding how on-load worked, so I do trust this PR and the suggestion to delete watch[mutation.oldValue]. I still use on-load, so it's great it's getting some love.

lukeburns commented 6 years ago

@bcomnes this looks good to me! 👍

greghuc commented 1 year ago

Hi @bcomnes.

I'm not sure if you're still maintaining the on-load package, but if you are.. could you consider merging this (just updated) pr from 2016?

I just touched some code that uses on-load, so I decided to update this pr. The pr has tests and was ok-ed by @lukeburns in 2018. I also updated everything to 2023 by a) updating deps and b) linting with standard.

The underlying pr was two changes:

Many thanks!

Greg

shama commented 1 year ago

Thanks @greghuc. Tested locally and LGTM. I'll merge this and do some other clean up then publish as new major (because of the var to let changes. Even though 6+ years later now let is well supported now, still technically a breaking change).

greghuc commented 1 year ago

@shama many thanks!