Skip to content

Commit

Permalink
fix(core): provide mock implementation for unimplemented platforms (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored Jul 29, 2020
1 parent 562c157 commit befe230
Showing 1 changed file with 29 additions and 2 deletions.
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

0 comments on commit befe230

Please sign in to comment.