(name, metadata)
| 815 | * The generated wrapper function. |
| 816 | */ |
| 817 | const wrapAsyncFunction = (name, metadata) => { |
| 818 | return function asyncFunctionWrapper(target, ...args) { |
| 819 | if (args.length < metadata.minArgs) { |
| 820 | throw new Error(`Expected at least ${metadata.minArgs} ${pluralizeArguments(metadata.minArgs)} for ${name}(), got ${args.length}`); |
| 821 | } |
| 822 | if (args.length > metadata.maxArgs) { |
| 823 | throw new Error(`Expected at most ${metadata.maxArgs} ${pluralizeArguments(metadata.maxArgs)} for ${name}(), got ${args.length}`); |
| 824 | } |
| 825 | return new Promise((resolve, reject) => { |
| 826 | if (metadata.fallbackToNoCallback) { |
| 827 | // This API method has currently no callback on Chrome, but it return a promise on Firefox, |
| 828 | // and so the polyfill will try to call it with a callback first, and it will fallback |
| 829 | // to not passing the callback if the first call fails. |
| 830 | try { |
| 831 | target[name](...args, makeCallback({ |
| 832 | resolve, |
| 833 | reject |
| 834 | }, metadata)); |
| 835 | } catch (cbError) { |
| 836 | console.warn(`${name} API method doesn't seem to support the callback parameter, ` + "falling back to call it without a callback: ", cbError); |
| 837 | target[name](...args); |
| 838 | |
| 839 | // Update the API method metadata, so that the next API calls will not try to |
| 840 | // use the unsupported callback anymore. |
| 841 | metadata.fallbackToNoCallback = false; |
| 842 | metadata.noCallback = true; |
| 843 | resolve(); |
| 844 | } |
| 845 | } else if (metadata.noCallback) { |
| 846 | target[name](...args); |
| 847 | resolve(); |
| 848 | } else { |
| 849 | target[name](...args, makeCallback({ |
| 850 | resolve, |
| 851 | reject |
| 852 | }, metadata)); |
| 853 | } |
| 854 | }); |
| 855 | }; |
| 856 | }; |
| 857 | |
| 858 | /** |
| 859 | * Wraps an existing method of the target object, so that calls to it are |
no test coverage detected