Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 1c4e85e

Browse files
committed
fix: patch MutationObserver
1 parent 8827e06 commit 1c4e85e

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Diff for: test/zone.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,42 @@ describe('Zone.patch', function () {
324324

325325
});
326326

327+
describe('MutationObserver', function () {
328+
it('should work', function () {
329+
if (!window.MutationObserver) {
330+
console.log('WARNING: skipping MutationObserver test (missing this API)');
331+
return;
332+
}
333+
334+
var flag = false,
335+
elt = document.createElement('div'),
336+
hasParent;
337+
338+
runs(function () {
339+
var ob = new MutationObserver(function () {
340+
hasParent = !!window.zone.parent;
341+
dump('yo')
342+
flag = true;
343+
});
344+
345+
ob.observe(elt, {
346+
childList: true
347+
});
348+
349+
elt.innerHTML = '<p>hey</p>';
350+
});
351+
352+
waitsFor(function() {
353+
return flag;
354+
}, 'promise to resolve', 100);
355+
356+
runs(function() {
357+
expect(hasParent).toBe(true);
358+
});
359+
360+
});
361+
});
362+
327363
describe('XMLHttpRequest', function () {
328364

329365
it('should work with onreadystatechange', function () {

Diff for: zone.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ Zone.patch = function patch () {
267267
'catch'
268268
]);
269269
}
270+
if (window.MutationObserver) {
271+
Zone.patchClass('MutationObserver');
272+
}
270273
};
271274

272275
//
@@ -315,11 +318,22 @@ Zone.patchViaCapturingAllTheEvents = function () {
315318
// TODO: wrap some native API
316319
Zone.patchClass = function (className) {
317320
var OriginalClass = window[className];
321+
if (!OriginalClass) {
322+
return;
323+
}
318324
window[className] = function () {
319-
this._o = new OriginalClass();
325+
var a = Zone.bindArguments(arguments);
326+
switch (a.length) {
327+
case 0: this._o = new OriginalClass(); break;
328+
case 1: this._o = new OriginalClass(a[0]); break;
329+
case 2: this._o = new OriginalClass(a[0], a[1]); break;
330+
case 3: this._o = new OriginalClass(a[0], a[1], a[2]); break;
331+
case 4: this._o = new OriginalClass(a[0], a[1], a[2], a[3]); break;
332+
default: throw new Error('what are you even doing?');
333+
}
320334
};
321335

322-
var instance = (new OriginalClass());
336+
var instance = new OriginalClass(className === 'MutationObserver' ? function () {} : undefined);
323337

324338
var prop;
325339
for (prop in instance) {

0 commit comments

Comments
 (0)