-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add
registerPlugin
for importing from plugin packages (#…
- Loading branch information
Showing
23 changed files
with
229 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { Capacitor, Plugins } from './global'; | ||
import { WebPlugin } from './web'; | ||
|
||
const PLUGIN_REGISTRY = new (class { | ||
protected readonly plugins: { | ||
[plugin: string]: RegisteredPlugin<unknown>; | ||
} = {}; | ||
|
||
get(name: string): RegisteredPlugin<unknown> | undefined { | ||
return this.plugins[name]; | ||
} | ||
|
||
getAll(): RegisteredPlugin<unknown>[] { | ||
return Object.values(this.plugins); | ||
} | ||
|
||
has(name: string): boolean { | ||
return !!this.get(name); | ||
} | ||
|
||
register(plugin: RegisteredPlugin<unknown>): void { | ||
this.plugins[plugin.name] = plugin; | ||
} | ||
})(); | ||
|
||
/** | ||
* A map of plugin implementations. | ||
* | ||
* Each key should be the lowercased platform name as recognized by Capacitor, | ||
* e.g. 'android', 'ios', and 'web'. Each value must be an instance of a plugin | ||
* implementation for the respective platform. | ||
*/ | ||
export type PluginImplementations<T> = { | ||
[platform: string]: T; | ||
}; | ||
|
||
/** | ||
* Represents a plugin registered with Capacitor. | ||
*/ | ||
export class RegisteredPlugin<T> { | ||
constructor( | ||
readonly name: string, | ||
readonly implementations: Readonly<PluginImplementations<T>>, | ||
) {} | ||
|
||
/** | ||
* Return the appropriate implementation of this plugin. | ||
* | ||
* Supply a platform to return the implementation for it, otherwise this | ||
* method will return the implementation for the current platform as detected | ||
* by Capacitor. | ||
* | ||
* @param platform Optionally return the implementation of the given | ||
* platform. | ||
*/ | ||
getImplementation(platform?: string): T | undefined { | ||
return this.implementations[platform ? platform : Capacitor.platform]; | ||
} | ||
} | ||
|
||
/** | ||
* Register plugin implementations with Capacitor. | ||
* | ||
* This function will create and register an instance that contains the | ||
* implementations of the plugin. | ||
* | ||
* Each plugin has multiple implementations, one per platform. Each | ||
* implementation must adhere to a common interface to ensure client code | ||
* behaves consistently across each platform. | ||
* | ||
* @param name The unique CamelCase name of this plugin. | ||
* @param implementations The map of plugin implementations. | ||
*/ | ||
export const registerPlugin = <T>( | ||
name: string, | ||
implementations: Readonly<PluginImplementations<T>>, | ||
): RegisteredPlugin<T> => { | ||
const plugin = new RegisteredPlugin(name, implementations); | ||
PLUGIN_REGISTRY.register(plugin); | ||
|
||
return plugin; | ||
}; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @deprecated Don't use this. | ||
*/ | ||
export const registerWebPlugin = (plugin: WebPlugin) => { | ||
console.warn( | ||
`Capacitor plugin ${plugin.config.name} is using deprecated method 'registerWebPlugin'`, | ||
); // TODO: add link to upgrade guide | ||
|
||
if (!PLUGIN_REGISTRY.has(plugin.config.name)) { | ||
const { name, platforms = ['web'] } = plugin.config; | ||
const implementations: PluginImplementations<unknown> = {}; | ||
|
||
PLUGIN_REGISTRY.register( | ||
new RegisteredPlugin( | ||
name, | ||
platforms.reduce((acc, value) => { | ||
acc[value] = plugin; | ||
return acc; | ||
}, implementations), | ||
), | ||
); | ||
|
||
mergeWebPlugin(plugin); | ||
} | ||
}; | ||
|
||
const shouldMergeWebPlugin = (plugin: WebPlugin) => { | ||
return ( | ||
plugin.config.platforms && | ||
plugin.config.platforms.indexOf(Capacitor.platform) >= 0 | ||
); | ||
}; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @deprecated Don't use this. | ||
*/ | ||
export const mergeWebPlugin = (plugin: WebPlugin) => { | ||
if ( | ||
Plugins.hasOwnProperty(plugin.config.name) && | ||
!shouldMergeWebPlugin(plugin) | ||
) { | ||
return; | ||
} | ||
|
||
Plugins[plugin.config.name] = plugin; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,55 @@ | ||
import { Plugins } from './global'; | ||
import { mergeWebPlugins, mergeWebPlugin, WebPlugin } from './web/index'; | ||
import { mergeWebPlugin } from './plugins'; | ||
|
||
import { Accessibility } from './web/accessibility'; | ||
import { App } from './web/app'; | ||
import { Browser } from './web/browser'; | ||
import { Camera } from './web/camera'; | ||
import { Clipboard } from './web/clipboard'; | ||
import { Device } from './web/device'; | ||
import { Filesystem } from './web/filesystem'; | ||
import { Geolocation } from './web/geolocation'; | ||
import { LocalNotifications } from './web/local-notifications'; | ||
import { Modals } from './web/modals'; | ||
import { Motion } from './web/motion'; | ||
import { Network } from './web/network'; | ||
import { Permissions } from './web/permissions'; | ||
import { Share } from './web/share'; | ||
import { SplashScreen } from './web/splash-screen'; | ||
import { Storage } from './web/storage'; | ||
import { Toast } from './web/toast'; | ||
|
||
export * from './web/accessibility'; | ||
export * from './web/app'; | ||
export * from './web/browser'; | ||
export * from './web/camera'; | ||
export * from './web/clipboard'; | ||
export * from './web/device'; | ||
export * from './web/filesystem'; | ||
export * from './web/geolocation'; | ||
export * from './web/device'; | ||
export * from './web/local-notifications'; | ||
export * from './web/share'; | ||
export * from './web/modals'; | ||
export * from './web/motion'; | ||
export * from './web/network'; | ||
export * from './web/permissions'; | ||
export * from './web/share'; | ||
export * from './web/splash-screen'; | ||
export * from './web/storage'; | ||
export * from './web/toast'; | ||
|
||
mergeWebPlugins(Plugins); | ||
|
||
export const registerWebPlugin = (plugin: WebPlugin) => { | ||
mergeWebPlugin(Plugins, plugin); | ||
}; | ||
mergeWebPlugin(Accessibility); | ||
mergeWebPlugin(App); | ||
mergeWebPlugin(Browser); | ||
mergeWebPlugin(Camera); | ||
mergeWebPlugin(Clipboard); | ||
mergeWebPlugin(Device); | ||
mergeWebPlugin(Filesystem); | ||
mergeWebPlugin(Geolocation); | ||
mergeWebPlugin(LocalNotifications); | ||
mergeWebPlugin(Modals); | ||
mergeWebPlugin(Motion); | ||
mergeWebPlugin(Network); | ||
mergeWebPlugin(Permissions); | ||
mergeWebPlugin(Share); | ||
mergeWebPlugin(SplashScreen); | ||
mergeWebPlugin(Storage); | ||
mergeWebPlugin(Toast); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.