Skip to content

Commit 64436ee

Browse files
committed
impr(dom utils): rename ondocumentready, add onwindowload
!nuf
1 parent 469a2f6 commit 64436ee

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

frontend/src/ts/controllers/ad-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Config from "../config";
77
import * as TestState from "../test/test-state";
88
import * as EG from "./eg-ad-controller";
99
import * as PW from "./pw-ad-controller";
10-
import { onDocumentReady, qs } from "../utils/dom";
10+
import { onDOMReady, qs } from "../utils/dom";
1111

1212
const breakpoint = 900;
1313
let widerThanBreakpoint = true;
@@ -317,7 +317,7 @@ BannerEvent.subscribe(() => {
317317
updateVerticalMargin();
318318
});
319319

320-
onDocumentReady(() => {
320+
onDOMReady(() => {
321321
updateBreakpoint(true);
322322
updateBreakpoint2();
323323
});

frontend/src/ts/pages/account-settings.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as BlockedUserTable from "../elements/account-settings/blocked-user-tab
1212
import * as Notifications from "../elements/notifications";
1313
import { z } from "zod";
1414
import * as AuthEvent from "../observables/auth-event";
15-
import { qs, qsr, onDocumentReady } from "../utils/dom";
15+
import { qs, qsr, onWindowLoad } from "../utils/dom";
1616

1717
const pageElement = qsr(".page.pageAccountSettings");
1818

@@ -242,10 +242,6 @@ export const page = new PageWithUrlParams({
242242
},
243243
});
244244

245-
onDocumentReady(() => {
246-
setTimeout(() => {
247-
//band aid fix for now, we need to delay saving the skeleton
248-
// to allow the click listeners to be registered first
249-
Skeleton.save("pageAccountSettings");
250-
}, 0);
245+
onWindowLoad(() => {
246+
Skeleton.save("pageAccountSettings");
251247
});

frontend/src/ts/ready.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { getActiveFunboxesWithFunction } from "./test/funbox/list";
1010
import { configLoadPromise } from "./config";
1111
import { authPromise } from "./firebase";
1212
import { animate } from "animejs";
13-
import { onDocumentReady, qs } from "./utils/dom";
13+
import { onDOMReady, qs } from "./utils/dom";
1414

15-
onDocumentReady(async () => {
15+
onDOMReady(async () => {
1616
await configLoadPromise;
1717
await authPromise;
1818

frontend/src/ts/utils/dom.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,32 @@ export function qsr<T extends HTMLElement = HTMLElement>(
5252
}
5353

5454
/**
55-
* Execute a callback function when the document is fully loaded.
55+
* Execute a callback function when the DOM is fully loaded. If you need to wait
56+
* for all resources (images, stylesheets, scripts, etc.) to load, use `onWindowLoad` instead.
5657
* If the document is already loaded, the callback is executed immediately.
5758
*/
58-
export function onDocumentReady(callback: () => void): void {
59+
export function onDOMReady(callback: () => void): void {
5960
if (document.readyState === "loading") {
6061
document.addEventListener("DOMContentLoaded", callback);
6162
} else {
6263
callback();
6364
}
6465
}
6566

67+
/**
68+
* Execute a callback function when the window 'load' event fires, which occurs
69+
* after the entire page (including all dependent resources such as images,
70+
* stylesheets, and scripts) has fully loaded.
71+
* If the window is already loaded, the callback is executed immediately.
72+
*/
73+
export function onWindowLoad(callback: () => void): void {
74+
if (document.readyState === "complete") {
75+
callback();
76+
} else {
77+
window.addEventListener("load", callback);
78+
}
79+
}
80+
6681
/**
6782
* Creates an ElementWithUtils wrapping a newly created element.
6883
* @param tagName The tag name of the element to create.

0 commit comments

Comments
 (0)