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

#4942: re-test entire selector on mutation in initialize helper #4943

Merged
merged 4 commits into from Jan 4, 2023

Conversation

twschiller
Copy link
Contributor

What does this PR do?

Discussion

  • Main concern would be impact on the performance of initialize triggers. However, originally .is gets called on all modified/new elements. So theoretically the additional overhead shouldn't be much especially if using selectors with browser support

Demo

  • See Jest tests

Checklist

  • Add tests
  • Designate a primary reviewer: @fregante

@twschiller twschiller changed the title #4942: re-test entire selector on mutation #4942: re-test entire selector on mutation in initialize helper Jan 3, 2023
@twschiller twschiller self-assigned this Jan 3, 2023
@codecov
Copy link

codecov bot commented Jan 3, 2023

Codecov Report

Merging #4943 (47f996a) into main (9f0de49) will increase coverage by 0.02%.
The diff coverage is n/a.

❗ Current head 47f996a differs from pull request most recent head bb82140. Consider uploading reports for the commit bb82140 to get more accurate results

@@            Coverage Diff             @@
##             main    #4943      +/-   ##
==========================================
+ Coverage   59.08%   59.11%   +0.02%     
==========================================
  Files         963      963              
  Lines       29043    29043              
  Branches     5558     5558              
==========================================
+ Hits        17161    17168       +7     
+ Misses      11882    11875       -7     
Impacted Files Coverage Δ
src/helpers.ts 72.00% <ø> (ø)
src/extensionPoints/helpers.ts 74.07% <0.00%> (+6.48%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

// Check if the match applies now that the document has been updated. The handles cases where an ancestor was
// added/modified causing an element on the page to now match. This strictly isn't an "initialization", as the
// element wasn't just added. But conceptually, it corresponds to the selector now matching
matches.push(...$safeFind(msobserver.selector, options.target).toArray());
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the original implementation misses some cases, then we can drop the entire block of code prior to this, right? That block tried to be efficient by checking exactly the parts the changed, but if we just check the whole tree at the end anyway, the optimization becomes duplicate.

Copy link
Contributor Author

@twschiller twschiller Jan 3, 2023

Choose a reason for hiding this comment

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

Hmm, that's a good point. I was thinking maybe we should keep the existing code and then throttle the $safeFind call, so you get sort of the both of best worlds. Thoughts?

A real optimization would involve parsing (using jquery.tokenize) the selector to watch for mutation involving potentially related elements

Copy link
Collaborator

@fregante fregante Jan 3, 2023

Choose a reason for hiding this comment

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

throttle the $safeFind call

I don’t think that would match the exact arguments passed by that block at least half the time. But if you throttle the call and drop the optimization then it's fine.

However initialize is sync so throttling is not super straightforward.

I see two options for now:

  • merge as is
  • drop the optimization block (assuming this line covers the entire target just as well as the block)

Further improvements/optimizations almost certainly would be worse than just rewriting the library with proper testing around it, since at the core it's just:

  • MutationObserver simply to detect document changes
  • throttle calls
  • avoid multiple callback calls for the same matched item (via WeakMap)

Copy link
Contributor Author

@twschiller twschiller Jan 4, 2023

Choose a reason for hiding this comment

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

I took the third way. The existing MutationObserver will be more performant

We already had an affordance for avoiding duplicate calls. That was required due to the MutationObserver running on subtree changes. An element could be visited as part of multiple mutations

@twschiller twschiller added the do not merge Merging of this PR is blocked by another one label Jan 3, 2023
@@ -153,6 +163,11 @@ msobservers.initialize = function (selector, callback, options) {
$(matches[i]).each(msobserver.callback);

isMatchinInProgress = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be last or it might enter a loop

$(this).each(callback);
}
};

var throttledCheckTarget = throttle(
function () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure why this non-arrow function as not blocked by the linter here 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Our lint rules are off for vendored code: src/vendors

@vercel
Copy link

vercel bot commented Jan 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
pixiebrix-extension 🔄 Building (Inspect) Jan 4, 2023 at 4:33PM (UTC)

Copy link
Collaborator

@fregante fregante left a comment

Choose a reason for hiding this comment

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

Oh forgot to approve

@github-actions
Copy link

github-actions bot commented Jan 4, 2023

No loom links were found in the first post. Please add one there if you'd like to it to appear on Slack.

Do not edit this comment manually.

@twschiller twschiller enabled auto-merge (squash) January 4, 2023 16:39
@twschiller twschiller removed the do not merge Merging of this PR is blocked by another one label Jan 4, 2023
@twschiller twschiller added this to the 1.7.16 milestone Jan 4, 2023
@twschiller twschiller merged commit 9d9c7bc into main Jan 4, 2023
@twschiller twschiller deleted the feature/4942-initialize branch January 4, 2023 16:42
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.

Button extension support: support selector that depends on other part of page changing
2 participants