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

Commit 857929d

Browse files
JiaLiPassionmhevery
authored andcommitted
feat(patch): fix #758, patch cordova.exec success/error with zone.wrap (#789)
* feat(patch): fix #758, patch cordova success/error with zone.wrap * add documents for cordova
1 parent 7ca3995 commit 857929d

11 files changed

+117
-5
lines changed

Diff for: NON-STANDARD-APIS.md

+18
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ remove the comment of the following line
7474
//import './extra/bluebird.spec';
7575
```
7676

77+
## Others
78+
79+
* Cordova
80+
81+
patch `cordova.exec` API
82+
83+
`cordova.exec(success, error, service, action, args);`
84+
85+
`success` and `error` will be patched with `Zone.wrap`.
86+
87+
to load the patch, you should load in the following order.
88+
89+
```
90+
<script src="zone.js"></script>
91+
<script src="cordova.js"></script>
92+
<script src="zone-patch-cordova.js"></script>
93+
```
94+
7795
## Usage
7896

7997
By default, those APIs' support will not be loaded in zone.js or zone-node.js,

Diff for: gulpfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ gulp.task('build/webapis-shadydom.min.js', ['compile-esm'], function(cb) {
123123
return generateScript('./lib/browser/shadydom.ts', 'webapis-shadydom.min.js', true, cb);
124124
});
125125

126+
gulp.task('build/zone-patch-cordova.js', ['compile-esm'], function(cb) {
127+
return generateScript('./lib/extra/cordova.ts', 'zone-patch-cordova.js', false, cb);
128+
});
129+
130+
gulp.task('build/zone-patch-cordova.min.js', ['compile-esm'], function(cb) {
131+
return generateScript('./lib/extra/cordova.ts', 'zone-patch-cordova.min.js', true, cb);
132+
});
133+
126134
gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
127135
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
128136
});
@@ -204,6 +212,8 @@ gulp.task('build', [
204212
'build/webapis-notification.min.js',
205213
'build/webapis-shadydom.js',
206214
'build/webapis-shadydom.min.js',
215+
'build/zone-patch-cordova.js',
216+
'build/zone-patch-cordova.min.js',
207217
'build/zone-mix.js',
208218
'build/bluebird.js',
209219
'build/bluebird.min.js',

Diff for: karma-build.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module.exports = function (config) {
1010
require('./karma-base.conf.js')(config);
1111
config.files.push('build/test/wtf_mock.js');
12-
config.files.push('build/test/custom_error.js');
12+
config.files.push('build/test/test_fake_polyfill.js');
1313
config.files.push('build/lib/zone.js');
1414
config.files.push('build/lib/common/promise.js');
1515
config.files.push('build/lib/common/error-rewrite.js');

Diff for: lib/browser/browser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@ Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType, api: _Z
203203
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
204204
api.patchEventTargetMethods = patchEventTargetMethods;
205205
api.patchOnProperties = patchOnProperties;
206+
api.patchMethod = patchMethod;
206207
});

Diff for: lib/extra/cordova.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
Zone.__load_patch('cordova', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
9+
if (global.cordova) {
10+
const nativeExec: Function = api.patchMethod(
11+
global.cordova, 'exec', (delegate: Function) => function(self: any, args: any[]) {
12+
if (args.length > 0 && typeof args[0] === 'function') {
13+
args[0] = Zone.current.wrap(args[0], 'cordova.exec.success');
14+
}
15+
if (args.length > 1 && typeof args[1] === 'function') {
16+
args[1] = Zone.current.wrap(args[1], 'cordova.exec.error');
17+
}
18+
return nativeExec.apply(self, args);
19+
});
20+
}
21+
});

Diff for: lib/zone.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ interface _ZonePrivate {
323323
patchEventTargetMethods:
324324
(obj: any, addFnName?: string, removeFnName?: string, metaCreator?: any) => boolean;
325325
patchOnProperties: (obj: any, properties: string[]) => void;
326+
patchMethod:
327+
(target: any, name: string,
328+
patchFn: (delegate: Function, delegateName: string, name: string) =>
329+
(self: any, args: any[]) => any) => Function;
326330
}
327331

328332
/** @internal */
@@ -1294,7 +1298,8 @@ const Zone: ZoneType = (function(global: any) {
12941298
scheduleMicroTask: scheduleMicroTask,
12951299
showUncaughtError: () => !(Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')],
12961300
patchEventTargetMethods: () => false,
1297-
patchOnProperties: noop
1301+
patchOnProperties: noop,
1302+
patchMethod: () => noop
12981303
};
12991304
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
13001305
let _currentTask: Task = null;

Diff for: test/browser-zone-setup.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ import '../lib/zone-spec/long-stack-trace';
1414
import '../lib/zone-spec/proxy';
1515
import '../lib/zone-spec/sync-test';
1616
import '../lib/zone-spec/task-tracking';
17-
import '../lib/zone-spec/wtf';
17+
import '../lib/zone-spec/wtf';
18+
import '../lib/extra/cordova';

Diff for: test/browser_entry_point.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ import './browser/XMLHttpRequest.spec';
2222
import './browser/MediaQuery.spec';
2323
import './browser/Notification.spec';
2424
import './mocha-patch.spec';
25-
import './jasmine-patch.spec';
25+
import './jasmine-patch.spec';
26+
import './extra/cordova.spec';

Diff for: test/extra/cordova.spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
describe('cordova test', () => {
9+
it('cordova.exec() should be patched as macroTask', (done) => {
10+
const cordova = (window as any).cordova;
11+
if (!cordova) {
12+
done();
13+
return;
14+
}
15+
16+
const zone = Zone.current.fork({name: 'cordova'});
17+
18+
zone.run(() => {
19+
cordova.exec(
20+
() => {
21+
expect(Zone.current.name).toEqual('cordova');
22+
done();
23+
},
24+
() => {
25+
fail('should not fail');
26+
},
27+
'service', 'successAction', ['arg0', 'arg1']);
28+
29+
cordova.exec(
30+
() => {
31+
fail('should not success');
32+
},
33+
() => {
34+
expect(Zone.current.name).toEqual('cordova');
35+
done();
36+
},
37+
'service', 'failAction', ['arg0', 'arg1']);
38+
});
39+
});
40+
});

Diff for: test/node_entry_point.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// Must be loaded before zone loads, so that zone can detect WTF.
1010
import './wtf_mock';
11-
import './custom_error';
11+
import './test_fake_polyfill';
1212

1313
// Setup tests for Zone without microtask support
1414
import '../lib/zone';

Diff for: test/custom_error.ts renamed to test/test_fake_polyfill.ts

+15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
88

99
'use strict';
1010
(function(global: any) {
11+
// add custom properties to Native Error
1112
const NativeError = global['Error'];
1213
NativeError.customProperty = 'customProperty';
1314
NativeError.customFunction = function() {};
15+
16+
// add fake cordova polyfill for test
17+
const fakeCordova = function() {};
18+
19+
(fakeCordova as any).exec = function(
20+
success: Function, error: Function, service: string, action: string, args: any[]) {
21+
if (action === 'successAction') {
22+
success();
23+
} else {
24+
error();
25+
}
26+
};
27+
28+
global.cordova = fakeCordova;
1429
})(typeof window === 'object' && window || typeof self === 'object' && self || global);

0 commit comments

Comments
 (0)