docbrown/app/assets/javascripts/initializePage.js
Daniel Uber 6884a3d7f1
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 -
301c6080e3, 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.
2021-04-12 09:57:40 -05:00

89 lines
2.9 KiB
JavaScript

/*
global initializeLocalStorageRender, initializeBodyData,
initializeAllChatButtons, initializeAllTagEditButtons, initializeUserFollowButts,
initializeBaseTracking, initializeCommentsPage,
initializeArticleDate, initializeArticleReactions, initNotifications,
initializeCommentDate, initializeCommentDropdown, initializeSettings,
initializeCommentPreview,
initializeTimeFixer, initializeDashboardSort, initializePWAFunctionality,
initializeEllipsisMenu, initializeArchivedPostFilter, initializeCreditsPage,
initializeUserProfilePage, initializeProfileInfoToggle, initializePodcastPlayback,
initializeVideoPlayback, initializeDrawerSliders, initializeProfileBadgesToggle,
initializeHeroBannerClose, initializeOnboardingTaskCard, initScrolling,
nextPage:writable, fetching:writable, done:writable, adClicked:writable,
initializePaymentPointers, initializeBroadcast, initializeDateHelpers,
initializeColorPicker, Runtime
*/
function callInitializers() {
initializeBaseTracking();
initializePaymentPointers();
initializeCommentsPage();
initializeArticleDate();
initializeArticleReactions();
initNotifications();
initializeCommentDate();
initializeCommentDropdown();
initializeSettings();
initializeCommentPreview();
initializeTimeFixer();
initializeDashboardSort();
initializePWAFunctionality();
initializeEllipsisMenu();
initializeArchivedPostFilter();
initializeCreditsPage();
initializeUserProfilePage();
initializeProfileInfoToggle();
initializeProfileBadgesToggle();
initializePodcastPlayback();
initializeVideoPlayback();
initializeDrawerSliders();
initializeHeroBannerClose();
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();
}
nextPage = 0;
fetching = false;
done = false;
adClicked = false;
setTimeout(function undone() {
done = false;
}, 300);
if (!initScrolling.called) {
initScrolling();
}
// Initialize data-runtime context to the body data-attribute
document.body.dataset.runtime = Runtime.currentContext();
}