(win: WindowCapacitor)
| 14 | } |
| 15 | |
| 16 | export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { |
| 17 | const capCustomPlatform: CapacitorCustomPlatformInstance = win.CapacitorCustomPlatform || null; |
| 18 | const cap: CapacitorInstance = win.Capacitor || ({} as any); |
| 19 | const Plugins = (cap.Plugins = cap.Plugins || ({} as any)); |
| 20 | |
| 21 | const getPlatform = () => { |
| 22 | return capCustomPlatform !== null ? capCustomPlatform.name : getPlatformId(win); |
| 23 | }; |
| 24 | |
| 25 | const isNativePlatform = () => getPlatform() !== 'web'; |
| 26 | |
| 27 | const isPluginAvailable = (pluginName: string): boolean => { |
| 28 | const plugin = registeredPlugins.get(pluginName); |
| 29 | |
| 30 | if (plugin?.platforms.has(getPlatform())) { |
| 31 | // JS implementation available for the current platform. |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | if (getPluginHeader(pluginName)) { |
| 36 | // Native implementation available. |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | }; |
| 42 | |
| 43 | const getPluginHeader = (pluginName: string): PluginHeader | undefined => |
| 44 | cap.PluginHeaders?.find((h) => h.name === pluginName); |
| 45 | |
| 46 | const handleError = (err: Error) => win.console.error(err); |
| 47 | |
| 48 | const registeredPlugins = new Map<string, RegisteredPlugin>(); |
| 49 | |
| 50 | const registerPlugin = (pluginName: string, jsImplementations: PluginImplementations = {}): any => { |
| 51 | const registeredPlugin = registeredPlugins.get(pluginName); |
| 52 | if (registeredPlugin) { |
| 53 | console.warn(`Capacitor plugin "${pluginName}" already registered. Cannot register plugins twice.`); |
| 54 | |
| 55 | return registeredPlugin.proxy; |
| 56 | } |
| 57 | |
| 58 | const platform = getPlatform(); |
| 59 | const pluginHeader = getPluginHeader(pluginName); |
| 60 | let jsImplementation: any; |
| 61 | |
| 62 | const loadPluginImplementation = async (): Promise<any> => { |
| 63 | if (!jsImplementation && platform in jsImplementations) { |
| 64 | jsImplementation = |
| 65 | typeof jsImplementations[platform] === 'function' |
| 66 | ? (jsImplementation = await jsImplementations[platform]()) |
| 67 | : (jsImplementation = jsImplementations[platform]); |
| 68 | } else if (capCustomPlatform !== null && !jsImplementation && 'web' in jsImplementations) { |
| 69 | jsImplementation = |
| 70 | typeof jsImplementations['web'] === 'function' |
| 71 | ? (jsImplementation = await jsImplementations['web']()) |
| 72 | : (jsImplementation = jsImplementations['web']); |
| 73 | } |
no outgoing calls
no test coverage detected