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): platforms api #4255

Merged
merged 23 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e2614cc
platforms api init
IT-MikeS Feb 27, 2021
55ca894
fix: wrong interface in defs.
IT-MikeS Feb 27, 2021
0aef29f
fix: missed a default declare
IT-MikeS Feb 27, 2021
9156c1e
Merge pull request #1 from ionic-team/main
IT-MikeS Apr 5, 2021
0feeed7
Merge pull request #2 from ionic-team/main
IT-MikeS Apr 5, 2021
aee56c7
Merge pull request #3 from ionic-team/main
IT-MikeS Apr 25, 2021
67e9f04
platforms api init
IT-MikeS Feb 27, 2021
dd7ae98
fix: wrong interface in defs.
IT-MikeS Feb 27, 2021
ffd67f0
fix: missed a default declare
IT-MikeS Feb 27, 2021
c5923a9
fix(cli): filter targets without id from run device list (#4397)
jcesarmobile Apr 5, 2021
8980938
feat(android): ability to add listeners to the Capacitor WebView (#4405)
carlpoole Apr 8, 2021
0c0b8f0
fix(android): resolve issue with activity result API registration for…
carlpoole Apr 12, 2021
9d97b1a
chore(cli): Update plist dependency (#4458)
jcesarmobile Apr 13, 2021
11d9b43
feat(cli): Add signup prompt on first init (#4440)
jcesarmobile Apr 14, 2021
513c5aa
fix(ios): put cancel button of confirm/prompt on the left (#4464)
jcesarmobile Apr 15, 2021
7a3a990
fix(cli): Allow prereleases on node version check (#4469)
jcesarmobile Apr 22, 2021
f2645da
fix(ios): fire resume/pause events if no cordova plugins installed (#…
jcesarmobile Apr 22, 2021
947e350
Merge branch 'add-platform-api' of https://github.com/IT-MikeS/capaci…
IT-MikeS May 3, 2021
ade3d9e
Merge pull request #5 from ionic-team/main
IT-MikeS May 3, 2021
7d17064
platforms api init
IT-MikeS Feb 27, 2021
a91047a
Merge branch 'add-platform-api' of https://github.com/IT-MikeS/capaci…
IT-MikeS May 3, 2021
68becd6
chore: clean
IT-MikeS May 3, 2021
8602b3c
chore: remove events file
IT-MikeS May 3, 2021
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
2 changes: 2 additions & 0 deletions core/src/definitions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
PluginResultData,
PluginResultError,
} from './definitions';
import { CapacitorPlatformsInstance } from './platforms';

export interface PluginHeaderMethod {
readonly name: string;
Expand Down Expand Up @@ -166,6 +167,7 @@ export interface StoredCallback {

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

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

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

Expand Down
61 changes: 61 additions & 0 deletions core/src/platforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { PluginImplementations } from "./definitions";
import { 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;
isNativePlatform?(): boolean;
}

export interface CapacitorPlatformsInstance {
currentPlatform: 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'});

const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms || {
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
}

const initPlatforms = (win: any) => (win.CapacitorPlatforms = createCapacitorPlatforms(win))

export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms(
(typeof globalThis !== 'undefined'
? globalThis
: typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: {}) as any,
);

export const addPlatform = CapacitorPlatforms.addPlatform;
export const setPlatform = CapacitorPlatforms.setPlatform;
18 changes: 12 additions & 6 deletions core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
WindowCapacitor,
} from './definitions-internal';
import { CapacitorException, getPlatformId, ExceptionCode } from './util';
import type { CapacitorPlatformsInstance } from './platforms';

export interface RegisteredPlugin {
readonly name: string;
Expand All @@ -15,12 +16,15 @@ export interface RegisteredPlugin {
export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {
const cap: CapacitorInstance = win.Capacitor || ({} as any);
const Plugins = (cap.Plugins = cap.Plugins || ({} as any));
const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms;

const getPlatform = () => getPlatformId(win);
const defaultGetPlatform = () => getPlatformId(win);
const getPlatform = capPlatforms.currentPlatform.getPlatform || defaultGetPlatform;

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

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

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

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

const getPluginHeader = (pluginName: string): PluginHeader | undefined =>
cap.PluginHeaders?.find(h => h.name === pluginName);
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 All @@ -53,7 +58,7 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {

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

const registerPlugin = (
const defaultRegisterPlugin = (
pluginName: string,
jsImplementations: PluginImplementations = {},
): any => {
Expand Down Expand Up @@ -209,6 +214,7 @@ 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