11const privateData = new WeakMap ( )
22
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-
83function isWildcard ( accept : string | null ) {
94 return accept && ! ! accept . split ( ',' ) . find ( x => x . match ( / ^ \s * \* \/ \* / ) )
105}
@@ -173,17 +168,20 @@ export default class IncludeFragmentElement extends HTMLElement {
173168 }
174169 }
175170
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+
176177 async #fetchDataWithEvents( ) : Promise < string > {
177178 // We mimic the same event order as <img>, including the spec
178179 // which states events must be dispatched after "queue a task".
179180 // https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
180181
181182 try {
182- await task ( )
183-
184- this . dispatchEvent ( new Event ( 'loadstart' ) )
183+ await this . #task( [ 'loadstart' ] )
185184 const response = await this . fetch ( this . request ( ) )
186-
187185 if ( response . status !== 200 ) {
188186 throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
189187 }
@@ -196,17 +194,13 @@ export default class IncludeFragmentElement extends HTMLElement {
196194 // Dispatch `load` and `loadend` async to allow
197195 // the `load()` promise to resolve _before_ these
198196 // events are fired.
199- await task ( )
200- this . dispatchEvent ( new Event ( 'load' ) )
201- this . dispatchEvent ( new Event ( 'loadend' ) )
197+ await this . #task( [ 'load' , 'loadend' ] )
202198 return data
203199 } catch ( error ) {
204200 // Dispatch `error` and `loadend` async to allow
205201 // the `load()` promise to resolve _before_ these
206202 // events are fired.
207- await task ( )
208- this . dispatchEvent ( new Event ( 'error' ) )
209- this . dispatchEvent ( new Event ( 'loadend' ) )
203+ await this . #task( [ 'error' , 'loadend' ] )
210204 throw error
211205 }
212206 }
0 commit comments