@@ -21,13 +21,9 @@ const observer = new IntersectionObserver(entries => {
21
21
} )
22
22
23
23
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
- } )
24
+ // Functional stand in for the W3 spec "queue a task" paradigm
25
+ function task ( ) : Promise < void > {
26
+ return new Promise ( resolve => setTimeout ( resolve , 0 ) )
31
27
}
32
28
33
29
async function handleData ( el : IncludeFragmentElement ) {
@@ -152,8 +148,11 @@ export default class IncludeFragmentElement extends HTMLElement {
152
148
load ( ) : Promise < string > {
153
149
observer . unobserve ( this )
154
150
return Promise . resolve ( )
155
- . then ( ( ) => fire ( 'loadstart' , this ) )
156
- . then ( ( ) => this . fetch ( this . request ( ) ) )
151
+ . then ( task )
152
+ . then ( ( ) => {
153
+ this . dispatchEvent ( new Event ( 'loadstart' ) )
154
+ return this . fetch ( this . request ( ) )
155
+ } )
157
156
. then ( response => {
158
157
if ( response . status !== 200 ) {
159
158
throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
@@ -165,10 +164,16 @@ export default class IncludeFragmentElement extends HTMLElement {
165
164
return response . text ( )
166
165
} )
167
166
. then ( data => {
168
- fire ( 'load' , this ) . then ( ( ) => fire ( 'loadend' , this ) )
167
+ task ( ) . then ( ( ) => {
168
+ this . dispatchEvent ( new Event ( 'load' ) )
169
+ this . dispatchEvent ( new Event ( 'loadend' ) )
170
+ } )
169
171
return data
170
172
} , error => {
171
- fire ( 'error' , this ) . then ( ( ) => fire ( 'loadend' , this ) )
173
+ task ( ) . then ( ( ) => {
174
+ this . dispatchEvent ( new Event ( 'error' ) )
175
+ this . dispatchEvent ( new Event ( 'loadend' ) )
176
+ } )
172
177
throw error
173
178
} )
174
179
}
0 commit comments