@@ -16,13 +16,12 @@ const L10N_ELEMENT_QUERY = `[${L10NID_ATTR_NAME}]`;
16
16
*/
17
17
export default class DOMLocalization extends Localization {
18
18
/**
19
- * @param {Window } windowElement
20
19
* @param {Array<String> } resourceIds - List of resource IDs
21
20
* @param {Function } generateMessages - Function that returns a
22
21
* generator over MessageContexts
23
22
* @returns {DOMLocalization }
24
23
*/
25
- constructor ( windowElement , resourceIds , generateMessages ) {
24
+ constructor ( resourceIds , generateMessages ) {
26
25
super ( resourceIds , generateMessages ) ;
27
26
28
27
// A Set of DOM trees observed by the `MutationObserver`.
@@ -31,10 +30,8 @@ export default class DOMLocalization extends Localization {
31
30
this . pendingrAF = null ;
32
31
// list of elements pending for translation.
33
32
this . pendingElements = new Set ( ) ;
34
- this . windowElement = windowElement ;
35
- this . mutationObserver = new windowElement . MutationObserver (
36
- mutations => this . translateMutations ( mutations )
37
- ) ;
33
+ this . windowElement = null ;
34
+ this . mutationObserver = null ;
38
35
39
36
this . observerConfig = {
40
37
attribute : true ,
@@ -132,6 +129,19 @@ export default class DOMLocalization extends Localization {
132
129
}
133
130
}
134
131
132
+ if ( this . windowElement ) {
133
+ if ( this . windowElement !== newRoot . ownerGlobal ) {
134
+ throw new Error ( `Cannot connect a root:
135
+ DOMLocalization already has a root from a different window.` ) ;
136
+ }
137
+ } else {
138
+ this . windowElement = newRoot . ownerGlobal ;
139
+ this . mutationObserver = new this . windowElement . MutationObserver (
140
+ mutations => this . translateMutations ( mutations )
141
+ ) ;
142
+ }
143
+
144
+
135
145
this . roots . add ( newRoot ) ;
136
146
this . mutationObserver . observe ( newRoot , this . observerConfig ) ;
137
147
}
@@ -150,11 +160,20 @@ export default class DOMLocalization extends Localization {
150
160
*/
151
161
disconnectRoot ( root ) {
152
162
this . roots . delete ( root ) ;
153
- // Pause and resume the mutation observer to stop observing `root`.
163
+ // Pause the mutation observer to stop observing `root`.
154
164
this . pauseObserving ( ) ;
155
- this . resumeObserving ( ) ;
156
165
157
- return this . roots . size === 0 ;
166
+ if ( this . roots . size === 0 ) {
167
+ this . mutationObserver = null ;
168
+ this . windowElement = null ;
169
+ this . pendingrAF = null ;
170
+ this . pendingElements . clear ( ) ;
171
+ return true ;
172
+ }
173
+
174
+ // Resume observing all other roots.
175
+ this . resumeObserving ( ) ;
176
+ return false ;
158
177
}
159
178
160
179
/**
@@ -175,6 +194,10 @@ export default class DOMLocalization extends Localization {
175
194
* @private
176
195
*/
177
196
pauseObserving ( ) {
197
+ if ( ! this . mutationObserver ) {
198
+ return ;
199
+ }
200
+
178
201
this . translateMutations ( this . mutationObserver . takeRecords ( ) ) ;
179
202
this . mutationObserver . disconnect ( ) ;
180
203
}
@@ -185,6 +208,10 @@ export default class DOMLocalization extends Localization {
185
208
* @private
186
209
*/
187
210
resumeObserving ( ) {
211
+ if ( ! this . mutationObserver ) {
212
+ return ;
213
+ }
214
+
188
215
for ( const root of this . roots ) {
189
216
this . mutationObserver . observe ( root , this . observerConfig ) ;
190
217
}
0 commit comments