@@ -129,10 +129,10 @@ export default class IncludeFragmentElement extends HTMLElement {
129
129
)
130
130
131
131
async #handleData( ) : Promise < void > {
132
- if ( this . #busy) return Promise . resolve ( )
132
+ if ( this . #busy) return
133
133
this . #busy = true
134
+ this . #observer. unobserve ( this )
134
135
try {
135
- this . #observer. unobserve ( this )
136
136
const html = await this . #getData( )
137
137
138
138
const template = document . createElement ( 'template' )
@@ -142,9 +142,7 @@ export default class IncludeFragmentElement extends HTMLElement {
142
142
const canceled = ! this . dispatchEvent (
143
143
new CustomEvent ( 'include-fragment-replace' , { cancelable : true , detail : { fragment} } )
144
144
)
145
- if ( canceled ) {
146
- return
147
- }
145
+ if ( canceled ) return
148
146
this . replaceWith ( fragment )
149
147
this . dispatchEvent ( new CustomEvent ( 'include-fragment-replaced' ) )
150
148
} catch ( _ ) {
@@ -181,28 +179,28 @@ export default class IncludeFragmentElement extends HTMLElement {
181
179
// which states events must be dispatched after "queue a task".
182
180
// https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
183
181
184
- try {
185
- await this . #task( [ 'loadstart' ] )
186
- const response = await this . fetch ( this . request ( ) )
187
- if ( response . status !== 200 ) {
188
- throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
189
- }
190
- const ct = response . headers . get ( 'Content-Type' )
191
- if ( ! isWildcard ( this . accept ) && ( ! ct || ! ct . includes ( this . accept ? this . accept : 'text/html' ) ) ) {
192
- throw new Error ( `Failed to load resource: expected ${ this . accept || 'text/html' } but was ${ ct } ` )
193
- }
194
- const data = await response . text ( )
182
+ await this . #task( [ 'loadstart' ] )
183
+ const response = await this . fetch ( this . request ( ) )
184
+ if ( response . status !== 200 ) {
185
+ throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
186
+ }
187
+ const ct = response . headers . get ( 'Content-Type' )
188
+ if ( ! isWildcard ( this . accept ) && ( ! ct || ! ct . includes ( this . accept ? this . accept : 'text/html' ) ) ) {
189
+ throw new Error ( `Failed to load resource: expected ${ this . accept || 'text/html' } but was ${ ct } ` )
190
+ }
191
+ const data = await response . text ( )
195
192
193
+ try {
196
194
// Dispatch `load` and `loadend` async to allow
197
195
// the `load()` promise to resolve _before_ these
198
196
// events are fired.
199
- await this . #task( [ 'load' , 'loadend' ] )
197
+ this . #task( [ 'load' , 'loadend' ] )
200
198
return data
201
199
} catch ( error ) {
202
200
// Dispatch `error` and `loadend` async to allow
203
201
// the `load()` promise to resolve _before_ these
204
202
// events are fired.
205
- await this . #task( [ 'error' , 'loadend' ] )
203
+ this . #task( [ 'error' , 'loadend' ] )
206
204
throw error
207
205
}
208
206
}
0 commit comments