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

Commit

Permalink
fix: make fetch promise patching safe
Browse files Browse the repository at this point in the history
Closes #451
  • Loading branch information
marclaval authored and mhevery committed Oct 1, 2016
1 parent c76255b commit 570cd5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (config) {
files: [
'node_modules/systemjs/dist/system-polyfills.js',
'node_modules/systemjs/dist/system.src.js',
'node_modules/whatwg-fetch/fetch.js',
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
{pattern: 'build/**/*.js.map', watched: true, served: true, included: false},
{pattern: 'build/**/*.js', watched: true, served: true, included: false},
Expand Down
2 changes: 1 addition & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ const Zone: ZoneType = (function(global: any) {
}
// ignore output to prevent error;
fetchPromise.then(() => null, () => null);
if (fetchPromise.constructor != NativePromise) {
if (fetchPromise.constructor != NativePromise && fetchPromise.constructor != ZoneAwarePromise) {
patchThen(fetchPromise.constructor);
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/systemjs": "^0.19.30",
"concurrently": "^2.2.0",
"es6-promise": "^3.0.2",
"whatwg-fetch": "^1.0.0",
"gulp": "^3.8.11",
"gulp-rename": "^1.2.2",
"gulp-rollup": "^2.3.0",
Expand Down
33 changes: 22 additions & 11 deletions test/common/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,22 @@ describe('Promise', ifEnvSupports('Promise', function () {
});
});

it('should work for blob response', function(done) {
testZone.run(function() {
global['fetch']('/base/test/assets/sample.json').then(function(response: any) {
it('should work for blob response', function (done) {
testZone.run(function () {
global['fetch']('/base/test/assets/sample.json').then(function (response:any) {
var fetchZone = Zone.current;
expect(fetchZone).toBe(testZone);

response.blob().then(function(blob) {
expect(Zone.current).toBe(fetchZone);
expect(blob instanceof Blob).toEqual(true);
// Android 4.3- doesn't support response.blob()
if (response.blob) {
response.blob().then(function (blob) {
expect(Zone.current).toBe(fetchZone);
expect(blob instanceof Blob).toEqual(true);
done();
});
} else {
done();
});
}
});
});
});
Expand All @@ -355,13 +360,19 @@ describe('Promise', ifEnvSupports('Promise', function () {
var fetchZone = Zone.current;
expect(fetchZone).toBe(testZone);

response.arrayBuffer().then(function(blob) {
expect(Zone.current).toBe(fetchZone);
expect(blob instanceof ArrayBuffer).toEqual(true);
// Android 4.3- doesn't support response.arrayBuffer()
if (response.arrayBuffer) {
response.arrayBuffer().then(function (blob) {
expect(Zone.current).toBe(fetchZone);
expect(blob instanceof ArrayBuffer).toEqual(true);
done();
});
} else {
done();
});
}
});
});
});

}));
}));

0 comments on commit 570cd5a

Please sign in to comment.