Opened 4 years ago
Last modified 4 years ago
#57517 new enhancement
Expose MutationObserver instance in wpEmoji
| Reported by: | rinatkhaziev | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Formatting | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: | ui, javascript, performance |
Description
The current implementation of MutationObserver in wpEmoji keeps the instance private, making it impossible to disconnect/reconnect the observer if necessary.
This has performance implications whenever a page performs large/complex DOM updates frequently.
A typical workaround is to disable wpEmoji altogether, but I believe there's a better way.
If we expose the observer instance, we can call connect/disconnect as needed.
// Before doing something expensive wp.emoji.getObserver().disconnect(); // render 10k nodes in your favorite frontend framework // Re-enable later wp.emoji.getObserver().observe( document.body, { childList: true, subtree: true } )
Change History (2)
This ticket was mentioned in PR #3879 on WordPress/wordpress-develop by rinatkhaziev.
4 years ago
#1
- Keywords has-patch added
rinatkhaziev commented on PR #3879:
4 years ago
#2
Hmm apparently the build process doesn't like arrow functions even though it's valid, making the necessary changes.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Previously an instance was in the local scope of load method. This prevented any sort of manipulation (e.g. disconnect/observe) which left the implementation hamstrung. The original observer callback is very greedy (observes the whole body of the document) - this has performance implications in case the application frequently inserts/updates many nodes.
The reason for the behavior is passing of the
subtreeargument.With this PR we can do something like this whenever we need.
{{{javascript
let obs = wp.emoji.getObserver();
Stop watching the body
if ( null !== obs ) {
}
}}}
An example of a basic MutationObserver can be seen in MDN
Trac ticket: https://core.trac.wordpress.org/ticket/57517