mirror of
https://github.com/hanzoai/sentinel.git
synced 2026-09-29 22:34:33 +00:00
31 lines
679 B
TypeScript
31 lines
679 B
TypeScript
let hasEventListener = false;
|
|
const queued: Function[] = [];
|
|
|
|
function onDomContentLoaded() {
|
|
if (!queued.length) {
|
|
return;
|
|
}
|
|
|
|
while (queued.length) {
|
|
queued.pop()?.();
|
|
}
|
|
|
|
// We can remove this listener immediately since `DOMContentLoaded` should only be fired once
|
|
document.removeEventListener('DOMContentLoaded', onDomContentLoaded);
|
|
hasEventListener = false;
|
|
}
|
|
|
|
export function renderOnDomReady(renderFn: () => void) {
|
|
if (document.readyState !== 'loading') {
|
|
renderFn();
|
|
return;
|
|
}
|
|
|
|
queued.push(renderFn);
|
|
|
|
if (!hasEventListener) {
|
|
document.addEventListener('DOMContentLoaded', onDomContentLoaded);
|
|
hasEventListener = true;
|
|
}
|
|
}
|