Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address final code review feedback - improve comments and test mocking
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
  • Loading branch information
Copilot and mattcosta7 committed Feb 17, 2026
commit b6f97664131e313d2f15bc56c89736d29e40ff12
10 changes: 6 additions & 4 deletions src/lazy-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ function scan(element: ElementLike) {
// FIX 7: Early return optimization
if (pending.size === 0) return

// FIX 7: Create snapshot to iterate safely
// FIX 7: Create snapshot to prevent modification-during-iteration issues
// (concurrent scans may delete tags from pending)
const tagList = Array.from(pending.keys())

for (const tagName of tagList) {
const child: Element | null =
element instanceof Element && element.matches(tagName) ? element : element.querySelector(tagName)
if (customElements.get(tagName) || child) {
// Skip if already triggered and no longer in pending
// Skip if already processed and not re-registered
if (triggered.has(tagName) && !pending.has(tagName)) continue

triggered.add(tagName)
Expand Down Expand Up @@ -167,8 +168,9 @@ export function lazyDefine(tagNameOrObj: string | Record<string, () => void>, si
}

for (const [tagName, callback] of Object.entries(tagNameOrObj)) {
// FIX 6: Late registration - execute immediately if already triggered AND elements exist
if (triggered.has(tagName) && document.querySelector(tagName) !== null) {
// FIX 6: Late registration - execute immediately if already triggered
// Check both triggered state and element existence to avoid executing for removed elements
if (triggered.has(tagName) && document.querySelector(tagName)) {
// eslint-disable-next-line github/no-then
Promise.resolve().then(() => {
try {
Expand Down
8 changes: 3 additions & 5 deletions test/lazy-define.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, fixture, html} from '@open-wc/testing'
import {spy} from 'sinon'
import {spy, stub} from 'sinon'
import {lazyDefine, observe} from '../src/lazy-define.js'

const animationFrame = () => new Promise<unknown>(resolve => requestAnimationFrame(resolve))
Expand Down Expand Up @@ -161,10 +161,8 @@ describe('lazyDefine', () => {
})
const onDefine2 = spy()

// Suppress global error reporting for this test
const originalReportError = globalThis.reportError
const errors: unknown[] = []
globalThis.reportError = (err: unknown) => errors.push(err)
const reportErrorStub = stub(globalThis, 'reportError').callsFake((err: unknown) => errors.push(err))

try {
lazyDefine('error-test-element', onDefine1)
Expand All @@ -180,7 +178,7 @@ describe('lazyDefine', () => {
// Error should have been reported
expect(errors.length).to.be.greaterThan(0)
} finally {
globalThis.reportError = originalReportError
reportErrorStub.restore()
}
})
})
Expand Down
Loading