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(ios): CAPPluginMethod selector-based initializer #7412

Merged
merged 3 commits into from
Jun 11, 2024

Conversation

Steven0351
Copy link
Member

@Steven0351 Steven0351 commented Apr 18, 2024

Add secondary initializer for CAPPluginMethod that takes a selector. Creates a convenience initializer as well with a Swift enum to avoid having to rely on global string values in the style of CAPPluginReturnPromise. For a pure Swift plugin implementation, it makes the definition potentially go from:

class MyPlugin: CAPPlugin, CAPBridgedPlugin {
  let pluginMethods: [CAPPluginMethod] = [
    .init(name: "promiseMethod", returnType: CAPPluginReturnPromise),
    .init(name: "callbackMethod", returnType: CAPPluginReturnCallback)
  ]

  @objc func promiseMethod(_ call: CAPPluginCall) {}
  @objc func callbackMethod(_ call: CAPPluginCall) {}
}

to this:

class MyPlugin: CAPPlugin, CAPBridgedPlugin {
  let pluginMethods: [CAPPluginMethod] = [
    .init(#selector(promiseMethod)),
    .init(#selector(callbackMethod), returnType: .callback)
  ]

  @objc func promiseMethod(_ call: CAPPluginCall) {}
  @objc func callbackMethod(_ call: CAPPluginCall) {}
}

A benefit of this approach include removing the need to stringify the name of the function. This will also validate the selector at compile time, so if it has not been annotated with @objc it will be a compiler error.

selector. Creates a convenience initializer as well with a Swift enum.

extension CAPPluginMethod {
public enum ReturnType: String {
case promise, callback
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add the none return type?

it's not used by any plugin other than console (as far as I know)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely could. none is weird in that the web code treats anything that is not a promise as a callback https://github.com/ionic-team/capacitor/blob/main/core/src/runtime.ts#L123-L133. The docs are actually misleading https://capacitorjs.com/docs/plugins/method-types#void-return in that it says "You can check the promise for an error but when it resolves the result is ignored" because you can't actually check for an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add it, so if we want to do a refactor in the future for the core plugins, we can use this new initializer in all of them instead of having to use a different one for Console plugin

@jcesarmobile jcesarmobile merged commit 44c5b55 into main Jun 11, 2024
6 checks passed
@jcesarmobile jcesarmobile deleted the cappluginmethod-alternative-init branch June 11, 2024 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants