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

Commit 091f44e

Browse files
committed
feat: support Promise
1 parent 7d3a8b1 commit 091f44e

File tree

2 files changed

+95
-14
lines changed

2 files changed

+95
-14
lines changed

test/zone.spec.js

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,81 @@ describe('Zone.patch', function () {
8585

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

88-
runs(function() {
89-
flag = false;
90-
hasParent = false;
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);
91101

92-
window.requestAnimationFrame(function () {
93-
hasParent = !!window.zone.parent;
94-
flag = true;
102+
runs(function() {
103+
expect(hasParent).toBe(true);
95104
});
105+
96106
});
107+
});
97108

98-
waitsFor(function() {
99-
return flag;
100-
}, "requestAnimationFrame to run", 1);
109+
describe('Promise', function () {
110+
var flag, hasParent;
101111

102-
runs(function() {
103-
expect(hasParent).toBe(true);
112+
beforeEach(function () {
113+
flag = false;
114+
hasParent = false;
104115
});
105116

117+
it('should work with .then', function () {
118+
if (!window.Promise) {
119+
return;
120+
}
121+
122+
runs(function() {
123+
new Promise(function (resolve) {
124+
requestAnimationFrame(resolve);
125+
}).then(function () {
126+
hasParent = !!window.zone.parent;
127+
flag = true;
128+
});
129+
});
130+
131+
waitsFor(function() {
132+
return flag;
133+
}, "requestAnimationFrame to run", 1);
134+
135+
runs(function() {
136+
expect(hasParent).toBe(true);
137+
});
106138
});
107-
});
108139

140+
it('should work with .catch', function () {
141+
if (!window.Promise) {
142+
return;
143+
}
144+
145+
runs(function() {
146+
new Promise(function (resolve, reject) {
147+
requestAnimationFrame(reject);
148+
}).catch(function () {
149+
hasParent = !!window.zone.parent;
150+
flag = true;
151+
});
152+
});
153+
154+
waitsFor(function() {
155+
return flag;
156+
}, "requestAnimationFrame to run", 1);
157+
158+
runs(function() {
159+
expect(hasParent).toBe(true);
160+
});
161+
});
162+
})
109163

110164
describe('element', function () {
111165

zone.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ Zone.patchFn = function (obj, fnNames) {
9696
var delegate = obj[name];
9797
if (delegate) {
9898
zone[name] = function () {
99-
arguments[0] = zone.bind(arguments[0]);
100-
return delegate.apply(obj, arguments);
99+
return delegate.apply(obj, Zone.bindArguments(arguments));
101100
};
102101

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

109+
Zone.patchPrototype = function (obj, fnNames) {
110+
fnNames.forEach(function (name) {
111+
var delegate = obj[name];
112+
if (delegate) {
113+
obj[name] = function () {
114+
return delegate.apply(this, Zone.bindArguments(arguments));
115+
};
116+
}
117+
});
118+
};
119+
120+
Zone.bindArguments = function (args) {
121+
for (var i = args.length - 1; i >= 0; i--) {
122+
if (typeof args[i] === 'function') {
123+
args[i] = zone.bind(args[i]);
124+
}
125+
}
126+
return args;
127+
}
128+
110129
Zone.patchableFn = function (obj, fnNames) {
111130
fnNames.forEach(function (name) {
112131
var delegate = obj[name];
@@ -223,6 +242,14 @@ Zone.patch = function patch () {
223242

224243
Zone.patchProperties(HTMLElement.prototype);
225244
Zone.patchProperties(XMLHttpRequest.prototype);
245+
246+
// patch promises
247+
if (window.Promise) {
248+
Zone.patchPrototype(Promise.prototype, [
249+
'then',
250+
'catch'
251+
]);
252+
}
226253
};
227254

228255
Zone.init = function init () {

0 commit comments

Comments
 (0)