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

Commit 76de58e

Browse files
committed
fix: wrap XMLHttpRequest when we cant patch protos
1 parent 3791431 commit 76de58e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

test/zone.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,56 @@ describe('Zone.patch', function () {
324324

325325
});
326326

327+
describe('XMLHttpRequest', function () {
328+
329+
it('should work with onreadystatechange', function () {
330+
var flag = false,
331+
hasParent;
332+
333+
runs(function () {
334+
var req = new XMLHttpRequest();
335+
req.onreadystatechange = function () {
336+
hasParent = !!window.zone.parent;
337+
flag = true;
338+
};
339+
req.open('get', '/', true);
340+
req.send();
341+
});
342+
343+
waitsFor(function() {
344+
return flag;
345+
}, 'HTTP request to resolve', 100);
346+
347+
runs(function() {
348+
expect(hasParent).toBe(true);
349+
});
350+
});
351+
352+
it('should work with onprogress', function () {
353+
var flag = false,
354+
hasParent;
355+
356+
runs(function () {
357+
var req = new XMLHttpRequest();
358+
req.onprogress = function () {
359+
hasParent = !!window.zone.parent;
360+
flag = true;
361+
};
362+
req.open('get', '/', true);
363+
req.send();
364+
});
365+
366+
waitsFor(function() {
367+
return flag;
368+
}, 'HTTP request to resolve', 100);
369+
370+
runs(function() {
371+
expect(hasParent).toBe(true);
372+
});
373+
});
374+
});
375+
376+
327377
describe('hooks', function () {
328378

329379
beforeEach(function () {

zone.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ Zone.patch = function patch () {
250250
Zone.patchViaPropertyDescriptor();
251251
} else {
252252
Zone.patchViaCapturingAllTheEvents();
253+
Zone.patchClass('XMLHttpRequest');
253254
}
254255

255256
// patch promises
@@ -304,6 +305,38 @@ Zone.patchViaCapturingAllTheEvents = function () {
304305
});
305306
};
306307

308+
// TODO: wrap some native API
309+
Zone.patchClass = function (className) {
310+
var OriginalClass = window[className];
311+
window[className] = function () {
312+
this._o = new OriginalClass();
313+
};
314+
315+
var instance = (new OriginalClass());
316+
317+
var prop;
318+
for (prop in instance) {
319+
(function (prop) {
320+
if (typeof instance[prop] === 'function') {
321+
window[className].prototype[prop] = function () {
322+
return this._o[prop].apply(this._o, arguments);
323+
};
324+
} else {
325+
Object.defineProperty(window[className].prototype, prop, {
326+
set: function (fn) {
327+
if (typeof fn === 'function') {
328+
this._o[prop] = zone.bind(fn);
329+
}
330+
},
331+
get: function () {
332+
return this._o[prop];
333+
}
334+
});
335+
}
336+
}(prop));
337+
};
338+
};
339+
307340
Zone.eventNames = 'copy cut paste abort blur focus canplay canplaythrough change click contextmenu dblclick drag dragend dragenter dragleave dragover dragstart drop durationchange emptied ended input invalid keydown keypress keyup load loadeddata loadedmetadata loadstart mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup pause play playing progress ratechange reset scroll seeked seeking select show stalled submit suspend timeupdate volumechange waiting mozfullscreenchange mozfullscreenerror mozpointerlockchange mozpointerlockerror error'.split(' ');
308341

309342
Zone.init = function init () {

0 commit comments

Comments
 (0)