Make WordPress Core

Opened 21 months ago

Last modified 21 months ago

#57517 new enhancement

Expose MutationObserver instance in wpEmoji

Reported by: rinatkhaziev's profile rinatkhaziev Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Formatting Keywords: has-patch
Focuses: ui, javascript, performance Cc:

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.


21 months ago
#1

  • Keywords has-patch added

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 subtree argument.

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 ) {

Stop watching the whole body and its child elements

obs.disconnect();

Do something of limited scope

obs.observe( document.querySelector('#content'), {subtree: true, childList: true} );

}

}}}

An example of a basic MutationObserver can be seen in MDN

Trac ticket: https://core.trac.wordpress.org/ticket/57517

rinatkhaziev commented on PR #3879:


21 months ago
#2

Hmm apparently the build process doesn't like arrow functions even though it's valid, making the necessary changes.

Note: See TracTickets for help on using tickets.