Merge pull request #92 from github/refetch

add refetch API
This commit is contained in:
Manuel Puyol 2022-12-19 11:03:27 -06:00 коммит произвёл GitHub
Родитель 5dfb8999c3 5056f9f867
Коммит 2d85b9f9f6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 26 добавлений и 1 удалений

Просмотреть файл

@ -125,6 +125,11 @@ export default class IncludeFragmentElement extends HTMLElement {
return fetch(request) return fetch(request)
} }
refetch() {
privateData.delete(this)
this.#handleData()
}
#observer = new IntersectionObserver( #observer = new IntersectionObserver(
entries => { entries => {
for (const entry of entries) { for (const entry of entries) {
@ -169,11 +174,17 @@ export default class IncludeFragmentElement extends HTMLElement {
const canceled = !this.dispatchEvent( const canceled = !this.dispatchEvent(
new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}}) new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}})
) )
if (canceled) return if (canceled) {
this.#busy = false
return
}
this.replaceWith(fragment) this.replaceWith(fragment)
this.dispatchEvent(new CustomEvent('include-fragment-replaced')) this.dispatchEvent(new CustomEvent('include-fragment-replaced'))
} catch { } catch {
this.classList.add('is-error') this.classList.add('is-error')
} finally {
this.#busy = false
} }
} }

Просмотреть файл

@ -97,6 +97,7 @@ suite('include-fragment-element', function () {
teardown(() => { teardown(() => {
document.body.innerHTML = '' document.body.innerHTML = ''
}) })
test('create from document.createElement', function () { test('create from document.createElement', function () {
const el = document.createElement('include-fragment') const el = document.createElement('include-fragment')
assert.equal('INCLUDE-FRAGMENT', el.nodeName) assert.equal('INCLUDE-FRAGMENT', el.nodeName)
@ -141,6 +142,19 @@ suite('include-fragment-element', function () {
) )
}) })
test('skips cache when using refetch', async function () {
const el = document.createElement('include-fragment')
el.src = '/count'
let data = await el.data
assert.equal('1', data)
el.refetch()
data = await el.data
assert.equal('2', data)
})
test('data with src attribute', function () { test('data with src attribute', function () {
const el = document.createElement('include-fragment') const el = document.createElement('include-fragment')
el.setAttribute('src', '/hello') el.setAttribute('src', '/hello')