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 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
9 changes: 9 additions & 0 deletions core/src/definitions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,18 @@ export interface StoredCallback {
reject?: (...args: any[]) => any;
}

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

export interface WindowCapacitor {
Capacitor?: CapacitorInstance;
/**
* @deprecated Use `CapacitorCustomPlatform` instead
*/
CapacitorPlatforms?: CapacitorPlatformsInstance;
CapacitorCustomPlatform?: CapacitorCustomPlatformInstance;
Ionic?: {
WebView?: {
getServerBasePath?: any;
Expand Down
10 changes: 9 additions & 1 deletion core/src/platforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const createCapacitorPlatforms = (win: any): CapacitorPlatformsInstance => {
const initPlatforms = (win: any) =>
(win.CapacitorPlatforms = createCapacitorPlatforms(win));

/**
* @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead
*/
export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms(
(typeof globalThis !== 'undefined'
? globalThis
Expand All @@ -59,6 +62,11 @@ export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms(
? global
: {}) as any,
);

/**
* @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead
*/
export const addPlatform = CapacitorPlatforms.addPlatform;
/**
* @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead
*/
export const setPlatform = CapacitorPlatforms.setPlatform;
23 changes: 21 additions & 2 deletions core/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CapacitorGlobal, PluginImplementations } from './definitions';
import type {
CapacitorCustomPlatformInstance,
CapacitorInstance,
PluginHeader,
WindowCapacitor,
Expand All @@ -14,15 +15,24 @@ 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));
/**
* @deprecated Use `capCustomPlatform` instead, default functions like registerPlugin will function with the new object.
*/
const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms;

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

const defaultIsNativePlatform = () => getPlatformId(win) !== 'web';
const defaultIsNativePlatform = () => getPlatform() !== 'web';
const isNativePlatform =
capPlatforms?.currentPlatform?.isNativePlatform || defaultIsNativePlatform;

Expand Down Expand Up @@ -89,6 +99,15 @@ 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']);
}

return jsImplementation;
Expand Down