From 6884a3d7f10ca6391ff08422475c36e90f50f71d Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Mon, 12 Apr 2021 09:57:40 -0500 Subject: [PATCH] Clean up initializePage (#13236) * Remove early call to setup local storage for user The first thing the callInitializers function does is re-invoke initializeLocalStorageRender, calling the same function twice (for a different outcome?) seems like it's not effective. I tried to trace down how long it had been that way, and came to the very first commit in the repo - 301c6080e3f64139ea339d37d8c151521bc07177, where the pattern already existed, so it's unclear at what point (prior to 2018) this was added, and what problem it was trying to solve. If we inlined callInitializers (by renaming it initializePage and deleting the bottom function), the duplication would be especially evident. This is the only call site for callInitializers, which would be the other reason we _might_ want to have redundancy. * Inline function call Since initializePage only acted as a new name for callInitializers (immediately transferring control to the callInitializer function), remove the wrapper function and rename the *actual* code with the public name initializePage. No logic/behavior should change, unless we were unit testing callInitializers. * Shorten method by moving long list of logicless calls to a helper "We don't want to make a habit of pandering to Code Climate and its metrics blindly". Appease the robot by moving about 20 lines of uninteresting sequential code out of the initializePage function and into a "callInitializers" function, which is also the name of the function I just inlined. --- app/assets/javascripts/initializePage.js | 53 ++++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/initializePage.js b/app/assets/javascripts/initializePage.js index 155d727b4..ce7384343 100644 --- a/app/assets/javascripts/initializePage.js +++ b/app/assets/javascripts/initializePage.js @@ -16,28 +16,6 @@ */ function callInitializers() { - initializeLocalStorageRender(); - initializeBodyData(); - - var waitingForDataLoad = setInterval(function wait() { - if (document.body.getAttribute('data-loaded') === 'true') { - clearInterval(waitingForDataLoad); - if (document.body.getAttribute('data-user-status') === 'logged-in') { - initializeBaseUserData(); - initializeAllChatButtons(); - initializeAllTagEditButtons(); - } - initializeBroadcast(); - initializeAllFollowButts(); - initializeUserFollowButts(); - initializeReadingListIcons(); - initializeSponsorshipVisibility(); - if (document.getElementById('sidebar-additional')) { - document.getElementById('sidebar-additional').classList.add('showing'); - } - } - }, 1); - initializeBaseTracking(); initializePaymentPointers(); initializeCommentsPage(); @@ -64,6 +42,32 @@ function callInitializers() { initializeOnboardingTaskCard(); initializeDateHelpers(); initializeColorPicker(); +} + +function initializePage() { + initializeLocalStorageRender(); + initializeBodyData(); + + var waitingForDataLoad = setInterval(function wait() { + if (document.body.getAttribute('data-loaded') === 'true') { + clearInterval(waitingForDataLoad); + if (document.body.getAttribute('data-user-status') === 'logged-in') { + initializeBaseUserData(); + initializeAllChatButtons(); + initializeAllTagEditButtons(); + } + initializeBroadcast(); + initializeAllFollowButts(); + initializeUserFollowButts(); + initializeReadingListIcons(); + initializeSponsorshipVisibility(); + if (document.getElementById('sidebar-additional')) { + document.getElementById('sidebar-additional').classList.add('showing'); + } + } + }, 1); + + callInitializers(); function freezeScrolling(event) { event.preventDefault(); @@ -83,8 +87,3 @@ function callInitializers() { // Initialize data-runtime context to the body data-attribute document.body.dataset.runtime = Runtime.currentContext(); } - -function initializePage() { - initializeLocalStorageRender(); - callInitializers(); -}