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

Commit

Permalink
fix: improve patching browsers with EventTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Mar 29, 2014
1 parent 648a95d commit 7d3a8b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
26 changes: 26 additions & 0 deletions test/zone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe('Zone.patch', function () {

});

describe('requestAnimationFrame', function () {
var flag, hasParent;

it('should work', function (done) {

runs(function() {
flag = false;
hasParent = false;

window.requestAnimationFrame(function () {
hasParent = !!window.zone.parent;
flag = true;
});
});

waitsFor(function() {
return flag;
}, "requestAnimationFrame to run", 1);

runs(function() {
expect(hasParent).toBe(true);
});

});
});


describe('element', function () {

Expand Down
56 changes: 44 additions & 12 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ Zone.prototype = {
Zone.patchFn = function (obj, fnNames) {
fnNames.forEach(function (name) {
var delegate = obj[name];
zone[name] = function () {
arguments[0] = zone.bind(arguments[0]);
return delegate.apply(obj, arguments);
};

obj[name] = function marker () {
return zone[name].apply(this, arguments);
};
if (delegate) {
zone[name] = function () {
arguments[0] = zone.bind(arguments[0]);
return delegate.apply(obj, arguments);
};

obj[name] = function marker () {
return zone[name].apply(this, arguments);
};
}
});
};

Expand Down Expand Up @@ -182,12 +184,42 @@ Zone.patchEventTargetMethods = function (obj) {
};

Zone.patch = function patch () {
Zone.patchFn(window, ['setTimeout', 'setInterval']);
Zone.patchFn(window, [
'setTimeout',
'setInterval',
'requestAnimationFrame',
'webkitRequestAnimationFrame'
]);
Zone.patchableFn(window, ['alert', 'prompt']);

// patched properties depend on addEventListener, so this comes first
// n.b. EventTarget is not available in all browsers so we patch Node here
Zone.patchEventTargetMethods(Node.prototype);
// patched properties depend on addEventListener, so this needs to come first
if (EventTarget) {
Zone.patchEventTargetMethods(EventTarget.prototype);

// Note: EventTarget is not available in all browsers,
// if it's not available, we instead patch the APIs in the IDL that inherit from EventTarget
} else {
[ ApplicationCache.prototype,
EventSource.prototype,
FileReader.prototype,
InputMethodContext.prototype,
MediaController.prototype,
MessagePort.prototype,
Node.prototype,
Performance.prototype,
SVGElementInstance.prototype,
SharedWorker.prototype,
TextTrack.prototype,
TextTrackCue.prototype,
TextTrackList.prototype,
WebKitNamedFlow.prototype,
Window.prototype,
Worker.prototype,
WorkerGlobalScope.prototype,
XMLHttpRequestEventTarget.prototype,
XMLHttpRequestUpload.prototype
].forEach(patchEventTargetMethods);
}

Zone.patchProperties(HTMLElement.prototype);
Zone.patchProperties(XMLHttpRequest.prototype);
Expand Down

0 comments on commit 7d3a8b1

Please sign in to comment.