Skip to content

Commit 01bd21e

Browse files
committed
feat: add runWhenRootExists
1 parent fcae488 commit 01bd21e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export function getOffsetPosition(
140140
// eslint-disable-next-line @typescript-eslint/ban-types
141141
export async function runOnce(key: string, func: Function): Promise<any>
142142

143+
// eslint-disable-next-line @typescript-eslint/ban-types
144+
export function runWhenRootExists(func: Function): void
145+
143146
// eslint-disable-next-line @typescript-eslint/ban-types
144147
export function runWhenHeadExists(func: Function): void
145148

lib/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ export const parseInt10 = (number, defaultValue) => {
372372
return Number.isNaN(result) ? defaultValue : result
373373
}
374374

375+
const rootFuncArray = []
375376
const headFuncArray = []
376377
const bodyFuncArray = []
377378
let headBodyObserver
@@ -386,6 +387,14 @@ const startObserveHeadBodyExists = () => {
386387
headBodyObserver.disconnect()
387388
}
388389

390+
if (doc.documentElement && rootFuncArray.length > 0) {
391+
for (const func of rootFuncArray) {
392+
func()
393+
}
394+
395+
rootFuncArray.length = 0
396+
}
397+
389398
if (doc.head && headFuncArray.length > 0) {
390399
for (const func of headFuncArray) {
391400
func()
@@ -409,6 +418,19 @@ const startObserveHeadBodyExists = () => {
409418
})
410419
}
411420

421+
/**
422+
* Run function when document.documentElement exsits.
423+
*/
424+
export const runWhenRootExists = (func) => {
425+
if (!doc.documentElement) {
426+
rootFuncArray.push(func)
427+
startObserveHeadBodyExists()
428+
return
429+
}
430+
431+
func()
432+
}
433+
412434
/**
413435
* Run function when document.head exsits.
414436
*/

0 commit comments

Comments
 (0)