5
5
*/
6
6
7
7
var Fetch = {
8
- // Map of integer fetch id to XHR request object
9
- xhrs : { } ,
8
+ // HandleAllocator for XHR request object
9
+ // xhrs: undefined ,
10
10
11
- // The web worker that runs proxied file I/O requests. (this field is populated on demand, start as undefined to save code size)
11
+ // The web worker that runs proxied file I/O requests. (this field is
12
+ // populated on demand, start as undefined to save code size)
12
13
// worker: undefined,
13
14
14
15
// Specifies an instance to the IndexedDB database. The database is opened
15
- // as a preload step before the Emscripten application starts. (this field is populated on demand, start as undefined to save code size)
16
+ // as a preload step before the Emscripten application starts. (this field is
17
+ // populated on demand, start as undefined to save code size)
16
18
// dbInstance: undefined,
17
19
18
20
#if FETCH_SUPPORT_INDEXEDDB
@@ -40,8 +42,12 @@ var Fetch = {
40
42
} ,
41
43
#endif
42
44
43
- staticInit : function ( ) {
45
+ init : function ( ) {
46
+ Fetch . xhrs = new HandleAllocator ( ) ;
44
47
#if FETCH_SUPPORT_INDEXEDDB
48
+ #if PTHREADS
49
+ if ( ENVIRONMENT_IS_PTHREAD ) return ;
50
+ #endif
45
51
var onsuccess = ( db ) => {
46
52
#if FETCH_DEBUG
47
53
dbg ( 'fetch: IndexedDB successfully opened.' ) ;
@@ -293,8 +299,12 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
293
299
xhr . setRequestHeader ( keyStr , valueStr ) ;
294
300
}
295
301
}
296
- var id = { { { makeGetValue ( 'fetch' , C_STRUCTS . emscripten_fetch_t . id , 'u32' ) } } } ;
297
- Fetch . xhrs [ id ] = xhr ;
302
+
303
+ var id = Fetch . xhrs . allocate ( xhr ) ;
304
+ #if FETCH_DEBUG
305
+ dbg ( 'fetch: id=' + id ) ;
306
+ #endif
307
+ { { { makeSetValue ( 'fetch' , C_STRUCTS . emscripten_fetch_t . id , 'id' , 'u32' ) } } } ;
298
308
var data = ( dataPtr && dataLength ) ? HEAPU8 . slice ( dataPtr , dataPtr + dataLength ) : null ;
299
309
// TODO: Support specifying custom headers to the request.
300
310
@@ -334,7 +344,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
334
344
335
345
xhr . onload = ( e ) => {
336
346
// check if xhr was aborted by user and don't try to call back
337
- if ( ! ( id in Fetch . xhrs ) ) {
347
+ if ( ! Fetch . xhrs . has ( id ) ) {
338
348
return ;
339
349
}
340
350
saveResponseAndStatus ( ) ;
@@ -352,7 +362,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
352
362
} ;
353
363
xhr . onerror = ( e ) => {
354
364
// check if xhr was aborted by user and don't try to call back
355
- if ( ! ( id in Fetch . xhrs ) ) {
365
+ if ( ! Fetch . xhrs . has ( id ) ) {
356
366
return ;
357
367
}
358
368
#if FETCH_DEBUG
@@ -363,7 +373,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
363
373
} ;
364
374
xhr . ontimeout = ( e ) => {
365
375
// check if xhr was aborted by user and don't try to call back
366
- if ( ! ( id in Fetch . xhrs ) ) {
376
+ if ( ! Fetch . xhrs . has ( id ) ) {
367
377
return ;
368
378
}
369
379
#if FETCH_DEBUG
@@ -373,7 +383,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
373
383
} ;
374
384
xhr . onprogress = ( e ) => {
375
385
// check if xhr was aborted by user and don't try to call back
376
- if ( ! ( id in Fetch . xhrs ) ) {
386
+ if ( ! Fetch . xhrs . has ( id ) ) {
377
387
return ;
378
388
}
379
389
var ptrLen = ( fetchAttrLoadToMemory && fetchAttrStreamData && xhr . response ) ? xhr . response . byteLength : 0 ;
@@ -405,7 +415,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
405
415
} ;
406
416
xhr . onreadystatechange = ( e ) => {
407
417
// check if xhr was aborted by user and don't try to call back
408
- if ( ! ( id in Fetch . xhrs ) ) {
418
+ if ( ! Fetch . xhrs . has ( id ) ) {
409
419
{ { { runtimeKeepalivePop ( ) } } }
410
420
return ;
411
421
}
@@ -560,27 +570,27 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
560
570
}
561
571
562
572
function fetchGetResponseHeadersLength ( id ) {
563
- return lengthBytesUTF8 ( Fetch . xhrs [ id ] . getAllResponseHeaders ( ) ) + 1 ;
573
+ return lengthBytesUTF8 ( Fetch . xhrs . get ( id ) . getAllResponseHeaders ( ) ) + 1 ;
564
574
}
565
575
566
576
function fetchGetResponseHeaders ( id , dst , dstSizeBytes ) {
567
- var responseHeaders = Fetch . xhrs [ id ] . getAllResponseHeaders ( ) ;
568
- var lengthBytes = lengthBytesUTF8 ( responseHeaders ) + 1 ;
569
- stringToUTF8 ( responseHeaders , dst , dstSizeBytes ) ;
570
- return Math . min ( lengthBytes , dstSizeBytes ) ;
577
+ var responseHeaders = Fetch . xhrs . get ( id ) . getAllResponseHeaders ( ) ;
578
+ var lengthBytes = lengthBytesUTF8 ( responseHeaders ) + 1 ;
579
+ stringToUTF8 ( responseHeaders , dst , dstSizeBytes ) ;
580
+ return Math . min ( lengthBytes , dstSizeBytes ) ;
571
581
}
572
582
573
583
//Delete the xhr JS object, allowing it to be garbage collected.
574
584
function fetchFree ( id ) {
575
585
#if FETCH_DEBUG
576
- dbg ( "fetch: Deleting id:" + id + " of " + Fetch . xhrs ) ;
586
+ dbg ( "fetch: fetchFree id:" + id ) ;
577
587
#endif
578
- var xhr = Fetch . xhrs [ id ] ;
579
- if ( xhr ) {
580
- delete Fetch . xhrs [ id ] ;
581
- // check if fetch is still in progress and should be aborted
582
- if ( xhr . readyState > 0 && xhr . readyState < 4 ) {
583
- xhr . abort ( ) ;
584
- }
588
+ if ( Fetch . xhrs . has ( id ) ) {
589
+ var xhr = Fetch . xhrs . get ( id ) ;
590
+ Fetch . xhrs . free ( id ) ;
591
+ // check if fetch is still in progress and should be aborted
592
+ if ( xhr . readyState > 0 && xhr . readyState < 4 ) {
593
+ xhr . abort ( ) ;
585
594
}
595
+ }
586
596
}
0 commit comments