@@ -11,7 +11,7 @@ export default RegisterLoader;
11
11
* - loader.register support
12
12
* - hookable higher-level resolve
13
13
* - instantiate hook returning a ModuleNamespace or undefined for es module loading
14
- * - loader error behaviour as in HTML and loader specs, clearing failed modules from registration cache synchronously
14
+ * - loader error behaviour as in HTML and loader specs, caching load and eval errors separately
15
15
* - build tracing support by providing a .trace=true and .loads object format
16
16
*/
17
17
@@ -25,8 +25,10 @@ function RegisterLoader () {
25
25
var deleted = registryDelete . call ( this , key ) ;
26
26
27
27
// also delete from register registry if linked
28
- if ( records . hasOwnProperty ( key ) && ! records [ key ] . linkRecord )
28
+ if ( records . hasOwnProperty ( key ) && ! records [ key ] . linkRecord ) {
29
29
delete records [ key ] ;
30
+ deleted = true ;
31
+ }
30
32
31
33
return deleted ;
32
34
} ;
@@ -74,6 +76,9 @@ function createLoadRecord (state, key, registration) {
74
76
// for already-loaded modules by adding themselves to their importerSetters
75
77
importerSetters : undefined ,
76
78
79
+ loadError : undefined ,
80
+ evalError : undefined ,
81
+
77
82
// in-flight linking record
78
83
linkRecord : {
79
84
// promise for instantiated
@@ -96,9 +101,8 @@ function createLoadRecord (state, key, registration) {
96
101
// indicates if the load and all its dependencies are instantiated and linked
97
102
// but not yet executed
98
103
// mostly just a performance shortpath to avoid rechecking the promises above
99
- linked : false ,
104
+ linked : false
100
105
101
- error : undefined
102
106
// NB optimization and way of ensuring module objects in setters
103
107
// indicates setters which should run pre-execution of that dependency
104
108
// setters is then just for completely executed module objects
@@ -130,10 +134,6 @@ RegisterLoader.prototype[Loader.resolveInstantiate] = function (key, parentKey)
130
134
return instantiateDeps ( loader , instantiated , instantiated . linkRecord , registry , state , [ instantiated ] )
131
135
. then ( function ( ) {
132
136
return ensureEvaluate ( loader , instantiated , instantiated . linkRecord , registry , state , undefined ) ;
133
- } )
134
- . catch ( function ( err ) {
135
- clearLoadErrors ( loader , instantiated ) ;
136
- throw err ;
137
137
} ) ;
138
138
} ) ;
139
139
} ;
@@ -243,7 +243,8 @@ function instantiate (loader, load, link, registry, state) {
243
243
return load ;
244
244
} )
245
245
. catch ( function ( err ) {
246
- throw link . error = addToError ( err , 'Instantiating ' + load . key ) ;
246
+ load . linkRecord = undefined ;
247
+ throw load . loadError = load . loadError || addToError ( err , 'Instantiating ' + load . key ) ;
247
248
} ) ) ;
248
249
}
249
250
@@ -402,6 +403,9 @@ function instantiateDeps (loader, load, link, registry, state, seen) {
402
403
var depLoad = link . dependencyInstantiations [ i ] ;
403
404
var depLink = depLoad . linkRecord ;
404
405
406
+ if ( depLoad . loadError )
407
+ throw depLoad . loadError ;
408
+
405
409
if ( ! depLink || depLink . linked )
406
410
continue ;
407
411
@@ -426,51 +430,12 @@ function instantiateDeps (loader, load, link, registry, state, seen) {
426
430
return load ;
427
431
} )
428
432
. catch ( function ( err ) {
429
- err = addToError ( err , 'Loading ' + load . key ) ;
430
-
431
433
// throw up the instantiateDeps stack
432
- // loads are then synchonously cleared at the top-level through the clearLoadErrors helper below
433
- // this then ensures avoiding partially unloaded tree states
434
- link . error = link . error || err ;
435
-
436
- throw err ;
434
+ load . linkRecord = undefined ;
435
+ throw load . loadError = load . loadError || addToError ( err , 'Loading ' + load . key ) ;
437
436
} ) ;
438
437
}
439
438
440
- // clears an errored load and all its errored dependencies from the loads registry
441
- function clearLoadErrors ( loader , load ) {
442
- var state = loader [ REGISTER_INTERNAL ] ;
443
-
444
- // clear from loads
445
- if ( state . records [ load . key ] === load )
446
- delete state . records [ load . key ] ;
447
-
448
- var link = load . linkRecord ;
449
-
450
- if ( ! link )
451
- return ;
452
-
453
- if ( link . dependencyInstantiations )
454
- link . dependencyInstantiations . forEach ( function ( depLoad , index ) {
455
- if ( ! depLoad || depLoad instanceof ModuleNamespace )
456
- return ;
457
-
458
- if ( depLoad . linkRecord ) {
459
- if ( depLoad . linkRecord . error ) {
460
- // provides a circular reference check
461
- if ( state . records [ depLoad . key ] === depLoad )
462
- clearLoadErrors ( loader , depLoad ) ;
463
- }
464
-
465
- // unregister setters for es dependency load records that will remain
466
- if ( link . setters && depLoad . importerSetters ) {
467
- var setterIndex = depLoad . importerSetters . indexOf ( link . setters [ index ] ) ;
468
- depLoad . importerSetters . splice ( setterIndex , 1 ) ;
469
- }
470
- }
471
- } ) ;
472
- }
473
-
474
439
/*
475
440
* System.register
476
441
*/
@@ -530,19 +495,17 @@ function ensureEvaluate (loader, load, link, registry, state, seen) {
530
495
if ( load . module )
531
496
return load . module ;
532
497
533
- if ( link . error )
534
- throw link . error ;
498
+ if ( load . evalError )
499
+ throw load . evalError ;
535
500
536
501
if ( seen && seen . indexOf ( load ) !== - 1 )
537
502
return load . linkRecord . moduleObj ;
538
503
539
504
// for ES loads we always run ensureEvaluate on top-level, so empty seen is passed regardless
540
505
// for dynamic loads, we pass seen if also dynamic
541
506
var err = doEvaluate ( loader , load , link , registry , state , link . setters ? [ ] : seen || [ ] ) ;
542
- if ( err ) {
543
- clearLoadErrors ( loader , load ) ;
507
+ if ( err )
544
508
throw err ;
545
- }
546
509
547
510
return load . module ;
548
511
}
@@ -587,16 +550,19 @@ function doEvaluate (loader, load, link, registry, state, seen) {
587
550
// custom Module returned from instantiate
588
551
depLink = depLoad . linkRecord ;
589
552
if ( depLink && seen . indexOf ( depLoad ) === - 1 ) {
590
- if ( depLink . error )
591
- err = depLink . error ;
553
+ if ( depLoad . evalError )
554
+ err = depLoad . evalError ;
592
555
else
593
556
// dynamic / declarative boundaries clear the "seen" list
594
557
// we just let cross format circular throw as would happen in real implementations
595
558
err = doEvaluate ( loader , depLoad , depLink , registry , state , depLink . setters ? seen : [ ] ) ;
596
559
}
597
560
598
- if ( err )
599
- return link . error = addToError ( err , 'Evaluating ' + load . key ) ;
561
+ if ( err ) {
562
+ load . linkRecord = undefined ;
563
+ load . evalError = addToError ( err , 'Evaluating ' + load . key ) ;
564
+ return load . evalError ;
565
+ }
600
566
}
601
567
}
602
568
@@ -647,8 +613,11 @@ function doEvaluate (loader, load, link, registry, state, seen) {
647
613
}
648
614
}
649
615
616
+ // dispose link record
617
+ load . linkRecord = undefined ;
618
+
650
619
if ( err )
651
- return link . error = addToError ( err , 'Evaluating ' + load . key ) ;
620
+ return load . evalError = addToError ( err , 'Evaluating ' + load . key ) ;
652
621
653
622
registry [ load . key ] = load . module = new ModuleNamespace ( link . moduleObj ) ;
654
623
@@ -661,9 +630,6 @@ function doEvaluate (loader, load, link, registry, state, seen) {
661
630
load . importerSetters [ i ] ( load . module ) ;
662
631
load . importerSetters = undefined ;
663
632
}
664
-
665
- // dispose link record
666
- load . linkRecord = undefined ;
667
633
}
668
634
669
635
// {} is the closest we can get to call(undefined)
0 commit comments