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

Commit dbadaf1

Browse files
committed
feat(patch): fix #758, patch cordova success/error with zone.wrap
1 parent b92b6e3 commit dbadaf1

8 files changed

+85
-4
lines changed

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');

lib/browser/browser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,5 @@ Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType, api: _Z
193193
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
194194
api.patchEventTargetMethods = patchEventTargetMethods;
195195
api.patchOnProperties = patchOnProperties;
196+
api.patchMethod = patchMethod;
196197
});

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+
});

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;

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';

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';

test/extra/cordova.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 scheduleSpy = jasmine.createSpy('scheduleTask');
11+
const cordova = (window as any).cordova;
12+
13+
const zone = Zone.current.fork({name: 'cordova'});
14+
15+
zone.run(() => {
16+
cordova.exec(
17+
() => {
18+
expect(Zone.current.name).toEqual('cordova');
19+
done();
20+
},
21+
() => {
22+
fail('should not fail');
23+
},
24+
'service', 'successAction', ['arg0', 'arg1']);
25+
26+
cordova.exec(
27+
() => {
28+
fail('should not success');
29+
},
30+
() => {
31+
expect(Zone.current.name).toEqual('cordova');
32+
done();
33+
},
34+
'service', 'failAction', ['arg0', 'arg1']);
35+
});
36+
});
37+
});

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)