@@ -21,10 +21,13 @@ const observer = new IntersectionObserver(entries => {
21
21
} )
22
22
23
23
24
- function fire ( name : string , target : Element ) {
25
- setTimeout ( function ( ) {
26
- target . dispatchEvent ( new Event ( name ) )
27
- } , 0 )
24
+ function fire ( name : string , target : Element ) : Promise < void > {
25
+ return new Promise ( resolve => {
26
+ setTimeout ( function ( ) {
27
+ target . dispatchEvent ( new Event ( name ) )
28
+ resolve ( )
29
+ } , 0 )
30
+ } )
28
31
}
29
32
30
33
async function handleData ( el : IncludeFragmentElement ) {
@@ -149,10 +152,8 @@ export default class IncludeFragmentElement extends HTMLElement {
149
152
load ( ) : Promise < string > {
150
153
observer . unobserve ( this )
151
154
return Promise . resolve ( )
152
- . then ( ( ) => {
153
- fire ( 'loadstart' , this )
154
- return this . fetch ( this . request ( ) )
155
- } )
155
+ . then ( ( ) => fire ( 'loadstart' , this ) )
156
+ . then ( ( ) => this . fetch ( this . request ( ) ) )
156
157
. then ( response => {
157
158
if ( response . status !== 200 ) {
158
159
throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
@@ -161,21 +162,15 @@ export default class IncludeFragmentElement extends HTMLElement {
161
162
if ( ! isWildcard ( this . accept ) && ( ! ct || ! ct . includes ( this . accept ? this . accept : 'text/html' ) ) ) {
162
163
throw new Error ( `Failed to load resource: expected ${ this . accept || 'text/html' } but was ${ ct } ` )
163
164
}
164
- return response
165
+ return response . text ( )
166
+ } )
167
+ . then ( data => {
168
+ fire ( 'load' , this ) . then ( ( ) => fire ( 'loadend' , this ) )
169
+ return Promise . resolve ( data )
170
+ } , error => {
171
+ fire ( 'error' , this ) . then ( ( ) => fire ( 'loadend' , this ) )
172
+ throw error
165
173
} )
166
- . then ( response => response . text ( ) )
167
- . then (
168
- data => {
169
- fire ( 'load' , this )
170
- fire ( 'loadend' , this )
171
- return data
172
- } ,
173
- error => {
174
- fire ( 'error' , this )
175
- fire ( 'loadend' , this )
176
- throw error
177
- }
178
- )
179
174
}
180
175
181
176
fetch ( request : RequestInfo ) : Promise < Response > {
0 commit comments