Skip to content

Commit 563790e

Browse files
CopilotCopilot
andcommitted
Address code review feedback
- Fix redundant check in late registration logic - Use for-of loops instead of traditional for loops for better readability Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b8317cd commit 563790e

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/lazy-define.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ function scan(element: ElementLike) {
7171
// FIX 7: Create snapshot to iterate safely
7272
const tagList = Array.from(pending.keys())
7373

74-
for (let i = 0; i < tagList.length; i++) {
75-
const tagName = tagList[i]
76-
74+
for (const tagName of tagList) {
7775
const child: Element | null =
7876
element instanceof Element && element.matches(tagName) ? element : element.querySelector(tagName)
7977
if (customElements.get(tagName) || child) {
@@ -92,8 +90,7 @@ function scan(element: ElementLike) {
9290

9391
// FIX 5: Wrap callback execution in try-catch and handle rejections
9492
const callbackList = Array.from(callbackSet || [])
95-
for (let j = 0; j < callbackList.length; j++) {
96-
const callback = callbackList[j]
93+
for (const callback of callbackList) {
9794
strategy(tagName)
9895
// eslint-disable-next-line github/no-then
9996
.then(() => {
@@ -134,8 +131,8 @@ export function lazyDefine(tagNameOrObj: string | Record<string, () => void>, si
134131
// FIX 6: Late registration - execute immediately if already triggered
135132
// AND elements exist in DOM
136133
const wasTriggered = triggered.has(tagName)
137-
const elementsExist = wasTriggered && document.querySelector(tagName) !== null
138-
if (elementsExist) {
134+
const elementsExist = document.querySelector(tagName) !== null
135+
if (wasTriggered && elementsExist) {
139136
// eslint-disable-next-line github/no-then
140137
Promise.resolve().then(() => {
141138
try {
@@ -161,11 +158,9 @@ export function observe(target: ElementLike): void {
161158
if (!elementLoader) {
162159
elementLoader = new MutationObserver(mutations => {
163160
if (!pending.size) return
164-
for (let i = 0; i < mutations.length; i++) {
165-
const mutation = mutations[i]
161+
for (const mutation of mutations) {
166162
const nodes = mutation.addedNodes
167-
for (let j = 0; j < nodes.length; j++) {
168-
const node = nodes[j]
163+
for (const node of nodes) {
169164
if (node instanceof Element) {
170165
scan(node)
171166
}

0 commit comments

Comments
 (0)