@@ -25,30 +25,50 @@ class StimulusLazyControllerHandler {
25
25
this . lazyLoadNewControllers ( document . documentElement ) ;
26
26
}
27
27
lazyLoadExistingControllers ( element ) {
28
- this . queryControllerNamesWithin ( element ) . forEach ( ( controllerName ) => this . loadLazyController ( controllerName ) ) ;
28
+ Array . from ( element . querySelectorAll ( `[${ controllerAttribute } ]` ) )
29
+ . flatMap ( extractControllerNamesFrom )
30
+ . forEach ( ( controllerName ) => this . loadLazyController ( controllerName ) ) ;
29
31
}
30
- async loadLazyController ( name ) {
31
- if ( canRegisterController ( name , this . application ) ) {
32
- if ( this . lazyControllers [ name ] === undefined ) {
33
- return ;
34
- }
35
- const controllerModule = await this . lazyControllers [ name ] ( ) ;
36
- registerController ( name , controllerModule . default , this . application ) ;
32
+ loadLazyController ( name ) {
33
+ if ( ! this . lazyControllers [ name ] ) {
34
+ return ;
35
+ }
36
+ const controllerLoader = this . lazyControllers [ name ] ;
37
+ delete this . lazyControllers [ name ] ;
38
+ if ( ! canRegisterController ( name , this . application ) ) {
39
+ return ;
37
40
}
41
+ this . application . logDebugActivity ( name , 'lazy:loading' ) ;
42
+ controllerLoader ( )
43
+ . then ( ( controllerModule ) => {
44
+ this . application . logDebugActivity ( name , 'lazy:loaded' ) ;
45
+ registerController ( name , controllerModule . default , this . application ) ;
46
+ } )
47
+ . catch ( ( error ) => {
48
+ console . error ( `Error loading controller "${ name } ":` , error ) ;
49
+ } ) ;
38
50
}
39
51
lazyLoadNewControllers ( element ) {
52
+ if ( Object . keys ( this . lazyControllers ) . length === 0 ) {
53
+ return ;
54
+ }
40
55
new MutationObserver ( ( mutationsList ) => {
41
- for ( const { attributeName, target, type } of mutationsList ) {
42
- switch ( type ) {
43
- case 'attributes' : {
44
- if ( attributeName === controllerAttribute &&
45
- target . getAttribute ( controllerAttribute ) ) {
46
- extractControllerNamesFrom ( target ) . forEach ( ( controllerName ) => this . loadLazyController ( controllerName ) ) ;
56
+ for ( const mutation of mutationsList ) {
57
+ switch ( mutation . type ) {
58
+ case 'childList' : {
59
+ for ( const node of mutation . addedNodes ) {
60
+ if ( node instanceof Element ) {
61
+ extractControllerNamesFrom ( node ) . forEach ( ( controllerName ) => {
62
+ this . loadLazyController ( controllerName ) ;
63
+ } ) ;
64
+ }
47
65
}
48
66
break ;
49
67
}
50
- case 'childList' : {
51
- this . lazyLoadExistingControllers ( target ) ;
68
+ case 'attributes' : {
69
+ if ( mutation . attributeName === controllerAttribute ) {
70
+ extractControllerNamesFrom ( mutation . target ) . forEach ( ( controllerName ) => this . loadLazyController ( controllerName ) ) ;
71
+ }
52
72
}
53
73
}
54
74
}
@@ -58,9 +78,6 @@ class StimulusLazyControllerHandler {
58
78
childList : true ,
59
79
} ) ;
60
80
}
61
- queryControllerNamesWithin ( element ) {
62
- return Array . from ( element . querySelectorAll ( `[${ controllerAttribute } ]` ) ) . flatMap ( extractControllerNamesFrom ) ;
63
- }
64
81
}
65
82
function registerController ( name , controller , application ) {
66
83
if ( canRegisterController ( name , application ) ) {
0 commit comments