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

Commit 4d3d524

Browse files
committed
feat(fetch): patch the fetch API
fixes #108
1 parent 8bce00e commit 4d3d524

File tree

6 files changed

+122
-23
lines changed

6 files changed

+122
-23
lines changed

Diff for: karma-browserify.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = function (config) {
66
files: [
77
'test/util.js',
88
'test/commonjs.spec.js',
9-
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false},
9+
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
1010
{pattern: 'lib/**/*.js', watched: true, served: false, included: false}
1111
],
1212

Diff for: karma-microtasks.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function (config) {
88
'test/setup-microtask.js',
99
'dist/*-zone.js',
1010
'test/**/*.spec.js',
11-
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false},
11+
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
1212
{pattern: 'lib/**/*.js', watched: true, served: false, included: false}
1313
],
1414

Diff for: karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function (config) {
99
'dist/*-zone.js',
1010
//'test/lib/brick.js',
1111
'test/**/*.spec.js',
12-
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false},
12+
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
1313
{pattern: 'lib/**/*.js', watched: true, served: false, included: false}
1414
],
1515

Diff for: lib/patch/promise.js

+34
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ if (global.Promise) {
4747
};
4848
}
4949

50+
51+
function _patchPromiseFnsOnObject(objectPath, fnNames) {
52+
var obj = global;
53+
54+
var exists = objectPath.every(function (segment) {
55+
obj = obj[segment];
56+
return obj;
57+
});
58+
59+
if (!exists) {
60+
return;
61+
}
62+
63+
fnNames.forEach(function (name) {
64+
var fn = obj[name];
65+
if (fn) {
66+
obj[name] = bindPromiseFn(fn);
67+
}
68+
});
69+
}
70+
5071
function _patchThenable(thenable) {
5172
var then = thenable.then;
5273
thenable.then = function () {
@@ -67,11 +88,24 @@ function _patchThenable(thenable) {
6788

6889

6990
function apply() {
91+
// Patch .then() and .catch() on native Promises to execute callbacks in the zone where
92+
// those functions are called.
7093
if (global.Promise) {
7194
utils.patchPrototype(Promise.prototype, [
7295
'then',
7396
'catch'
7497
]);
98+
99+
// Patch browser APIs that return a Promise
100+
var patchFns = [
101+
// fetch
102+
[[], ['fetch']],
103+
[['Response', 'prototype'], ['arrayBuffer', 'blob', 'json', 'text']]
104+
];
105+
106+
patchFns.forEach(function(objPathAndFns) {
107+
_patchPromiseFnsOnObject(objPathAndFns[0], objPathAndFns[1]);
108+
});
75109
}
76110
}
77111

Diff for: test/assets/sample.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"hello": "world"}

Diff for: test/patch/Promise.spec.js

+84-20
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,98 @@
33
describe('Promise', ifEnvSupports('Promise', function () {
44
var testZone = window.zone.fork();
55

6-
it('should work with .then', function (done) {
7-
var resolve;
8-
9-
testZone.run(function() {
10-
new Promise(function (resolveFn) {
11-
resolve = resolveFn;
12-
}).then(function () {
13-
expect(window.zone).toBeDirectChildOf(testZone);
14-
done();
6+
describe('Promise API', function () {
7+
it('should work with .then', function (done) {
8+
var resolve;
9+
10+
testZone.run(function() {
11+
new Promise(function (resolveFn) {
12+
resolve = resolveFn;
13+
}).then(function () {
14+
expect(window.zone).toBeDirectChildOf(testZone);
15+
done();
16+
});
1517
});
18+
19+
resolve();
1620
});
1721

18-
resolve();
22+
it('should work with .catch', function (done) {
23+
var reject;
24+
25+
testZone.run(function() {
26+
new Promise(function (resolveFn, rejectFn) {
27+
reject = rejectFn;
28+
}).catch(function () {
29+
expect(window.zone).toBeDirectChildOf(testZone);
30+
done();
31+
});
32+
});
33+
34+
reject();
35+
});
1936
});
2037

21-
it('should work with .catch', function (done) {
22-
var reject;
38+
describe('fetch', ifEnvSupports('fetch', function () {
39+
it('should work for text response', function(done) {
40+
testZone.run(function() {
41+
fetch('/base/test/assets/sample.json').then(function(response) {
42+
var fetchZone = zone;
43+
expect(fetchZone).toBeDirectChildOf(testZone);
2344

24-
testZone.run(function() {
25-
new Promise(function (resolveFn, rejectFn) {
26-
reject = rejectFn;
27-
}).catch(function () {
28-
expect(window.zone).toBeDirectChildOf(testZone);
29-
done();
45+
response.text().then(function(text) {
46+
expect(zone).toBeDirectChildOf(fetchZone);
47+
expect(text.trim()).toEqual('{"hello": "world"}');
48+
done();
49+
});
50+
});
3051
});
3152
});
3253

33-
reject();
34-
});
54+
it('should work for json response', function(done) {
55+
testZone.run(function() {
56+
fetch('/base/test/assets/sample.json').then(function(response) {
57+
var fetchZone = zone;
58+
expect(fetchZone).toBeDirectChildOf(testZone);
59+
60+
response.json().then(function(obj) {
61+
expect(zone).toBeDirectChildOf(fetchZone);
62+
expect(obj.hello).toEqual('world');
63+
done();
64+
});
65+
});
66+
});
67+
});
68+
69+
it('should work for blob response', function(done) {
70+
testZone.run(function() {
71+
fetch('/base/test/assets/sample.json').then(function(response) {
72+
var fetchZone = zone;
73+
expect(fetchZone).toBeDirectChildOf(testZone);
74+
75+
response.blob().then(function(blob) {
76+
expect(zone).toBeDirectChildOf(fetchZone);
77+
expect(blob instanceof Blob).toEqual(true);
78+
done();
79+
});
80+
});
81+
});
82+
});
83+
84+
it('should work for arrayBuffer response', function(done) {
85+
testZone.run(function() {
86+
fetch('/base/test/assets/sample.json').then(function(response) {
87+
var fetchZone = zone;
88+
expect(fetchZone).toBeDirectChildOf(testZone);
89+
90+
response.arrayBuffer().then(function(blob) {
91+
expect(zone).toBeDirectChildOf(fetchZone);
92+
expect(blob instanceof ArrayBuffer).toEqual(true);
93+
done();
94+
});
95+
});
96+
});
97+
});
98+
}));
3599

36100
}));

0 commit comments

Comments
 (0)