Skip to content

Commit

Permalink
feat(patch): fix angular#758, patch cordova success/error with zone.wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed May 21, 2017
1 parent b92b6e3 commit 7820c7f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 5 deletions.
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;
});
2 changes: 1 addition & 1 deletion lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,4 @@ export function findEventTask(target: any, evtName: string): Task[] {

export function attachOriginToPatched(patched: Function, original: any) {
(patched as any)[zoneSymbol('OriginalDelegate')] = original;
}
}
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';
37 changes: 37 additions & 0 deletions test/extra/cordova.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @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 scheduleSpy = jasmine.createSpy('scheduleTask');
const cordova = (window as any).cordova;

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

0 comments on commit 7820c7f

Please sign in to comment.