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

feat(core): implement CapacitorCustomPlatform for 3rd party platforms #4771

Merged
merged 22 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 6 additions & 2 deletions core/src/definitions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
PluginResultData,
PluginResultError,
} from './definitions';
import type { CapacitorPlatformsInstance } from './platforms';

export interface PluginHeaderMethod {
readonly name: string;
Expand Down Expand Up @@ -165,9 +164,14 @@ export interface StoredCallback {
reject?: (...args: any[]) => any;
}

export interface CapacitorCustomPlatformInstance {
name: string;
plugins: { [pluginName: string]: any };
}

export interface WindowCapacitor {
Capacitor?: CapacitorInstance;
CapacitorPlatforms?: CapacitorPlatformsInstance;
CapacitorCustomPlatform?: CapacitorCustomPlatformInstance;
Ionic?: {
WebView?: {
getServerBasePath?: any;
Expand Down
3 changes: 0 additions & 3 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export type {
PluginResultError,
} from './definitions';

// Platforms Map
export { CapacitorPlatforms, addPlatform, setPlatform } from './platforms';

// Global APIs
export { Capacitor, registerPlugin } from './global';

Expand Down
64 changes: 0 additions & 64 deletions core/src/platforms.ts

This file was deleted.

42 changes: 22 additions & 20 deletions core/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CapacitorGlobal, PluginImplementations } from './definitions';
import type {
CapacitorCustomPlatformInstance,
CapacitorInstance,
PluginHeader,
WindowCapacitor,
} from './definitions-internal';
import type { CapacitorPlatformsInstance } from './platforms';
import { CapacitorException, getPlatformId, ExceptionCode } from './util';

export interface RegisteredPlugin {
Expand All @@ -14,19 +14,20 @@ export interface RegisteredPlugin {
}

export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {
const capCustomPlatform: CapacitorCustomPlatformInstance =
win.CapacitorCustomPlatform || null;
const cap: CapacitorInstance = win.Capacitor || ({} as any);
const Plugins = (cap.Plugins = cap.Plugins || ({} as any));
const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms;

const defaultGetPlatform = () => getPlatformId(win);
const getPlatform =
capPlatforms?.currentPlatform?.getPlatform || defaultGetPlatform;
const getPlatform = () => {
return capCustomPlatform !== null
? capCustomPlatform.name
: getPlatformId(win);
};

const defaultIsNativePlatform = () => getPlatformId(win) !== 'web';
const isNativePlatform =
capPlatforms?.currentPlatform?.isNativePlatform || defaultIsNativePlatform;
const isNativePlatform = () => getPlatformId(win) !== 'web';
IT-MikeS marked this conversation as resolved.
Show resolved Hide resolved

const defaultIsPluginAvailable = (pluginName: string): boolean => {
const isPluginAvailable = (pluginName: string): boolean => {
const plugin = registeredPlugins.get(pluginName);

if (plugin?.platforms.has(getPlatform())) {
Expand All @@ -41,16 +42,9 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {

return false;
};
const isPluginAvailable =
capPlatforms?.currentPlatform?.isPluginAvailable ||
defaultIsPluginAvailable;

const defaultGetPluginHeader = (
pluginName: string,
): PluginHeader | undefined =>
const getPluginHeader = (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 All @@ -66,7 +60,7 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {

const registeredPlugins = new Map<string, RegisteredPlugin>();

const defaultRegisterPlugin = (
const registerPlugin = (
pluginName: string,
jsImplementations: PluginImplementations = {},
): any => {
Expand All @@ -89,7 +83,17 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {
typeof jsImplementations[platform] === 'function'
? (jsImplementation = await jsImplementations[platform]())
: (jsImplementation = jsImplementations[platform]);
} else if (
capCustomPlatform !== null &&
!jsImplementation &&
'web' in jsImplementations
) {
jsImplementation =
typeof jsImplementations['web'] === 'function'
? (jsImplementation = await jsImplementations['web']())
: (jsImplementation = jsImplementations['web']);
}
//*/
IT-MikeS marked this conversation as resolved.
Show resolved Hide resolved

return jsImplementation;
};
Expand Down Expand Up @@ -222,8 +226,6 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {

return proxy;
};
const registerPlugin =
capPlatforms?.currentPlatform?.registerPlugin || defaultRegisterPlugin;

// Add in convertFileSrc for web, it will already be available in native context
if (!cap.convertFileSrc) {
Expand Down