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

feat(electron): fix #537, #669, add electron support #983

Merged
merged 2 commits into from Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions NON-STANDARD-APIS.md
Expand Up @@ -160,3 +160,15 @@ For example, in an Angular application, you can load this patch in your `app.mod
import 'zone.js/dist/zone-patch-rxjs';
```

* electron

## Usage.

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

```
import 'zone.js/dist/zone-patch-electron';
```

there is a sampel repo [zone-electron](https://github.com/JiaLiPassion/zone-electron) here

10 changes: 10 additions & 0 deletions gulpfile.js
Expand Up @@ -176,6 +176,14 @@ gulp.task('build/zone-patch-cordova.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/cordova.ts', 'zone-patch-cordova.min.js', true, cb);
});

gulp.task('build/zone-patch-electron.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/electron.ts', 'zone-patch-electron.js', false, cb);
});

gulp.task('build/zone-patch-electron.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/electron.ts', 'zone-patch-electron.min.js', true, cb);
});

gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
});
Expand Down Expand Up @@ -277,6 +285,8 @@ gulp.task('build', [
'build/webapis-shadydom.min.js',
'build/zone-patch-cordova.js',
'build/zone-patch-cordova.min.js',
'build/zone-patch-electron.js',
'build/zone-patch-electron.min.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/browser.ts
Expand Up @@ -12,7 +12,7 @@

import {findEventTasks} from '../common/events';
import {patchTimer} from '../common/timers';
import {patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, wrapFunctionArgs, zoneSymbol} from '../common/utils';
import {patchArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, wrapFunctionArgs, zoneSymbol} from '../common/utils';

import {propertyPatch} from './define-property';
import {eventTargetPatch, patchEvent} from './event-target';
Expand All @@ -22,6 +22,7 @@ import {registerElementPatch} from './register-element';
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchOnProperties = patchOnProperties;
api.patchMethod = patchMethod;
api.patchArguments = patchArguments;
});

Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
Expand Down
8 changes: 8 additions & 0 deletions lib/common/utils.ts
Expand Up @@ -40,6 +40,14 @@ export function wrapFunctionArgs(func: Function, source?: string): Function {
};
}

export function patchArguments(target: any, name: string, source: string): Function {
return patchMethod(
target, name,
(delegate: Function, delegateName: string, name: string) => (self: any, args: any[]) => {
return delegate && delegate.apply(self, bindArguments(args, source));
});
}

export function patchPrototype(prototype: any, fnNames: string[]) {
const source = prototype.constructor['name'];
for (let i = 0; i < fnNames.length; i++) {
Expand Down
27 changes: 27 additions & 0 deletions lib/extra/electron.ts
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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
*/
Zone.__load_patch('electron', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const FUNCTION = 'function';
const {desktopCapturer, shell, CallbackRegistry} = require('electron');
// patch api in renderer process directly
// desktopCapturer
if (desktopCapturer) {
api.patchArguments(desktopCapturer, 'getSources', 'electron.desktopCapturer.getSources');
}
// shell
if (shell) {
api.patchArguments(shell, 'openExternal', 'electron.shell.openExternal');
}

// patch api in main process through CallbackRegistry
if (!CallbackRegistry) {
return;
}

api.patchArguments(CallbackRegistry.prototype, 'add', 'CallbackRegistry.add');
});
8 changes: 7 additions & 1 deletion lib/node/node.ts
Expand Up @@ -11,11 +11,17 @@ import './fs';

import {findEventTasks} from '../common/events';
import {patchTimer} from '../common/timers';
import {isMix, patchMacroTask, patchMicroTask} from '../common/utils';
import {isMix, patchArguments, patchMacroTask, patchMethod, patchMicroTask, patchOnProperties} from '../common/utils';

const set = 'set';
const clear = 'clear';

Zone.__load_patch('node_util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchOnProperties = patchOnProperties;
api.patchMethod = patchMethod;
api.patchArguments = patchArguments;
});

Zone.__load_patch('node_timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// Timers
let globalUseTimeoutFromTimer = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/zone.ts
Expand Up @@ -327,6 +327,7 @@ interface _ZonePrivate {
(target: any, name: string,
patchFn: (delegate: Function, delegateName: string, name: string) =>
(self: any, args: any[]) => any) => Function;
patchArguments: (target: any, name: string, source: string) => Function;
}

/** @internal */
Expand Down Expand Up @@ -1317,6 +1318,7 @@ const Zone: ZoneType = (function(global: any) {
patchEventTarget: () => [],
patchOnProperties: noop,
patchMethod: () => noop,
patchArguments: () => noop,
setNativePromise: (NativePromise: any) => {
// sometimes NativePromise.resolve static function
// is not ready yet, (such as core-js/es6.promise)
Expand Down