Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

size optimisations in core #7629

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,19 @@ 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;

const defaultIsNativePlatform = () => getPlatform() !== 'web';
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;
Expand All @@ -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<string, RegisteredPlugin>();

Expand Down
15 changes: 6 additions & 9 deletions core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 4 additions & 6 deletions core/src/web-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down