Skip to content

Commit

Permalink
fix(bridge): Fix type errors with new Platforms API (#4524)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvidas authored May 3, 2021
1 parent 7d62713 commit 7bbaea8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core/src/definitions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +167,7 @@ export interface StoredCallback {

export interface WindowCapacitor {
Capacitor?: CapacitorInstance;
CapacitorPlatforms: CapacitorPlatformsInstance;
CapacitorPlatforms?: CapacitorPlatformsInstance;
Ionic?: {
WebView?: {
getServerBasePath?: any;
Expand Down
29 changes: 16 additions & 13 deletions core/src/platforms.ts
Original file line number Diff line number Diff line change
@@ -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<string, CapacitorPlatform>,
platforms: Map<string, CapacitorPlatform>;
addPlatform(name: string, platform: CapacitorPlatform): void;
setPlatform(name: string): void;
}


const createCapacitorPlatforms = (win: any): CapacitorPlatformsInstance => {
const defaultPlatformMap = new Map<string, CapacitorPlatform>();
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'
Expand Down
23 changes: 16 additions & 7 deletions core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down

1 comment on commit 7bbaea8

@IT-MikeS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, forgot to run the linter 😅

Please sign in to comment.