|
| 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('jsonp', (global: any, Zone: ZoneType, api: _ZonePrivate) => { |
| 9 | + const noop = function() {}; |
| 10 | + // because jsonp is not a standard api, there are a lot of |
| 11 | + // implementations, so zone.js just provide a helper util to |
| 12 | + // patch the jsonp send and onSuccess/onError callback |
| 13 | + // the options is an object which contains |
| 14 | + // - jsonp, the jsonp object which hold the send function |
| 15 | + // - sendFuncName, the name of the send function |
| 16 | + // - successFuncName, success func name |
| 17 | + // - failedFuncName, failed func name |
| 18 | + (Zone as any)[Zone.__symbol__('jsonp')] = function patchJsonp(options: any) { |
| 19 | + if (!options || !options.jsonp || !options.sendFuncName) { |
| 20 | + return; |
| 21 | + } |
| 22 | + const noop = function() {}; |
| 23 | + |
| 24 | + [options.successFuncName, options.failedFuncName].forEach(methodName => { |
| 25 | + if (!methodName) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const oriFunc = global[methodName]; |
| 30 | + if (oriFunc) { |
| 31 | + api.patchMethod(global, methodName, (delegate: Function) => (self: any, args: any[]) => { |
| 32 | + const task = global[api.symbol('jsonTask')]; |
| 33 | + if (task) { |
| 34 | + task.callback = delegate; |
| 35 | + return task.invoke.apply(self, args); |
| 36 | + } else { |
| 37 | + return delegate.apply(self, args); |
| 38 | + } |
| 39 | + }); |
| 40 | + } else { |
| 41 | + Object.defineProperty(global, methodName, { |
| 42 | + configurable: true, |
| 43 | + enumerable: true, |
| 44 | + get: function() { |
| 45 | + return function() { |
| 46 | + const task = global[api.symbol('jsonpTask')]; |
| 47 | + const target = this ? this : global; |
| 48 | + const delegate = global[api.symbol(`jsonp${methodName}callback`)]; |
| 49 | + |
| 50 | + if (task) { |
| 51 | + if (delegate) { |
| 52 | + task.callback = delegate; |
| 53 | + } |
| 54 | + global[api.symbol('jsonpTask')] = undefined; |
| 55 | + return task.invoke.apply(this, arguments); |
| 56 | + } else { |
| 57 | + if (delegate) { |
| 58 | + return delegate.apply(this, arguments); |
| 59 | + } |
| 60 | + } |
| 61 | + return null; |
| 62 | + }; |
| 63 | + }, |
| 64 | + set: function(callback: Function) { |
| 65 | + this[api.symbol(`jsonp${methodName}callback`)] = callback; |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + api.patchMethod(options.jsonp, options.sendFuncName, (delegate: Function) => (self: any, args: any[]) => { |
| 72 | + global[api.symbol('jsonpTask')] = Zone.current.scheduleMacroTask('jsonp', noop, {}, (task: Task) => { |
| 73 | + return delegate.apply(self, args); |
| 74 | + }, noop); |
| 75 | + }); |
| 76 | + }; |
| 77 | +}); |
0 commit comments