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

feat(patch): fix #758, patch cordova.exec success/error with zone.wrap #789

Merged
merged 2 commits into from
Jun 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions NON-STANDARD-APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ remove the comment of the following line
//import './extra/bluebird.spec';
```

## Others

* Cordova

patch `cordova.exec` API

`cordova.exec(success, error, service, action, args);`

`success` and `error` will be patched with `Zone.wrap`.

to load the patch, you should load in the following order.

```
<script src="zone.js"></script>
<script src="cordova.js"></script>
<script src="zone-patch-cordova.js"></script>
```

## Usage

By default, those APIs' support will not be loaded in zone.js or zone-node.js,
Expand Down
10 changes: 10 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ gulp.task('build/webapis-shadydom.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/shadydom.ts', 'webapis-shadydom.min.js', true, cb);
});

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

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/bluebird.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
});
Expand Down Expand Up @@ -204,6 +212,8 @@ gulp.task('build', [
'build/webapis-notification.min.js',
'build/webapis-shadydom.js',
'build/webapis-shadydom.min.js',
'build/zone-patch-cordova.js',
'build/zone-patch-cordova.min.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
Expand Down
2 changes: 1 addition & 1 deletion karma-build.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
module.exports = function (config) {
require('./karma-base.conf.js')(config);
config.files.push('build/test/wtf_mock.js');
config.files.push('build/test/custom_error.js');
config.files.push('build/test/test_fake_polyfill.js');
config.files.push('build/lib/zone.js');
config.files.push('build/lib/common/promise.js');
config.files.push('build/lib/common/error-rewrite.js');
Expand Down
1 change: 1 addition & 0 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,5 @@ Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType, api: _Z
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchEventTargetMethods = patchEventTargetMethods;
api.patchOnProperties = patchOnProperties;
api.patchMethod = patchMethod;
});
21 changes: 21 additions & 0 deletions lib/extra/cordova.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @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('cordova', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
if (global.cordova) {
const nativeExec: Function = api.patchMethod(
global.cordova, 'exec', (delegate: Function) => function(self: any, args: any[]) {
if (args.length > 0 && typeof args[0] === 'function') {
args[0] = Zone.current.wrap(args[0], 'cordova.exec.success');
}
if (args.length > 1 && typeof args[1] === 'function') {
args[1] = Zone.current.wrap(args[1], 'cordova.exec.error');
}
return nativeExec.apply(self, args);
});
}
});
7 changes: 6 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ interface _ZonePrivate {
patchEventTargetMethods:
(obj: any, addFnName?: string, removeFnName?: string, metaCreator?: any) => boolean;
patchOnProperties: (obj: any, properties: string[]) => void;
patchMethod:
(target: any, name: string,
patchFn: (delegate: Function, delegateName: string, name: string) =>
(self: any, args: any[]) => any) => Function;
}

/** @internal */
Expand Down Expand Up @@ -1294,7 +1298,8 @@ const Zone: ZoneType = (function(global: any) {
scheduleMicroTask: scheduleMicroTask,
showUncaughtError: () => !(Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')],
patchEventTargetMethods: () => false,
patchOnProperties: noop
patchOnProperties: noop,
patchMethod: () => noop
};
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
let _currentTask: Task = null;
Expand Down
3 changes: 2 additions & 1 deletion test/browser-zone-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ import '../lib/zone-spec/long-stack-trace';
import '../lib/zone-spec/proxy';
import '../lib/zone-spec/sync-test';
import '../lib/zone-spec/task-tracking';
import '../lib/zone-spec/wtf';
import '../lib/zone-spec/wtf';
import '../lib/extra/cordova';
3 changes: 2 additions & 1 deletion test/browser_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ import './browser/XMLHttpRequest.spec';
import './browser/MediaQuery.spec';
import './browser/Notification.spec';
import './mocha-patch.spec';
import './jasmine-patch.spec';
import './jasmine-patch.spec';
import './extra/cordova.spec';
40 changes: 40 additions & 0 deletions test/extra/cordova.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @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
*/
describe('cordova test', () => {
it('cordova.exec() should be patched as macroTask', (done) => {
const cordova = (window as any).cordova;
if (!cordova) {
done();
return;
}

const zone = Zone.current.fork({name: 'cordova'});

zone.run(() => {
cordova.exec(
() => {
expect(Zone.current.name).toEqual('cordova');
done();
},
() => {
fail('should not fail');
},
'service', 'successAction', ['arg0', 'arg1']);

cordova.exec(
() => {
fail('should not success');
},
() => {
expect(Zone.current.name).toEqual('cordova');
done();
},
'service', 'failAction', ['arg0', 'arg1']);
});
});
});
2 changes: 1 addition & 1 deletion test/node_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Must be loaded before zone loads, so that zone can detect WTF.
import './wtf_mock';
import './custom_error';
import './test_fake_polyfill';

// Setup tests for Zone without microtask support
import '../lib/zone';
Expand Down
15 changes: 15 additions & 0 deletions test/custom_error.ts → test/test_fake_polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@

'use strict';
(function(global: any) {
// add custom properties to Native Error
const NativeError = global['Error'];
NativeError.customProperty = 'customProperty';
NativeError.customFunction = function() {};

// add fake cordova polyfill for test
const fakeCordova = function() {};

(fakeCordova as any).exec = function(
success: Function, error: Function, service: string, action: string, args: any[]) {
if (action === 'successAction') {
success();
} else {
error();
}
};

global.cordova = fakeCordova;
})(typeof window === 'object' && window || typeof self === 'object' && self || global);