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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor.xcodeproj/project.pbxproj
Expand Up @@ -88,6 +88,7 @@
A38C3D7B2848BE6F004B3680 /* CapacitorCookieManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A38C3D7A2848BE6F004B3680 /* CapacitorCookieManager.swift */; };
A71289E627F380A500DADDF3 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71289E527F380A500DADDF3 /* Router.swift */; };
A71289EB27F380FD00DADDF3 /* RouterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71289EA27F380FD00DADDF3 /* RouterTests.swift */; };
A7187FD22BD1CB7D00093C45 /* CAPPluginMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7187FD12BD1CB7D00093C45 /* CAPPluginMethod.swift */; };
A76739792B98E09700795F7B /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = A76739782B98E09700795F7B /* PrivacyInfo.xcprivacy */; };
A7BE62CC2B486A5400165ACB /* KeyValueStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7BE62CB2B486A5400165ACB /* KeyValueStore.swift */; };
A7D8B3522B238A840003FAD6 /* JSValueEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D8B3512B238A840003FAD6 /* JSValueEncoder.swift */; };
Expand Down Expand Up @@ -241,6 +242,7 @@
A38C3D7A2848BE6F004B3680 /* CapacitorCookieManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapacitorCookieManager.swift; sourceTree = "<group>"; };
A71289E527F380A500DADDF3 /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
A71289EA27F380FD00DADDF3 /* RouterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouterTests.swift; sourceTree = "<group>"; };
A7187FD12BD1CB7D00093C45 /* CAPPluginMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CAPPluginMethod.swift; sourceTree = "<group>"; };
A76739782B98E09700795F7B /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
A7BE62CB2B486A5400165ACB /* KeyValueStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyValueStore.swift; sourceTree = "<group>"; };
A7D8B3512B238A840003FAD6 /* JSValueEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSValueEncoder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -356,6 +358,7 @@
A7DB03AB29B001E300888AE9 /* CAPBridgedPlugin+getMethod.swift */,
62959B092524DA7700A3D7F1 /* CAPPluginMethod.h */,
62959AE82524DA7700A3D7F1 /* CAPPluginMethod.m */,
A7187FD12BD1CB7D00093C45 /* CAPPluginMethod.swift */,
62959AE22524DA7700A3D7F1 /* CAPPluginCall.h */,
62959B062524DA7700A3D7F1 /* CAPPluginCall.m */,
62959AE62524DA7700A3D7F1 /* CAPPluginCall.swift */,
Expand Down Expand Up @@ -700,6 +703,7 @@
62D43AF02581817500673C24 /* WKWebView+Capacitor.swift in Sources */,
62959B432524DA7800A3D7F1 /* Data+Capacitor.swift in Sources */,
62E207AE2588234500A78983 /* WebViewDelegationHandler.swift in Sources */,
A7187FD22BD1CB7D00093C45 /* CAPPluginMethod.swift in Sources */,
621ECCBC2542046400D3D615 /* JSTypes.swift in Sources */,
621ECCDA254205C400D3D615 /* CapacitorBridge.swift in Sources */,
62959B382524DA7800A3D7F1 /* CAPPluginCall.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/CAPPluginMethod.h
Expand Up @@ -31,6 +31,6 @@ typedef NSString CAPPluginReturnType;
@property (nonatomic, strong) CAPPluginReturnType *returnType; // Return type of method (i.e. callback/promise/sync)

- (instancetype)initWithName:(NSString *)name returnType:(CAPPluginReturnType *)returnType;

- (instancetype)initWithSelector:(SEL)selector returnType:(CAPPluginReturnType *)returnType;

@end
8 changes: 8 additions & 0 deletions ios/Capacitor/Capacitor/CAPPluginMethod.m
Expand Up @@ -31,6 +31,14 @@ -(instancetype)initWithName:(NSString *)name returnType:(CAPPluginReturnType *)r
return self;
}

-(instancetype)initWithSelector:(SEL) selector returnType:(CAPPluginReturnType *)returnType {
// need to drop the : from the selector string
NSString* rawSelString = NSStringFromSelector(selector);
self.name = [rawSelString substringToIndex:[rawSelString length] - 1];
self.selector = selector;
self.returnType = returnType;
return self;
}

@end

17 changes: 17 additions & 0 deletions ios/Capacitor/Capacitor/CAPPluginMethod.swift
@@ -0,0 +1,17 @@
//
// CAPPluginMethod.swift
// Capacitor
//
// Created by Steven Sherry on 4/18/24.
// Copyright © 2024 Drifty Co. All rights reserved.
//

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

}

public convenience init(_ selector: Selector, returnType: ReturnType = .promise) {
self.init(selector: selector, returnType: returnType.rawValue)
}
}