
committed by
Kara Erickson

parent
7b3bcc23af
commit
5eb7426216
55
packages/zone.js/lib/extra/bluebird.ts
Normal file
55
packages/zone.js/lib/extra/bluebird.ts
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @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('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
|
||||
// TODO: @JiaLiPassion, we can automatically patch bluebird
|
||||
// if global.Promise = Bluebird, but sometimes in nodejs,
|
||||
// global.Promise is not Bluebird, and Bluebird is just be
|
||||
// used by other libraries such as sequelize, so I think it is
|
||||
// safe to just expose a method to patch Bluebird explicitly
|
||||
const BLUEBIRD = 'bluebird';
|
||||
(Zone as any)[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird: any) {
|
||||
// patch method of Bluebird.prototype which not using `then` internally
|
||||
const bluebirdApis: string[] = ['then', 'spread', 'finally'];
|
||||
bluebirdApis.forEach(bapi => {
|
||||
api.patchMethod(
|
||||
Bluebird.prototype, bapi, (delegate: Function) => (self: any, args: any[]) => {
|
||||
const zone = Zone.current;
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const func = args[i];
|
||||
if (typeof func === 'function') {
|
||||
args[i] = function() {
|
||||
const argSelf: any = this;
|
||||
const argArgs: any = arguments;
|
||||
return new Bluebird((res: any, rej: any) => {
|
||||
zone.scheduleMicroTask('Promise.then', () => {
|
||||
try {
|
||||
res(func.apply(argSelf, argArgs));
|
||||
} catch (error) {
|
||||
rej(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
return delegate.apply(self, args);
|
||||
});
|
||||
});
|
||||
|
||||
Bluebird.onPossiblyUnhandledRejection(function(e: any, promise: any) {
|
||||
try {
|
||||
Zone.current.runGuarded(() => { throw e; });
|
||||
} catch (err) {
|
||||
api.onUnhandledError(err);
|
||||
}
|
||||
});
|
||||
|
||||
// override global promise
|
||||
global[api.symbol('ZoneAwarePromise')] = Bluebird;
|
||||
};
|
||||
});
|
39
packages/zone.js/lib/extra/cordova.ts
Normal file
39
packages/zone.js/lib/extra/cordova.ts
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @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 SUCCESS_SOURCE = 'cordova.exec.success';
|
||||
const ERROR_SOURCE = 'cordova.exec.error';
|
||||
const FUNCTION = 'function';
|
||||
const nativeExec: Function|null =
|
||||
api.patchMethod(global.cordova, 'exec', () => function(self: any, args: any[]) {
|
||||
if (args.length > 0 && typeof args[0] === FUNCTION) {
|
||||
args[0] = Zone.current.wrap(args[0], SUCCESS_SOURCE);
|
||||
}
|
||||
if (args.length > 1 && typeof args[1] === FUNCTION) {
|
||||
args[1] = Zone.current.wrap(args[1], ERROR_SOURCE);
|
||||
}
|
||||
return nativeExec !.apply(self, args);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Zone.__load_patch('cordova.FileReader', (global: any, Zone: ZoneType) => {
|
||||
if (global.cordova && typeof global['FileReader'] !== 'undefined') {
|
||||
document.addEventListener('deviceReady', () => {
|
||||
const FileReader = global['FileReader'];
|
||||
['abort', 'error', 'load', 'loadstart', 'loadend', 'progress'].forEach(prop => {
|
||||
const eventNameSymbol = Zone.__symbol__('ON_PROPERTY' + prop);
|
||||
Object.defineProperty(FileReader.prototype, eventNameSymbol, {
|
||||
configurable: true,
|
||||
get: function() { return this._realReader && this._realReader[eventNameSymbol]; }
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
31
packages/zone.js/lib/extra/electron.ts
Normal file
31
packages/zone.js/lib/extra/electron.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @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) => {
|
||||
function patchArguments(target: any, name: string, source: string): Function|null {
|
||||
return api.patchMethod(target, name, (delegate: Function) => (self: any, args: any[]) => {
|
||||
return delegate && delegate.apply(self, api.bindArguments(args, source));
|
||||
});
|
||||
}
|
||||
const {desktopCapturer, shell, CallbacksRegistry} = require('electron');
|
||||
// patch api in renderer process directly
|
||||
// desktopCapturer
|
||||
if (desktopCapturer) {
|
||||
patchArguments(desktopCapturer, 'getSources', 'electron.desktopCapturer.getSources');
|
||||
}
|
||||
// shell
|
||||
if (shell) {
|
||||
patchArguments(shell, 'openExternal', 'electron.shell.openExternal');
|
||||
}
|
||||
|
||||
// patch api in main process through CallbackRegistry
|
||||
if (!CallbacksRegistry) {
|
||||
return;
|
||||
}
|
||||
|
||||
patchArguments(CallbacksRegistry.prototype, 'add', 'CallbackRegistry.add');
|
||||
});
|
77
packages/zone.js/lib/extra/jsonp.ts
Normal file
77
packages/zone.js/lib/extra/jsonp.ts
Normal file
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @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('jsonp', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
|
||||
const noop = function() {};
|
||||
// because jsonp is not a standard api, there are a lot of
|
||||
// implementations, so zone.js just provide a helper util to
|
||||
// patch the jsonp send and onSuccess/onError callback
|
||||
// the options is an object which contains
|
||||
// - jsonp, the jsonp object which hold the send function
|
||||
// - sendFuncName, the name of the send function
|
||||
// - successFuncName, success func name
|
||||
// - failedFuncName, failed func name
|
||||
(Zone as any)[Zone.__symbol__('jsonp')] = function patchJsonp(options: any) {
|
||||
if (!options || !options.jsonp || !options.sendFuncName) {
|
||||
return;
|
||||
}
|
||||
const noop = function() {};
|
||||
|
||||
[options.successFuncName, options.failedFuncName].forEach(methodName => {
|
||||
if (!methodName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const oriFunc = global[methodName];
|
||||
if (oriFunc) {
|
||||
api.patchMethod(global, methodName, (delegate: Function) => (self: any, args: any[]) => {
|
||||
const task = global[api.symbol('jsonTask')];
|
||||
if (task) {
|
||||
task.callback = delegate;
|
||||
return task.invoke.apply(self, args);
|
||||
} else {
|
||||
return delegate.apply(self, args);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Object.defineProperty(global, methodName, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return function() {
|
||||
const task = global[api.symbol('jsonpTask')];
|
||||
const target = this ? this : global;
|
||||
const delegate = global[api.symbol(`jsonp${methodName}callback`)];
|
||||
|
||||
if (task) {
|
||||
if (delegate) {
|
||||
task.callback = delegate;
|
||||
}
|
||||
global[api.symbol('jsonpTask')] = undefined;
|
||||
return task.invoke.apply(this, arguments);
|
||||
} else {
|
||||
if (delegate) {
|
||||
return delegate.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
},
|
||||
set: function(callback: Function) {
|
||||
this[api.symbol(`jsonp${methodName}callback`)] = callback;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
api.patchMethod(
|
||||
options.jsonp, options.sendFuncName, (delegate: Function) => (self: any, args: any[]) => {
|
||||
global[api.symbol('jsonpTask')] = Zone.current.scheduleMacroTask(
|
||||
'jsonp', noop, {}, (task: Task) => { return delegate.apply(self, args); }, noop);
|
||||
});
|
||||
};
|
||||
});
|
22
packages/zone.js/lib/extra/socket-io.ts
Normal file
22
packages/zone.js/lib/extra/socket-io.ts
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @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('socketio', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
|
||||
(Zone as any)[Zone.__symbol__('socketio')] = function patchSocketIO(io: any) {
|
||||
// patch io.Socket.prototype event listener related method
|
||||
api.patchEventTarget(global, [io.Socket.prototype], {
|
||||
useG: false,
|
||||
chkDup: false,
|
||||
rt: true,
|
||||
diff: (task: any, delegate: any) => { return task.callback === delegate; }
|
||||
});
|
||||
// also patch io.Socket.prototype.on/off/removeListener/removeAllListeners
|
||||
io.Socket.prototype.on = io.Socket.prototype.addEventListener;
|
||||
io.Socket.prototype.off = io.Socket.prototype.removeListener =
|
||||
io.Socket.prototype.removeAllListeners = io.Socket.prototype.removeEventListener;
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user