From 7bbaea85494c53a950abab40bb77f37087e22abe Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 3 May 2021 16:37:30 -0400 Subject: [PATCH] fix(bridge): Fix type errors with new Platforms API (#4524) --- core/src/definitions-internal.ts | 4 ++-- core/src/platforms.ts | 29 ++++++++++++++++------------- core/src/runtime.ts | 23 ++++++++++++++++------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/core/src/definitions-internal.ts b/core/src/definitions-internal.ts index e896f665f..6660a9793 100644 --- a/core/src/definitions-internal.ts +++ b/core/src/definitions-internal.ts @@ -4,7 +4,7 @@ import type { PluginResultData, PluginResultError, } from './definitions'; -import { CapacitorPlatformsInstance } from './platforms'; +import type { CapacitorPlatformsInstance } from './platforms'; export interface PluginHeaderMethod { readonly name: string; @@ -167,7 +167,7 @@ export interface StoredCallback { export interface WindowCapacitor { Capacitor?: CapacitorInstance; - CapacitorPlatforms: CapacitorPlatformsInstance; + CapacitorPlatforms?: CapacitorPlatformsInstance; Ionic?: { WebView?: { getServerBasePath?: any; diff --git a/core/src/platforms.ts b/core/src/platforms.ts index 408fd1077..65da81da8 100644 --- a/core/src/platforms.ts +++ b/core/src/platforms.ts @@ -1,49 +1,52 @@ -import { PluginImplementations } from "./definitions"; -import { PluginHeader } from "./definitions-internal"; +import type { PluginImplementations } from './definitions'; +import type { PluginHeader } from './definitions-internal'; export interface CapacitorPlatform { name: string; getPlatform?(): string; isPluginAvailable?(pluginName: string): boolean; getPluginHeader?(pluginName: string): PluginHeader | undefined; - registerPlugin?(pluginName: string, jsImplementations: PluginImplementations): any; + registerPlugin?( + pluginName: string, + jsImplementations: PluginImplementations, + ): any; isNativePlatform?(): boolean; } export interface CapacitorPlatformsInstance { currentPlatform: CapacitorPlatform; - platforms: Map, + platforms: Map; addPlatform(name: string, platform: CapacitorPlatform): void; setPlatform(name: string): void; } - const createCapacitorPlatforms = (win: any): CapacitorPlatformsInstance => { const defaultPlatformMap = new Map(); - defaultPlatformMap.set('web', {name: 'web'}); - + defaultPlatformMap.set('web', { name: 'web' }); + const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms || { - currentPlatform: {name: 'web'}, + currentPlatform: { name: 'web' }, platforms: defaultPlatformMap, }; const addPlatform = (name: string, platform: CapacitorPlatform) => { capPlatforms.platforms.set(name, platform); - } + }; const setPlatform = (name: string) => { if (capPlatforms.platforms.has(name)) { capPlatforms.currentPlatform = capPlatforms.platforms.get(name); } - } + }; capPlatforms.addPlatform = addPlatform; capPlatforms.setPlatform = setPlatform; - return capPlatforms -} + return capPlatforms; +}; -const initPlatforms = (win: any) => (win.CapacitorPlatforms = createCapacitorPlatforms(win)) +const initPlatforms = (win: any) => + (win.CapacitorPlatforms = createCapacitorPlatforms(win)); export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms( (typeof globalThis !== 'undefined' diff --git a/core/src/runtime.ts b/core/src/runtime.ts index 1e5ea2cbe..7ca2549e4 100644 --- a/core/src/runtime.ts +++ b/core/src/runtime.ts @@ -4,8 +4,8 @@ import type { PluginHeader, WindowCapacitor, } from './definitions-internal'; -import { CapacitorException, getPlatformId, ExceptionCode } from './util'; import type { CapacitorPlatformsInstance } from './platforms'; +import { CapacitorException, getPlatformId, ExceptionCode } from './util'; export interface RegisteredPlugin { readonly name: string; @@ -19,10 +19,12 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms; const defaultGetPlatform = () => getPlatformId(win); - const getPlatform = capPlatforms.currentPlatform.getPlatform || defaultGetPlatform; + const getPlatform = + capPlatforms?.currentPlatform?.getPlatform || defaultGetPlatform; const defaultIsNativePlatform = () => getPlatformId(win) !== 'web'; - const isNativePlatform = capPlatforms.currentPlatform.isNativePlatform || defaultIsNativePlatform; + const isNativePlatform = + capPlatforms?.currentPlatform?.isNativePlatform || defaultIsNativePlatform; const defaultIsPluginAvailable = (pluginName: string): boolean => { const plugin = registeredPlugins.get(pluginName); @@ -39,10 +41,16 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { return false; }; - const isPluginAvailable = capPlatforms.currentPlatform.isPluginAvailable || defaultIsPluginAvailable; + const isPluginAvailable = + capPlatforms?.currentPlatform?.isPluginAvailable || + defaultIsPluginAvailable; - const defaultGetPluginHeader = (pluginName: string): PluginHeader | undefined => cap.PluginHeaders?.find(h => h.name === pluginName); - const getPluginHeader = capPlatforms.currentPlatform.getPluginHeader || defaultGetPluginHeader; + const defaultGetPluginHeader = ( + pluginName: string, + ): PluginHeader | undefined => + cap.PluginHeaders?.find(h => h.name === pluginName); + const getPluginHeader = + capPlatforms?.currentPlatform?.getPluginHeader || defaultGetPluginHeader; const handleError = (err: Error) => win.console.error(err); @@ -214,7 +222,8 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { return proxy; }; - const registerPlugin = capPlatforms.currentPlatform.registerPlugin || defaultRegisterPlugin; + const registerPlugin = + capPlatforms?.currentPlatform?.registerPlugin || defaultRegisterPlugin; // Add in convertFileSrc for web, it will already be available in native context if (!cap.convertFileSrc) {