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