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

fix(core): provide mock implementation for unimplemented platforms #3352

Merged
merged 3 commits into from
Jul 29, 2020
Merged
Changes from 2 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
31 changes: 29 additions & 2 deletions core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,38 @@ export class RegisteredPlugin<T> {
* method will return the implementation for the current platform as detected
* by Capacitor.
*
* If an implementation for the specified platform does not exist, a mock
* implementation is returned which throws 'unimplemented' errors for every
* method call and property access.
*
* @param platform Optionally return the implementation of the given
* platform.
*/
getImplementation(platform?: string): T | undefined {
return this.implementations[platform ? platform : Capacitor.platform];
getImplementation(platform: string = Capacitor.platform): T {
const implementation = this.implementations[platform];

if (!implementation) {
// Degraded experience for non-Proxy environments, e.g. IE11.
if (typeof Proxy === 'undefined') {
return;
}

return new Proxy({}, this.createImplementationProxyHandler(platform));
}

return implementation;
}

protected createImplementationProxyHandler(
platform: string,
): ProxyHandler<any> {
return {
get: () => {
throw new Error(
`${this.name} does not have an implementation for ${platform}.`,
);
},
};
}
}

Expand Down