@@ -12,7 +12,8 @@ define([
12
12
13
13
var counter = 1 ,
14
14
watchers ,
15
- globalObserver ;
15
+ globalObserver ,
16
+ disabledNodes = [ ] ;
16
17
17
18
watchers = {
18
19
selectors : { } ,
@@ -238,11 +239,58 @@ define([
238
239
} ;
239
240
}
240
241
242
+ /**
243
+ * Verify if the DOM node is a child of a defined disabled node, if so we shouldn't observe provided mutation.
244
+ *
245
+ * @param {Object } mutation - a single mutation
246
+ * @returns {Boolean }
247
+ */
248
+ function shouldObserveMutation ( mutation ) {
249
+ var isDisabled ;
250
+
251
+ if ( disabledNodes . length > 0 ) {
252
+ // Iterate through the disabled nodes and determine if this mutation is occurring inside one of them
253
+ isDisabled = _ . find ( disabledNodes , function ( node ) {
254
+ return node === mutation . target || $ . contains ( node , mutation . target ) ;
255
+ } ) ;
256
+
257
+ // If we find a matching node we should not observe the mutation
258
+ return ! isDisabled ;
259
+ }
260
+
261
+ return true ;
262
+ }
263
+
264
+ /**
265
+ * Should we observe these mutations? Check the first and last mutation to determine if this is a disabled mutation,
266
+ * we check both the first and last in case one has been removed from the DOM during the process of the mutation.
267
+ *
268
+ * @param {Array } mutations - An array of mutation records.
269
+ * @returns {Boolean }
270
+ */
271
+ function shouldObserveMutations ( mutations ) {
272
+ var firstMutation ,
273
+ lastMutation ;
274
+
275
+ if ( mutations . length > 0 ) {
276
+ firstMutation = mutations [ 0 ] ;
277
+ lastMutation = mutations [ mutations . length - 1 ] ;
278
+
279
+ return shouldObserveMutation ( firstMutation ) && shouldObserveMutation ( lastMutation ) ;
280
+ }
281
+
282
+ return true ;
283
+ }
284
+
241
285
globalObserver = new MutationObserver ( function ( mutations ) {
242
- var changes = formChangesLists ( mutations ) ;
286
+ var changes ;
287
+
288
+ if ( shouldObserveMutations ( mutations ) ) {
289
+ changes = formChangesLists ( mutations ) ;
243
290
244
- changes . removed . forEach ( processRemoved ) ;
245
- changes . added . forEach ( processAdded ) ;
291
+ changes . removed . forEach ( processRemoved ) ;
292
+ changes . added . forEach ( processAdded ) ;
293
+ }
246
294
} ) ;
247
295
248
296
globalObserver . observe ( document . body , {
@@ -251,6 +299,16 @@ define([
251
299
} ) ;
252
300
253
301
return {
302
+ /**
303
+ * Disable a node from being observed by the mutations, you may want to disable specific aspects of the
304
+ * application which are heavy on DOM changes. The observer running on some actions could cause significant
305
+ * delays and degrade the performance of that specific part of the application exponentially.
306
+ *
307
+ * @param {HTMLElement } node - a HTML node within the document
308
+ */
309
+ disableNode : function ( node ) {
310
+ disabledNodes . push ( node ) ;
311
+ } ,
254
312
255
313
/**
256
314
* Adds listener for the appearance of nodes that matches provided
0 commit comments