1
1
const privateData = new WeakMap ( )
2
2
3
- // Functional stand in for the W3 spec "queue a task" paradigm
4
- function task ( ) : Promise < void > {
5
- return new Promise ( resolve => setTimeout ( resolve , 0 ) )
6
- }
7
-
8
3
function isWildcard ( accept : string | null ) {
9
4
return accept && ! ! accept . split ( ',' ) . find ( x => x . match ( / ^ \s * \* \/ \* / ) )
10
5
}
@@ -173,17 +168,20 @@ export default class IncludeFragmentElement extends HTMLElement {
173
168
}
174
169
}
175
170
171
+ // Functional stand in for the W3 spec "queue a task" paradigm
172
+ async #task( eventsToDispatch : string [ ] ) : Promise < void > {
173
+ await new Promise ( resolve => setTimeout ( resolve , 0 ) )
174
+ eventsToDispatch . forEach ( eventType => this . dispatchEvent ( new Event ( eventType ) ) )
175
+ }
176
+
176
177
async #fetchDataWithEvents( ) : Promise < string > {
177
178
// We mimic the same event order as <img>, including the spec
178
179
// which states events must be dispatched after "queue a task".
179
180
// https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
180
181
181
182
try {
182
- await task ( )
183
-
184
- this . dispatchEvent ( new Event ( 'loadstart' ) )
183
+ await this . #task( [ 'loadstart' ] )
185
184
const response = await this . fetch ( this . request ( ) )
186
-
187
185
if ( response . status !== 200 ) {
188
186
throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
189
187
}
@@ -196,17 +194,13 @@ export default class IncludeFragmentElement extends HTMLElement {
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 task ( )
200
- this . dispatchEvent ( new Event ( 'load' ) )
201
- this . dispatchEvent ( new Event ( 'loadend' ) )
197
+ await this . #task( [ 'load' , 'loadend' ] )
202
198
return data
203
199
} catch ( error ) {
204
200
// Dispatch `error` and `loadend` async to allow
205
201
// the `load()` promise to resolve _before_ these
206
202
// events are fired.
207
- await task ( )
208
- this . dispatchEvent ( new Event ( 'error' ) )
209
- this . dispatchEvent ( new Event ( 'loadend' ) )
203
+ await this . #task( [ 'error' , 'loadend' ] )
210
204
throw error
211
205
}
212
206
}
0 commit comments