Skip to content

i18n-calypso: Replace EventEmitter with simplified subscriber callbacks #103304

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

Merged
merged 8 commits into from
May 14, 2025

Conversation

aduth
Copy link
Member

@aduth aduth commented May 9, 2025

Part of ARC-129

Proposed Changes

  • Removes EventEmitter usage from i18n-calypso package, substituting a simplified subscriber implementation

Why are these changes being made?

On performance, I discovered that the events library does have a surprisingly-performant subscriber removal behavior. Early iterations here included a "readable" Array#filter implementation (see a8fb2d4), but considering that many of our localized components will remove event listeners once they're unmounted, it seemed worthwhile to micro-optimize. After several rounds of micro-benchmarking, I found a solution which performs even better (~38%) than events's implementation, and ~850x better than a naive Array#filter implementation in a scenario with 2000 subscribers.

Separately, I had explored an implementation which largely maintained the same API if we'd rather reduce the scope of the changes.

Testing Instructions

  • The idea is that this should be drop-in compatible for localization, and should have no impact on the display and re-rendering of translation data. Changes can be emulated by switching languages in the dashboard.

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
    • For UI changes, have we tested the change in various languages (for example, ES, PT, FR, or DE)? The length of text and words vary significantly between languages.
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

@aduth aduth requested a review from a team May 9, 2025 19:27
@aduth aduth requested a review from a team as a code owner May 9, 2025 19:27
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label May 9, 2025
Copy link

github-actions bot commented May 9, 2025

@matticbot
Copy link
Contributor

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • blaze-dashboard
  • command-palette-wp-admin
  • happy-blocks
  • help-center
  • notifications
  • odyssey-stats
  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug remove/i18n-events-dep on your sandbox.

@matticbot
Copy link
Contributor

matticbot commented May 9, 2025

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

App Entrypoints (~195 bytes removed 📉 [gzipped])

name                   parsed_size           gzip_size
entry-subscriptions         -376 B  (-0.0%)     -109 B  (-0.0%)
entry-login                 -376 B  (-0.0%)     -106 B  (-0.0%)
entry-main                  -356 B  (-0.0%)      -92 B  (-0.0%)
entry-reauth-required       -354 B  (-0.0%)      -89 B  (-0.0%)
entry-stepper               -315 B  (-0.0%)      -77 B  (-0.0%)
entry-domains-landing       -270 B  (-0.0%)      -50 B  (-0.0%)

Common code that is always downloaded and parsed every time the app is loaded, no matter which route is used.

Sections (~241 bytes removed 📉 [gzipped])

name                                parsed_size           gzip_size
stepper-user-step                         -61 B  (-0.0%)      -28 B  (-0.0%)
async-step-use-my-domain                  -61 B  (-0.0%)      -31 B  (-0.0%)
async-step-unified-plans                  -61 B  (-0.0%)      -27 B  (-0.0%)
async-step-unified-domains                -61 B  (-0.0%)      -28 B  (-0.0%)
site-migration-flow                       -41 B  (-0.1%)      -20 B  (-0.1%)
woocommerce-installation                  -20 B  (-0.0%)      -15 B  (-0.0%)
woocommerce                               -20 B  (-0.0%)      -15 B  (-0.0%)
themes                                    -20 B  (-0.0%)      -15 B  (-0.0%)
theme                                     -20 B  (-0.0%)      -15 B  (-0.0%)
subscribers                               -20 B  (-0.0%)      -15 B  (-0.0%)
stats                                     -20 B  (-0.0%)      -15 B  (-0.0%)
staging-site                              -20 B  (-0.0%)      -13 B  (-0.0%)
sites-dashboard                           -20 B  (-0.0%)      -13 B  (-0.0%)
site-settings                             -20 B  (-0.0%)      -13 B  (-0.0%)
site-purchases                            -20 B  (-0.0%)      -10 B  (-0.0%)
site-profiler                             -20 B  (-0.0%)      -15 B  (-0.0%)
site-performance                          -20 B  (-0.0%)      -13 B  (-0.0%)
site-monitoring                           -20 B  (-0.0%)      -13 B  (-0.0%)
site-logs                                 -20 B  (-0.0%)      -13 B  (-0.0%)
site-blocks                               -20 B  (-0.0%)      -15 B  (-0.0%)
settings-writing                          -20 B  (-0.0%)      -15 B  (-0.0%)
settings-security                         -20 B  (-0.0%)      -15 B  (-0.0%)
settings-reading                          -20 B  (-0.0%)      -15 B  (-0.0%)
settings-podcast                          -20 B  (-0.0%)      -15 B  (-0.0%)
settings-performance                      -20 B  (-0.0%)      -15 B  (-0.0%)
settings-newsletter                       -20 B  (-0.0%)      -15 B  (-0.0%)
settings-jetpack                          -20 B  (-0.0%)      -15 B  (-0.0%)
settings-discussion                       -20 B  (-0.0%)      -15 B  (-0.0%)
settings                                  -20 B  (-0.0%)      -15 B  (-0.0%)
security                                  -20 B  (-0.0%)      -15 B  (-0.0%)
scan                                      -20 B  (-0.0%)      -15 B  (-0.0%)
reader                                    -20 B  (-0.0%)      -14 B  (-0.0%)
purchases                                 -20 B  (-0.0%)      -10 B  (-0.0%)
purchase-product                          -20 B  (-0.0%)      -15 B  (-0.0%)
promote-post-i2                           -20 B  (-0.0%)      -15 B  (-0.0%)
privacy                                   -20 B  (-0.0%)      -15 B  (-0.0%)
posts-custom                              -20 B  (-0.0%)      -15 B  (-0.0%)
posts                                     -20 B  (-0.0%)      -15 B  (-0.0%)
plugins                                   -20 B  (-0.0%)      -13 B  (-0.0%)
plans                                     -20 B  (-0.0%)      -13 B  (-0.0%)
performance-profiler                      -20 B  (-0.0%)      -15 B  (-0.0%)
people                                    -20 B  (-0.0%)      -15 B  (-0.0%)
patterns                                  -20 B  (-0.0%)      -13 B  (-0.0%)
pages                                     -20 B  (-0.0%)      -15 B  (-0.0%)
overview                                  -20 B  (-0.0%)      -13 B  (-0.0%)
notification-settings                     -20 B  (-0.0%)      -15 B  (-0.0%)
migrate                                   -20 B  (-0.0%)      -15 B  (-0.0%)
media                                     -20 B  (-0.0%)      -15 B  (-0.0%)
me                                        -20 B  (-0.0%)      -15 B  (-0.0%)
marketplace                               -20 B  (-0.0%)      -15 B  (-0.0%)
marketing                                 -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-social                            -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-search                            -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-connect                           -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-settings                    -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-pricing                     -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-plugin-management           -20 B  (-0.0%)      -13 B  (-0.0%)
jetpack-cloud-partner-portal              -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-overview                    -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-manage-pricing              -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-features-comparison         -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud-agency-signup               -20 B  (-0.0%)      -15 B  (-0.1%)
jetpack-cloud-agency-dashboard            -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-cloud                             -20 B  (-0.0%)      -15 B  (-0.0%)
jetpack-app                               -20 B  (-0.0%)      -15 B  (-0.0%)
import                                    -20 B  (-0.0%)      -15 B  (-0.0%)
hosting                                   -20 B  (-0.0%)      -13 B  (-0.0%)
home                                      -20 B  (-0.0%)      -15 B  (-0.0%)
help                                      -20 B  (-0.0%)      -15 B  (-0.0%)
gutenberg-editor                          -20 B  (-0.0%)      -15 B  (-0.0%)
google-my-business                        -20 B  (-0.0%)      -15 B  (-0.0%)
github-deployments                        -20 B  (-0.0%)      -13 B  (-0.0%)
export                                    -20 B  (-0.0%)      -15 B  (-0.0%)
entrepreneur-flow                         -20 B  (-0.0%)      -15 B  (-0.0%)
email                                     -20 B  (-0.0%)      -15 B  (-0.0%)
earn                                      -20 B  (-0.0%)      -15 B  (-0.0%)
domains                                   -20 B  (-0.0%)      -13 B  (-0.0%)
developer                                 -20 B  (-0.0%)      -15 B  (-0.0%)
customize                                 -20 B  (-0.0%)      -15 B  (-0.0%)
concierge                                 -20 B  (-0.0%)      -15 B  (-0.0%)
comments                                  -20 B  (-0.0%)      -15 B  (-0.0%)
checkout                                  -20 B  (-0.0%)      -15 B  (-0.0%)
backup                                    -20 B  (-0.0%)      -15 B  (-0.0%)
add-ons                                   -20 B  (-0.0%)      -15 B  (-0.0%)
activity                                  -20 B  (-0.0%)      -15 B  (-0.0%)
account-close                             -20 B  (-0.0%)      -15 B  (-0.0%)
account                                   -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-woopayments              -20 B  (-0.0%)      -16 B  (-0.0%)
a8c-for-agencies-team                     -20 B  (-0.0%)      -14 B  (-0.0%)
a8c-for-agencies-sites                    -20 B  (-0.0%)      -11 B  (-0.0%)
a8c-for-agencies-settings                 -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-referrals                -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-purchases                -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-plugins                  -20 B  (-0.0%)      -13 B  (-0.0%)
a8c-for-agencies-partner-directory        -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-overview                 -20 B  (-0.0%)      -11 B  (-0.0%)
a8c-for-agencies-migrations               -20 B  (-0.0%)      -17 B  (-0.0%)
a8c-for-agencies-marketplace              -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-landing                  -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies-client                   -20 B  (-0.0%)      -14 B  (-0.0%)
a8c-for-agencies-agency-tier              -20 B  (-0.0%)      -15 B  (-0.0%)
a8c-for-agencies                          -20 B  (-0.0%)      -15 B  (-0.0%)

Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

Async-loaded Components (~51 bytes removed 📉 [gzipped])

name                                                                              parsed_size           gzip_size
async-load-automattic-help-center-stepper                                               -61 B  (-0.0%)      -32 B  (-0.0%)
async-load-signup-steps-woocommerce-install-transfer                                    -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-woocommerce-install-step-store-address                          -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-woocommerce-install-step-business-info                          -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-woocommerce-install-confirm                                     -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-website-content-section-types                                   -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-website-content                                                 -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-user                                                            -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-store-features                                                  -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-starting-point                                                  -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-social-profiles                                                 -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-site-picker                                                     -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-site-or-domain                                                  -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-site-options                                                    -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-site                                                            -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-rewind-were-backing                                             -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-rewind-form-creds                                               -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-plans-theme-preselected                                         -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-plans                                                           -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-page-picker                                                     -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-new-or-existing-site                                            -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-intent                                                          -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-hosting-decider                                                 -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-emails                                                          -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-domains                                                         -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-difm-site-picker                                                -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-design-picker                                                   -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-creds-permission                                                -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-creds-confirm                                                   -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-courses                                                         -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-clone-start                                                     -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-clone-ready                                                     -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-clone-point                                                     -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-clone-destination                                               -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-signup-steps-clone-credentials                                               -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-signup-steps-clone-cloning                                                   -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-purchase-modal-wrapper                                                       -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-my-sites-checkout-purchase-modal-is-eligible-for-one-click-checkou...        -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-masterbar-cart-masterbar-cart-wrapper                                        -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-design-playground                                                            -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-design-blocks                                                                -20 B  (-0.0%)      -10 B  (-0.0%)
async-load-design                                                                       -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-calypso-notifications                                                        -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-calypso-blocks-support-article-dialog-dialog                                 -20 B  (-0.0%)      -16 B  (-0.0%)
async-load-calypso-blocks-jitm-templates-home-task                                      -20 B  (-0.0%)      -15 B  (-0.1%)
async-load-automattic-help-center                                                       -20 B  (-0.0%)      -15 B  (-0.0%)
async-load-calypso-layout-community-translator                                          +11 B  (+0.0%)       +7 B  (+0.1%)

React components that are loaded lazily, when a certain part of UI is displayed for the first time.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@tyxla tyxla requested a review from yuliyan May 12, 2025 12:41
Copy link
Member

@yuliyan yuliyan left a comment

Choose a reason for hiding this comment

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

The changes appear to be working well, and the rationale behind them makes sense. Regarding the suggestion to use a Set instead of an array for the subscribers - I haven't done a deep dive into the performance implications, so feel free to disregard that if using an array makes more sense in this context.
LGTM 👍

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

Thanks for working on it @aduth 🙌

Happy to see one more EventEmitter down. So few remaining!

The only thing I'm hesitant about is what @yuliyan already recognized with how we're cleaning up the subscribers.

Copy link
Member

@jsnajdr jsnajdr left a comment

Choose a reason for hiding this comment

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

Nice discussion, but I'm afraid we're already deep in the "good enough" territory where all the proposed alternatives are perfectly fine.

@@ -328,6 +328,6 @@ export function trackTranslatorStatus( isTranslatorEnabled ) {
}

// re-initialize when new locale data is loaded
i18n.on( 'change', communityTranslatorJumpstart.init.bind( communityTranslatorJumpstart ) );
i18n.subscribe( communityTranslatorJumpstart.init.bind( communityTranslatorJumpstart ) );
Copy link
Member

Choose a reason for hiding this comment

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

The call of the init method, does it even work? The method takes two args, user and isUserSettingsReady. And returns early if user is null-ish. But the change event listener was not called with any arguments, is it? And neither is the new subscribe callback.

Copy link
Member Author

Choose a reason for hiding this comment

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

The call of the init method, does it even work? The method takes two args, user and isUserSettingsReady. And returns early if user is null-ish. But the change event listener was not called with any arguments, is it? And neither is the new subscribe callback.

I'll check on this tomorrow 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

The call of the init method, does it even work? The method takes two args, user and isUserSettingsReady. And returns early if user is null-ish. But the change event listener was not called with any arguments, is it? And neither is the new subscribe callback.

I'll check on this tomorrow 👍

Update: You're correct that the initialization triggered by this subscriber always short-circuits at the user check. This subscriber is very old (originates in pre-OSS version), at which time the init function didn't take arguments. Additionally, I don't think we should rely on a side-effect like this when importing a module, since it isn't compatible with tree-shaking. That being said, the subscriber is being added.

Instead, I think the initialization is happening in the TranslatorLauncher component, which passes the user arguments correctly. It also appears to reinitialize many times when the component re-renders.

I'm not entirely familiar with how this should be working (e.g. in response to asynchronous loads or language changes), but I think it's fair to say that the subscriber is not doing anything in its current form and can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to keep this in as-is to keep it functionally equivalent, and will follow-up with a pull request proposing to remove the ineffective subscribe call.

aduth added 3 commits May 14, 2025 09:44
Since we're interested in subscriber behavior in response to certain actions (e.g. setting locale), not relevant to test here, and duplicative.

See: #103304 (comment)
@aduth aduth merged commit f6d7711 into trunk May 14, 2025
14 checks passed
@aduth aduth deleted the remove/i18n-events-dep branch May 14, 2025 14:44
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants