@@ -98,11 +98,6 @@ function createLoadRecord (state, key, registration) {
98
98
// will be the array of dependency load record or a module namespace
99
99
dependencyInstantiations : undefined ,
100
100
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
-
106
101
// NB optimization and way of ensuring module objects in setters
107
102
// indicates setters which should run pre-execution of that dependency
108
103
// setters is then just for completely executed module objects
@@ -133,12 +128,9 @@ RegisterLoader.prototype[Loader.resolveInstantiate] = function (key, parentKey)
133
128
throw instantiated . evalError ;
134
129
}
135
130
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 )
140
132
. then ( function ( ) {
141
- return ensureEvaluate ( loader , instantiated , instantiated . linkRecord , registry , state , undefined ) ;
133
+ return ensureEvaluate ( loader , instantiated , link , registry , state , undefined ) ;
142
134
} ) ;
143
135
} ) ;
144
136
} ;
@@ -244,13 +236,6 @@ function instantiate (loader, load, link, registry, state) {
244
236
registerDeclarative ( loader , load , link , registration [ 1 ] ) ;
245
237
}
246
238
247
- // shortpath to instantiateDeps
248
- if ( ! link . dependencies . length ) {
249
- link . linked = true ;
250
- if ( loader . trace )
251
- traceLoad ( loader , load , link ) ;
252
- }
253
-
254
239
return load ;
255
240
} )
256
241
. catch ( function ( err ) {
@@ -372,16 +357,16 @@ function registerDeclarative (loader, load, link, declare) {
372
357
}
373
358
}
374
359
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 ;
379
363
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 ) ;
382
365
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 )
385
370
. then ( function ( dependencyInstantiations ) {
386
371
link . dependencyInstantiations = dependencyInstantiations ;
387
372
@@ -406,43 +391,58 @@ function instantiateDeps (loader, load, link, registry, state, seen) {
406
391
}
407
392
}
408
393
}
409
- } ) ) )
410
- . then ( function ( ) {
411
- // now deeply instantiateDeps on each dependencyInstantiation that is a load record
412
- var deepDepsInstantiatePromises = [ ] ;
413
394
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
+ } ) ;
429
397
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 ( ) {
437
400
traceLoad ( loader , load , link ) ;
401
+ return load ;
402
+ } ) ;
438
403
439
- return load ;
440
- } )
441
- . catch ( function ( err ) {
404
+ depsInstantiatePromise = depsInstantiatePromise . catch ( function ( err ) {
442
405
// throw up the instantiateDeps stack
443
- link . depsInstantiatePromise = undefined ;
444
406
throw addToError ( err , 'Loading ' + load . key ) ;
445
407
} ) ;
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
+ } ) ;
446
446
}
447
447
448
448
/*
0 commit comments