@@ -32,6 +32,12 @@ addToLibrary({
3232 return byteArray ;
3333 } ,
3434
35+ // Legacy version of FS_preloadFile that uses callback rather than async
36+ $FS_createPreloadedFile__deps : [ '$FS_preloadFile' ] ,
37+ $FS_createPreloadedFile : ( parent , name , url , canRead , canWrite , onload , onerror , dontCreateFile , canOwn , preFinish ) => {
38+ FS_preloadFile ( parent , name , url , canRead , canWrite , dontCreateFile , canOwn , preFinish ) . then ( onload ) . catch ( onerror ) ;
39+ } ,
40+
3541 // Preloads a file asynchronously. You can call this before run, for example in
3642 // preRun. run will be delayed until this file arrives and is set up.
3743 // If you call it after run(), you may want to pause the main loop until it
@@ -44,39 +50,33 @@ addToLibrary({
4450 // You can also call this with a typed array instead of a url. It will then
4551 // do preloading for the Image/Audio part, as if the typed array were the
4652 // result of an XHR that you did manually.
47- $FS_createPreloadedFile__deps : [
53+ $FS_preloadFile__deps : [
4854 '$asyncLoad' ,
4955 '$PATH_FS' ,
5056 '$FS_createDataFile' ,
5157 '$getUniqueRunDependency' ,
5258 '$FS_handledByPreloadPlugin' ,
5359 ] ,
54- $FS_createPreloadedFile : ( parent , name , url , canRead , canWrite , onload , onerror , dontCreateFile , canOwn , preFinish ) => {
60+ $FS_preloadFile : async ( parent , name , url , canRead , canWrite , dontCreateFile , canOwn , preFinish ) => {
5561 // TODO we should allow people to just pass in a complete filename instead
5662 // of parent and name being that we just join them anyways
5763 var fullname = name ? PATH_FS . resolve ( PATH . join2 ( parent , name ) ) : parent ;
5864 var dep = getUniqueRunDependency ( `cp ${ fullname } ` ) ; // might have several active requests for the same fullname
59- function processData ( byteArray ) {
60- FS_handledByPreloadPlugin ( byteArray , fullname )
61- . then ( ( byteArray ) => {
62- preFinish ?. ( ) ;
63- if ( ! dontCreateFile ) {
64- FS_createDataFile ( parent , name , byteArray , canRead , canWrite , canOwn ) ;
65- }
66- onload ?. ( ) ;
67- removeRunDependency ( dep ) ;
68- } )
69- . catch ( ( ) => {
70- onerror ?. ( ) ;
71- removeRunDependency ( dep ) ;
72- } ) ;
73- }
74-
7565 addRunDependency ( dep ) ;
76- if ( typeof url == 'string' ) {
77- asyncLoad ( url ) . then ( processData , onerror ) ;
78- } else {
79- processData ( url ) ;
66+
67+ try {
68+ var byteArray = url ;
69+ if ( typeof url == 'string' ) {
70+ byteArray = await asyncLoad ( url ) ;
71+ }
72+
73+ byteArray = await FS_handledByPreloadPlugin ( byteArray , fullname ) ;
74+ preFinish ?. ( ) ;
75+ if ( ! dontCreateFile ) {
76+ FS_createDataFile ( parent , name , byteArray , canRead , canWrite , canOwn ) ;
77+ }
78+ } finally {
79+ removeRunDependency ( dep ) ;
8080 }
8181 } ,
8282#endif
0 commit comments