Skip to content

Commit c405e3a

Browse files
authored
Merge pull request #79 from github/check-if-network-request-is-already-in-flight
Check if there's a network request already in flight
2 parents b5007c8 + 155a8cb commit c405e3a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export default class IncludeFragmentElement extends HTMLElement {
5050
return this.#getData()
5151
}
5252

53+
#busy = false
54+
5355
attributeChangedCallback(attribute: string, oldVal: string | null): void {
5456
if (attribute === 'src') {
5557
// Source changed after attached so replace element.
@@ -132,6 +134,9 @@ export default class IncludeFragmentElement extends HTMLElement {
132134
)
133135

134136
#handleData(): Promise<void> {
137+
if (this.#busy) return Promise.resolve()
138+
this.#busy = true
139+
135140
this.#observer.unobserve(this)
136141
return this.#getData().then(
137142
(html: string) => {

test/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,24 @@ suite('include-fragment-element', function () {
587587
assert.equal(document.querySelector('#replaced').textContent, 'hello')
588588
})
589589
})
590+
591+
test('include-fragment-replaced is only called once', function () {
592+
const div = document.createElement('div')
593+
div.hidden = true
594+
document.body.append(div)
595+
596+
div.innerHTML = `<include-fragment src="/hello">loading</include-fragment>`
597+
div.firstChild.addEventListener('include-fragment-replaced', () => (loadCount += 1))
598+
599+
let loadCount = 0
600+
setTimeout(() => {
601+
div.hidden = false
602+
}, 0)
603+
604+
return when(div.firstChild, 'include-fragment-replaced').then(() => {
605+
assert.equal(loadCount, 1, 'Load occured too many times')
606+
assert.equal(document.querySelector('include-fragment'), null)
607+
assert.equal(document.querySelector('#replaced').textContent, 'hello')
608+
})
609+
})
590610
})

0 commit comments

Comments
 (0)