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

Commit

Permalink
feat(Core): fix #910, add a flag to allow user to ignore duplicate Zo…
Browse files Browse the repository at this point in the history
…ne error
  • Loading branch information
JiaLiPassion committed Jun 20, 2018
1 parent 0a2f6ff commit 858ed49
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 45 deletions.
4 changes: 2 additions & 2 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ export function copySymbolProperties(src: any, dest: any) {
symbols.forEach((symbol: any) => {
const desc = Object.getOwnPropertyDescriptor(src, symbol);
Object.defineProperty(dest, symbol, {
get: function () {
get: function() {
return src[symbol];
},
set: function (value: any) {
set: function(value: any) {
if (desc && (!desc.writable || typeof desc.set !== 'function')) {
// if src[symbol] is not writable or not have a setter, just return
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/node/node_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {setShouldCopySymbolProperties, patchOnProperties, patchMethod, bindArguments} from '../common/utils';
import {bindArguments, patchMethod, patchOnProperties, setShouldCopySymbolProperties} from '../common/utils';

Zone.__load_patch('node_util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchOnProperties = patchOnProperties;
Expand Down
16 changes: 15 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,21 @@ const Zone: ZoneType = (function(global: any) {
}
mark('Zone');
if (global['Zone']) {
throw new Error('Zone already loaded.');
// if global['Zone'] already exists (maybe zone.js was already loaded or
// some other lib also registered a global object named Zone), we may need
// to throw an error, but sometimes user may not want this error.
// For example,
// we have two web pages, page1 includes zone.js, page2 doesn't.
// and the 1st time user load page1 and page2, everything work fine,
// but when user load page2 again, error occurs because global['Zone'] already exists.
// so we add a flag to let user choose whether to throw this error or not.
// By default, if existing Zone is from zone.js, we will not throw the error.
if (global[('__zone_symbol__forceDuplicateZoneCheck')] === true ||
typeof global['Zone'].__symbol__ !== 'function') {
throw new Error('Zone already loaded.');
} else {
return global['Zone'];
}
}

class Zone implements AmbientZone {
Expand Down
2 changes: 1 addition & 1 deletion test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if ((window as any)[(Zone as any).__symbol__('setTimeout')]) {
// this means that Zone has not patched the browser yet, which means we must be running in
// build mode and need to load the browser patch.
browserPatchedPromise = System.import('/base/build/test/browser-zone-setup').then(() => {
let testFrameworkPatch = typeof(window as any).Mocha !== 'undefined' ?
let testFrameworkPatch = typeof (window as any).Mocha !== 'undefined' ?
'/base/build/lib/mocha/mocha' :
'/base/build/lib/jasmine/jasmine';
return System.import(testFrameworkPatch);
Expand Down
61 changes: 34 additions & 27 deletions test/node/fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {exists, read, unlink, unwatchFile, watch, write, watchFile, writeFile, openSync, fstatSync, closeSync, unlinkSync} from 'fs';
import {closeSync, exists, fstatSync, openSync, read, unlink, unlinkSync, unwatchFile, watch, watchFile, write, writeFile} from 'fs';
import * as util from 'util';

describe('nodejs file system', () => {
Expand Down Expand Up @@ -94,12 +94,15 @@ describe('nodejs file system', () => {
describe('util.promisify', () => {
it('fs.exists should work with util.promisify', (done: DoneFn) => {
const promisifyExists = util.promisify(exists);
promisifyExists(__filename).then(r => {
expect(r).toBe(true);
done();
}, err => {
fail(`should not be here with error: ${err}`);
});
promisifyExists(__filename)
.then(
r => {
expect(r).toBe(true);
done();
},
err => {
fail(`should not be here with error: ${err}`);
});
});

it('fs.read should work with util.promisify', (done: DoneFn) => {
Expand All @@ -111,15 +114,17 @@ describe('util.promisify', () => {
const buffer = new Buffer(bufferSize);
let bytesRead = 0;
// fd, buffer, offset, length, position, callback
promisifyRead(fd, buffer, bytesRead, chunkSize, bytesRead).then(
(value) => {
expect(value.bytesRead).toBe(chunkSize);
closeSync(fd);
done();
}, err => {
closeSync(fd);
fail(`should not be here with error: ${error}.`);
});
promisifyRead(fd, buffer, bytesRead, chunkSize, bytesRead)
.then(
(value) => {
expect(value.bytesRead).toBe(chunkSize);
closeSync(fd);
done();
},
err => {
closeSync(fd);
fail(`should not be here with error: ${error}.`);
});
});

it('fs.write should work with util.promisify', (done: DoneFn) => {
Expand All @@ -133,16 +138,18 @@ describe('util.promisify', () => {
buffer[i] = 0;
}
// fd, buffer, offset, length, position, callback
promisifyWrite(fd, buffer, 0, chunkSize, 0).then(
(value) => {
expect(value.bytesWritten).toBe(chunkSize);
closeSync(fd);
unlinkSync(dest);
done();
}, err => {
closeSync(fd);
unlinkSync(dest);
fail(`should not be here with error: ${error}.`);
});
promisifyWrite(fd, buffer, 0, chunkSize, 0)
.then(
(value) => {
expect(value.bytesWritten).toBe(chunkSize);
closeSync(fd);
unlinkSync(dest);
done();
},
err => {
closeSync(fd);
unlinkSync(dest);
fail(`should not be here with error: ${error}.`);
});
});
});
31 changes: 18 additions & 13 deletions test/node/timer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { promisify } from 'util';
import {promisify} from 'util';

describe('node timer', () => {
it('util.promisify should work with setTimeout', (done: DoneFn) => {
const setTimeoutPromise = promisify(setTimeout);
setTimeoutPromise(50, 'value').then(value => {
expect(value).toEqual('value');
done();
}, error => {
fail(`should not be here with error: ${error}.`);
});
setTimeoutPromise(50, 'value')
.then(
value => {
expect(value).toEqual('value');
done();
},
error => {
fail(`should not be here with error: ${error}.`);
});
});

it('util.promisify should work with setImmediate', (done: DoneFn) => {
const setImmediatePromise = promisify(setImmediate);
setImmediatePromise('value').then(value => {
expect(value).toEqual('value');
done();
}, error => {
fail(`should not be here with error: ${error}.`);
});
setImmediatePromise('value').then(
value => {
expect(value).toEqual('value');
done();
},
error => {
fail(`should not be here with error: ${error}.`);
});
});
});

0 comments on commit 858ed49

Please sign in to comment.