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

Commit

Permalink
feat: support Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Mar 29, 2014
1 parent 7d3a8b1 commit 091f44e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
78 changes: 66 additions & 12 deletions test/zone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,81 @@ describe('Zone.patch', function () {

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

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

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

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

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

});
});

waitsFor(function() {
return flag;
}, "requestAnimationFrame to run", 1);
describe('Promise', function () {
var flag, hasParent;

runs(function() {
expect(hasParent).toBe(true);
beforeEach(function () {
flag = false;
hasParent = false;
});

it('should work with .then', function () {
if (!window.Promise) {
return;
}

runs(function() {
new Promise(function (resolve) {
requestAnimationFrame(resolve);
}).then(function () {
hasParent = !!window.zone.parent;
flag = true;
});
});

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

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

it('should work with .catch', function () {
if (!window.Promise) {
return;
}

runs(function() {
new Promise(function (resolve, reject) {
requestAnimationFrame(reject);
}).catch(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
31 changes: 29 additions & 2 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ Zone.patchFn = function (obj, fnNames) {
var delegate = obj[name];
if (delegate) {
zone[name] = function () {
arguments[0] = zone.bind(arguments[0]);
return delegate.apply(obj, arguments);
return delegate.apply(obj, Zone.bindArguments(arguments));
};

obj[name] = function marker () {
Expand All @@ -107,6 +106,26 @@ Zone.patchFn = function (obj, fnNames) {
});
};

Zone.patchPrototype = function (obj, fnNames) {
fnNames.forEach(function (name) {
var delegate = obj[name];
if (delegate) {
obj[name] = function () {
return delegate.apply(this, Zone.bindArguments(arguments));
};
}
});
};

Zone.bindArguments = function (args) {
for (var i = args.length - 1; i >= 0; i--) {
if (typeof args[i] === 'function') {
args[i] = zone.bind(args[i]);
}
}
return args;
}

Zone.patchableFn = function (obj, fnNames) {
fnNames.forEach(function (name) {
var delegate = obj[name];
Expand Down Expand Up @@ -223,6 +242,14 @@ Zone.patch = function patch () {

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

// patch promises
if (window.Promise) {
Zone.patchPrototype(Promise.prototype, [
'then',
'catch'
]);
}
};

Zone.init = function init () {
Expand Down

0 comments on commit 091f44e

Please sign in to comment.