Skip to content

Commit

Permalink
feat(core): implement CapacitorCustomPlatform for 3rd party platforms (
Browse files Browse the repository at this point in the history
…#4771)


Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
IT-MikeS and jcesarmobile authored Aug 12, 2021
1 parent 9ad2376 commit 12c6294
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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

0 comments on commit 12c6294

Please sign in to comment.