@@ -108,9 +108,7 @@ export default class extends Controller implements LiveController {
108
108
throw new Error ( 'Invalid Element Type' ) ;
109
109
}
110
110
111
- if ( this . element . dataset . poll !== undefined ) {
112
- this . _initiatePolling ( this . element . dataset . poll ) ;
113
- }
111
+ this . _initiatePolling ( ) ;
114
112
115
113
window . addEventListener ( 'beforeunload' , this . markAsWindowUnloaded ) ;
116
114
this . _startAttributesMutationObserver ( ) ;
@@ -123,9 +121,7 @@ export default class extends Controller implements LiveController {
123
121
}
124
122
125
123
disconnect ( ) {
126
- this . pollingIntervals . forEach ( ( interval ) => {
127
- clearInterval ( interval ) ;
128
- } ) ;
124
+ this . _stopAllPolling ( ) ;
129
125
130
126
window . removeEventListener ( 'beforeunload' , this . markAsWindowUnloaded ) ;
131
127
this . element . removeEventListener ( 'live:update-model' , this . handleUpdateModelEvent ) ;
@@ -794,7 +790,14 @@ export default class extends Controller implements LiveController {
794
790
this . _updateModelFromElement ( target , 'change' )
795
791
}
796
792
797
- _initiatePolling ( rawPollConfig : string ) {
793
+ _initiatePolling ( ) {
794
+ this . _stopAllPolling ( ) ;
795
+
796
+ if ( ( this . element as HTMLElement ) . dataset . poll === undefined ) {
797
+ return ;
798
+ }
799
+
800
+ const rawPollConfig = ( this . element as HTMLElement ) . dataset . poll ;
798
801
const directives = parseDirectives ( rawPollConfig || '$render' ) ;
799
802
800
803
directives . forEach ( ( directive ) => {
@@ -959,7 +962,10 @@ export default class extends Controller implements LiveController {
959
962
}
960
963
961
964
/**
962
- * Re-establishes the data-original-data attribute if missing.
965
+ * Helps "re-normalize" certain root element attributes after a re-render.
966
+ *
967
+ * 1) Re-establishes the data-original-data attribute if missing.
968
+ * 2) Stops or re-initializes data-poll
963
969
*
964
970
* This happens if a parent component re-renders a child component
965
971
* and morphdom *updates* child. This commonly happens if a parent
@@ -979,9 +985,13 @@ export default class extends Controller implements LiveController {
979
985
980
986
this . mutationObserver = new MutationObserver ( ( mutations ) => {
981
987
mutations . forEach ( ( mutation ) => {
982
- if ( mutation . type === 'attributes' && ! element . dataset . originalData ) {
983
- this . originalDataJSON = this . valueStore . asJson ( ) ;
984
- this . _exposeOriginalData ( ) ;
988
+ if ( mutation . type === 'attributes' ) {
989
+ if ( ! element . dataset . originalData ) {
990
+ this . originalDataJSON = this . valueStore . asJson ( ) ;
991
+ this . _exposeOriginalData ( ) ;
992
+ }
993
+
994
+ this . _initiatePolling ( ) ;
985
995
}
986
996
} ) ;
987
997
} ) ;
@@ -994,6 +1004,12 @@ export default class extends Controller implements LiveController {
994
1004
private getDefaultDebounce ( ) : number {
995
1005
return this . hasDebounceValue ? this . debounceValue : DEFAULT_DEBOUNCE ;
996
1006
}
1007
+
1008
+ private _stopAllPolling ( ) {
1009
+ this . pollingIntervals . forEach ( ( interval ) => {
1010
+ clearInterval ( interval ) ;
1011
+ } ) ;
1012
+ }
997
1013
}
998
1014
999
1015
/**
0 commit comments