From cf0751fc9321d37f97192d1a1e50ac5e7d074576 Mon Sep 17 00:00:00 2001 From: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:05:49 +0200 Subject: [PATCH] size optimisations in core --- core/src/runtime.ts | 29 +++++++---------------------- core/src/util.ts | 15 ++++++--------- core/src/web-plugin.ts | 10 ++++------ 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/core/src/runtime.ts b/core/src/runtime.ts index b196269ad..410cab13f 100644 --- a/core/src/runtime.ts +++ b/core/src/runtime.ts @@ -24,11 +24,8 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { */ const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms; - const defaultGetPlatform = () => { - return capCustomPlatform !== null - ? capCustomPlatform.name - : getPlatformId(win); - }; + const defaultGetPlatform = () => + capCustomPlatform !== null ? capCustomPlatform.name : getPlatformId(win); const getPlatform = capPlatforms?.currentPlatform?.getPlatform || defaultGetPlatform; @@ -36,21 +33,10 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { const isNativePlatform = capPlatforms?.currentPlatform?.isNativePlatform || defaultIsNativePlatform; - const defaultIsPluginAvailable = (pluginName: string): boolean => { - const plugin = registeredPlugins.get(pluginName); - - if (plugin?.platforms.has(getPlatform())) { - // JS implementation available for the current platform. - return true; - } + const defaultIsPluginAvailable = (pluginName: string): boolean => + registeredPlugins.get(pluginName)?.platforms.has(getPlatform()) || // JS implementation available for the current platform. + !!getPluginHeader(pluginName); // Native implementation available. - if (getPluginHeader(pluginName)) { - // Native implementation available. - return true; - } - - return false; - }; const isPluginAvailable = capPlatforms?.currentPlatform?.isPluginAvailable || defaultIsPluginAvailable; @@ -68,11 +54,10 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { _target: any, prop: PropertyKey, pluginName: string, - ) => { - return Promise.reject( + ) => + Promise.reject( `${pluginName} does not have an implementation of "${prop as any}".`, ); - }; const registeredPlugins = new Map(); diff --git a/core/src/util.ts b/core/src/util.ts index a4063b0f0..5e597da8f 100644 --- a/core/src/util.ts +++ b/core/src/util.ts @@ -35,12 +35,9 @@ export class CapacitorException extends Error { export const getPlatformId = ( win: WindowCapacitor, -): 'android' | 'ios' | 'web' => { - if (win?.androidBridge) { - return 'android'; - } else if (win?.webkit?.messageHandlers?.bridge) { - return 'ios'; - } else { - return 'web'; - } -}; +): 'android' | 'ios' | 'web' => + win?.androidBridge + ? 'android' + : win?.webkit?.messageHandlers?.bridge + ? 'ios' + : 'web'; diff --git a/core/src/web-plugin.ts b/core/src/web-plugin.ts index 46ebe4598..373bfafa2 100644 --- a/core/src/web-plugin.ts +++ b/core/src/web-plugin.ts @@ -108,13 +108,11 @@ export class WebPlugin implements Plugin { }; } - protected unimplemented(msg = 'not implemented'): CapacitorException { - return new Capacitor.Exception(msg, ExceptionCode.Unimplemented); - } + protected unimplemented = (msg = 'not implemented'): CapacitorException => + new Capacitor.Exception(msg, ExceptionCode.Unimplemented); - protected unavailable(msg = 'not available'): CapacitorException { - return new Capacitor.Exception(msg, ExceptionCode.Unavailable); - } + protected unavailable = (msg = 'not available'): CapacitorException => + new Capacitor.Exception(msg, ExceptionCode.Unavailable); private async removeListener( eventName: string,