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

Commit

Permalink
Merge branch 'master' into socketio
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery authored Feb 8, 2018
2 parents 2469bc9 + 2dc7e5c commit 3acc43a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 10 additions & 3 deletions NON-STANDARD-APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,20 @@ import 'zone.js/dist/zone-patch-rxjs';

* electron

In electron, we patched the following APIs with `zone.js`

1. Browser API
2. NodeJS
3. Electorn Native API

## Usage.

add following line into `polyfill.ts` after load zone-mix.
add/update following line into `polyfill.ts`.

```
import 'zone.js/dist/zone-patch-electron';
//import 'zone.js/dist/zone'; // originally added by angular-cli, comment it out
import 'zone.js/dist/zone-mix'; // add zone-mix to patch both Browser and Nodejs
import 'zone.js/dist/zone-patch-electron'; // add zone-patch-electron to patch Electron native API
```

there is a sample repo [zone-electron](https://github.com/JiaLiPassion/zone-electron) here
Expand All @@ -189,4 +197,3 @@ user need to patch `io` themselves just like following code.

please reference the sample repo [zone-socketio](https://github.com/JiaLiPassion/zone-socketio) about
detail usage.

9 changes: 5 additions & 4 deletions lib/common/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {zoneSymbol} from './utils';

// override Function.prototype.toString to make zone.js patched function
// look like native function
Zone.__load_patch('toString', (global: any, Zone: ZoneType) => {
Zone.__load_patch('toString', (global: any) => {
// patch Func.prototype.toString to let them look like native
const originalFunctionToString = (Zone as any)['__zone_symbol__originalToString'] =
Function.prototype.toString;
const originalFunctionToString = Function.prototype.toString;

const ORIGINAL_DELEGATE_SYMBOL = zoneSymbol('OriginalDelegate');
const PROMISE_SYMBOL = zoneSymbol('Promise');
const ERROR_SYMBOL = zoneSymbol('Error');
Function.prototype.toString = function() {
const newFunctionToString = function toString() {
if (typeof this === 'function') {
const originalDelegate = this[ORIGINAL_DELEGATE_SYMBOL];
if (originalDelegate) {
Expand All @@ -42,6 +41,8 @@ Zone.__load_patch('toString', (global: any, Zone: ZoneType) => {
}
return originalFunctionToString.apply(this, arguments);
};
(newFunctionToString as any)[ORIGINAL_DELEGATE_SYMBOL] = originalFunctionToString;
Function.prototype.toString = newFunctionToString;


// patch Object.prototype.toString to let them look like native
Expand Down
4 changes: 4 additions & 0 deletions test/common/toString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('global function patch', () => {
expect(Function.prototype.toString.call(Error)).toContain('[native code]');
});

it('Function toString should look like native', () => {
expect(Function.prototype.toString.call(Function.prototype.toString)).toContain('[native code]');
});

it('EventTarget addEventListener should look like native', ifEnvSupports('HTMLElement', () => {
expect(Function.prototype.toString.call(HTMLElement.prototype.addEventListener))
.toContain('[native code]');
Expand Down

0 comments on commit 3acc43a

Please sign in to comment.