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

Commit 7d3a8b1

Browse files
committed
fix: improve patching browsers with EventTarget
1 parent 648a95d commit 7d3a8b1

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

Diff for: test/zone.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ describe('Zone.patch', function () {
8080

8181
});
8282

83+
describe('requestAnimationFrame', function () {
84+
var flag, hasParent;
85+
86+
it('should work', function (done) {
87+
88+
runs(function() {
89+
flag = false;
90+
hasParent = false;
91+
92+
window.requestAnimationFrame(function () {
93+
hasParent = !!window.zone.parent;
94+
flag = true;
95+
});
96+
});
97+
98+
waitsFor(function() {
99+
return flag;
100+
}, "requestAnimationFrame to run", 1);
101+
102+
runs(function() {
103+
expect(hasParent).toBe(true);
104+
});
105+
106+
});
107+
});
108+
83109

84110
describe('element', function () {
85111

Diff for: zone.js

+44-12
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ Zone.prototype = {
9494
Zone.patchFn = function (obj, fnNames) {
9595
fnNames.forEach(function (name) {
9696
var delegate = obj[name];
97-
zone[name] = function () {
98-
arguments[0] = zone.bind(arguments[0]);
99-
return delegate.apply(obj, arguments);
100-
};
101-
102-
obj[name] = function marker () {
103-
return zone[name].apply(this, arguments);
104-
};
97+
if (delegate) {
98+
zone[name] = function () {
99+
arguments[0] = zone.bind(arguments[0]);
100+
return delegate.apply(obj, arguments);
101+
};
102+
103+
obj[name] = function marker () {
104+
return zone[name].apply(this, arguments);
105+
};
106+
}
105107
});
106108
};
107109

@@ -182,12 +184,42 @@ Zone.patchEventTargetMethods = function (obj) {
182184
};
183185

184186
Zone.patch = function patch () {
185-
Zone.patchFn(window, ['setTimeout', 'setInterval']);
187+
Zone.patchFn(window, [
188+
'setTimeout',
189+
'setInterval',
190+
'requestAnimationFrame',
191+
'webkitRequestAnimationFrame'
192+
]);
186193
Zone.patchableFn(window, ['alert', 'prompt']);
187194

188-
// patched properties depend on addEventListener, so this comes first
189-
// n.b. EventTarget is not available in all browsers so we patch Node here
190-
Zone.patchEventTargetMethods(Node.prototype);
195+
// patched properties depend on addEventListener, so this needs to come first
196+
if (EventTarget) {
197+
Zone.patchEventTargetMethods(EventTarget.prototype);
198+
199+
// Note: EventTarget is not available in all browsers,
200+
// if it's not available, we instead patch the APIs in the IDL that inherit from EventTarget
201+
} else {
202+
[ ApplicationCache.prototype,
203+
EventSource.prototype,
204+
FileReader.prototype,
205+
InputMethodContext.prototype,
206+
MediaController.prototype,
207+
MessagePort.prototype,
208+
Node.prototype,
209+
Performance.prototype,
210+
SVGElementInstance.prototype,
211+
SharedWorker.prototype,
212+
TextTrack.prototype,
213+
TextTrackCue.prototype,
214+
TextTrackList.prototype,
215+
WebKitNamedFlow.prototype,
216+
Window.prototype,
217+
Worker.prototype,
218+
WorkerGlobalScope.prototype,
219+
XMLHttpRequestEventTarget.prototype,
220+
XMLHttpRequestUpload.prototype
221+
].forEach(patchEventTargetMethods);
222+
}
191223

192224
Zone.patchProperties(HTMLElement.prototype);
193225
Zone.patchProperties(XMLHttpRequest.prototype);

0 commit comments

Comments
 (0)