@@ -98,11 +98,6 @@ function createLoadRecord (state, key, registration) {
9898 // will be the array of dependency load record or a module namespace
9999 dependencyInstantiations : undefined ,
100100
101- // indicates if the load and all its dependencies are instantiated and linked
102- // but not yet executed
103- // mostly just a performance shortpath to avoid rechecking the promises above
104- linked : false
105-
106101 // NB optimization and way of ensuring module objects in setters
107102 // indicates setters which should run pre-execution of that dependency
108103 // setters is then just for completely executed module objects
@@ -133,12 +128,9 @@ RegisterLoader.prototype[Loader.resolveInstantiate] = function (key, parentKey)
133128 throw instantiated . evalError ;
134129 }
135130
136- if ( instantiated . linkRecord . linked )
137- return ensureEvaluate ( loader , instantiated , instantiated . linkRecord , registry , state , undefined ) ;
138-
139- return instantiateDeps ( loader , instantiated , instantiated . linkRecord , registry , state , [ instantiated ] )
131+ return deepInstantiateDeps ( loader , instantiated , link , registry , state )
140132 . then ( function ( ) {
141- return ensureEvaluate ( loader , instantiated , instantiated . linkRecord , registry , state , undefined ) ;
133+ return ensureEvaluate ( loader , instantiated , link , registry , state , undefined ) ;
142134 } ) ;
143135 } ) ;
144136} ;
@@ -244,13 +236,6 @@ function instantiate (loader, load, link, registry, state) {
244236 registerDeclarative ( loader , load , link , registration [ 1 ] ) ;
245237 }
246238
247- // shortpath to instantiateDeps
248- if ( ! link . dependencies . length ) {
249- link . linked = true ;
250- if ( loader . trace )
251- traceLoad ( loader , load , link ) ;
252- }
253-
254239 return load ;
255240 } )
256241 . catch ( function ( err ) {
@@ -372,16 +357,16 @@ function registerDeclarative (loader, load, link, declare) {
372357 }
373358}
374359
375- function instantiateDeps ( loader , load , link , registry , state , seen ) {
376- return ( link . depsInstantiatePromise || ( link . depsInstantiatePromise = Promise . resolve ( )
377- . then ( function ( ) {
378- var depsInstantiatePromises = Array ( link . dependencies . length ) ;
360+ function instantiateDeps ( loader , load , link , registry , state ) {
361+ if ( link . depsInstantiatePromise )
362+ return link . depsInstantiatePromise ;
379363
380- for ( var i = 0 ; i < link . dependencies . length ; i ++ )
381- depsInstantiatePromises [ i ] = resolveInstantiateDep ( loader , link . dependencies [ i ] , load . key , registry , state , loader . trace && link . depMap || ( link . depMap = { } ) ) ;
364+ var depsInstantiatePromises = Array ( link . dependencies . length ) ;
382365
383- return Promise . all ( depsInstantiatePromises ) ;
384- } )
366+ for ( var i = 0 ; i < link . dependencies . length ; i ++ )
367+ depsInstantiatePromises [ i ] = resolveInstantiateDep ( loader , link . dependencies [ i ] , load . key , registry , state , loader . trace && link . depMap || ( link . depMap = { } ) ) ;
368+
369+ var depsInstantiatePromise = Promise . all ( depsInstantiatePromises )
385370 . then ( function ( dependencyInstantiations ) {
386371 link . dependencyInstantiations = dependencyInstantiations ;
387372
@@ -406,43 +391,58 @@ function instantiateDeps (loader, load, link, registry, state, seen) {
406391 }
407392 }
408393 }
409- } ) ) )
410- . then ( function ( ) {
411- // now deeply instantiateDeps on each dependencyInstantiation that is a load record
412- var deepDepsInstantiatePromises = [ ] ;
413394
414- for ( var i = 0 ; i < link . dependencies . length ; i ++ ) {
415- var depLoad = link . dependencyInstantiations [ i ] ;
416- var depLink = depLoad . linkRecord ;
417-
418- if ( ! depLink || depLink . linked )
419- continue ;
420-
421- if ( seen . indexOf ( depLoad ) !== - 1 ) {
422- deepDepsInstantiatePromises . push ( depLink . depsInstantiatePromise ) ;
423- continue ;
424- }
425- seen . push ( depLoad ) ;
426-
427- deepDepsInstantiatePromises . push ( instantiateDeps ( loader , depLoad , depLoad . linkRecord , registry , state , seen ) ) ;
428- }
395+ return load ;
396+ } ) ;
429397
430- return Promise . all ( deepDepsInstantiatePromises ) ;
431- } )
432- . then ( function ( ) {
433- // as soon as all dependencies instantiated, we are ready for evaluation so can add to the registry
434- // this can run multiple times, but so what
435- link . linked = true ;
436- if ( loader . trace )
398+ if ( loader . trace )
399+ depsInstantiatePromise = depsInstantiatePromise . then ( function ( ) {
437400 traceLoad ( loader , load , link ) ;
401+ return load ;
402+ } ) ;
438403
439- return load ;
440- } )
441- . catch ( function ( err ) {
404+ depsInstantiatePromise = depsInstantiatePromise . catch ( function ( err ) {
442405 // throw up the instantiateDeps stack
443- link . depsInstantiatePromise = undefined ;
444406 throw addToError ( err , 'Loading ' + load . key ) ;
445407 } ) ;
408+
409+ depsInstantiatePromise . catch ( function ( ) { } ) ;
410+
411+ return link . depsInstantiatePromise = depsInstantiatePromise ;
412+ }
413+
414+ function deepInstantiateDeps ( loader , load , link , registry , state ) {
415+ return new Promise ( function ( resolve , reject ) {
416+ var seen = [ ] ;
417+ var loadCnt = 0 ;
418+ function queueLoad ( load ) {
419+ var link = load . linkRecord ;
420+ if ( ! link )
421+ return ;
422+
423+ if ( seen . indexOf ( load ) !== - 1 )
424+ return ;
425+ seen . push ( load ) ;
426+
427+ loadCnt ++ ;
428+ instantiateDeps ( loader , load , link , registry , state )
429+ . then ( processLoad , reject ) ;
430+ }
431+ function processLoad ( load ) {
432+ loadCnt -- ;
433+ var link = load . linkRecord ;
434+ if ( link ) {
435+ for ( var i = 0 ; i < link . dependencies . length ; i ++ ) {
436+ var depLoad = link . dependencyInstantiations [ i ] ;
437+ if ( ! ( depLoad instanceof ModuleNamespace ) )
438+ queueLoad ( depLoad ) ;
439+ }
440+ }
441+ if ( loadCnt === 0 )
442+ resolve ( ) ;
443+ }
444+ queueLoad ( load ) ;
445+ } ) ;
446446}
447447
448448/*
0 commit comments