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): Unifying saving plugin calls #4253

Merged
merged 6 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 9 additions & 2 deletions ios/Capacitor/Capacitor/CAPBridgeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ import WebKit
@available(*, deprecated, message: "Moved - equivalent is found on config.localURL")
func getLocalUrl() -> String

@available(*, deprecated, renamed: "savedCall(withID:)")
func getSavedCall(_ callbackId: String) -> CAPPluginCall?

@available(*, deprecated, renamed: "releaseCall(withID:)")
func releaseCall(callbackId: String)

// MARK: - Plugin Access
func plugin(withName: String) -> CAPPlugin?

// MARK: - Call Management
func getSavedCall(_ callbackId: String) -> CAPPluginCall?
func saveCall(_ call: CAPPluginCall)
func savedCall(withID: String) -> CAPPluginCall?
func releaseCall(_ call: CAPPluginCall)
func releaseCall(callbackId: String)
func releaseCall(withID: String)

// MARK: - JavaScript Handling
// `js` is a short name but needs to be preserved for backwards compatibility.
Expand Down
6 changes: 3 additions & 3 deletions ios/Capacitor/Capacitor/CAPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ - (void)notifyListeners:(NSString *)eventName data:(NSDictionary<NSString *,id>

- (void)addListener:(CAPPluginCall *)call {
NSString *eventName = [call.options objectForKey:@"eventName"];
[call setIsSaved:TRUE];
[call setKeepAlive:TRUE];
[self addEventListener:eventName listener:call];
}

- (void)removeListener:(CAPPluginCall *)call {
NSString *eventName = [call.options objectForKey:@"eventName"];
NSString *callbackId = [call.options objectForKey:@"callbackId"];
CAPPluginCall *storedCall = [self.bridge getSavedCall:callbackId];
CAPPluginCall *storedCall = [self.bridge savedCallWithID:callbackId];
[self removeEventListener:eventName listener:storedCall];
[self.bridge releaseCallWithCallbackId:callbackId];
[self.bridge releaseCallWithID:callbackId];
}

- (void)removeAllListeners:(CAPPluginCall *)call {
Expand Down
5 changes: 3 additions & 2 deletions ios/Capacitor/Capacitor/CAPPluginCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ typedef void(^CAPPluginCallErrorHandler)(CAPPluginCallError *error);

@interface CAPPluginCall : NSObject

@property (nonatomic, assign) BOOL isSaved;
@property (nonatomic, assign) BOOL isSaved DEPRECATED_MSG_ATTRIBUTE("Use 'keepAlive' instead.");
@property (nonatomic, assign) BOOL keepAlive;
@property (nonatomic, strong) NSString *callbackId;
@property (nonatomic, strong) NSDictionary *options;
@property (nonatomic, copy) CAPPluginCallSuccessHandler successHandler;
@property (nonatomic, copy) CAPPluginCallErrorHandler errorHandler;

- (instancetype)initWithCallbackId:(NSString *)callbackId options:(NSDictionary *)options success:(CAPPluginCallSuccessHandler)success error:(CAPPluginCallErrorHandler)error;

- (void)save;
- (void)save DEPRECATED_MSG_ATTRIBUTE("Use the 'keepAlive' property instead.");
@end
10 changes: 9 additions & 1 deletion ios/Capacitor/Capacitor/CAPPluginCall.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ - (instancetype)initWithCallbackId:(NSString *)callbackId options:(NSDictionary
return self;
}

- (BOOL)isSaved {
return self.keepAlive;
}

- (void)setIsSaved:(BOOL)saved {
self.keepAlive = saved;
}

- (void)save {
self.isSaved = true;
self.keepAlive = true;
}

@end
38 changes: 26 additions & 12 deletions ios/Capacitor/Capacitor/CapacitorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
return plugin
}

func savePluginCall(_ call: CAPPluginCall) {
storedCalls[call.callbackId] = call
}

// MARK: - CAPBridgeProtocol: Plugin Access

@objc public func plugin(withName: String) -> CAPPlugin? {
Expand All @@ -315,18 +311,34 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {

// MARK: - CAPBridgeProtocol: Call Management

@objc public func getSavedCall(_ callbackId: String) -> CAPPluginCall? {
return storedCalls[callbackId]
@objc public func saveCall(_ call: CAPPluginCall) {
storedCalls[call.callbackId] = call
}

@objc public func savedCall(withID: String) -> CAPPluginCall? {
return storedCalls[withID]
}

@objc public func releaseCall(_ call: CAPPluginCall) {
storedCalls.removeValue(forKey: call.callbackId)
releaseCall(withID: call.callbackId)
}

@objc public func releaseCall(withID: String) {
storedCalls.removeValue(forKey: withID)
}

// MARK: - Deprecated Versions

@objc public func getSavedCall(_ callbackId: String) -> CAPPluginCall? {
return savedCall(withID: callbackId)
}

@objc public func releaseCall(callbackId: String) {
storedCalls.removeValue(forKey: callbackId)
releaseCall(withID: callbackId)
}

// MARK: - Internal

func getDispatchQueue() -> DispatchQueue {
return self.dispatchQueue
}
Expand Down Expand Up @@ -406,9 +418,9 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
formattingDatesAsStrings: plugin.shouldStringifyDatesInCalls) ?? [:],
success: {(result: CAPPluginCallResult?, pluginCall: CAPPluginCall?) -> Void in
if let result = result {
self?.toJs(result: JSResult(call: call, callResult: result), save: pluginCall?.isSaved ?? false)
self?.toJs(result: JSResult(call: call, callResult: result), save: pluginCall?.keepAlive ?? false)
} else {
self?.toJs(result: JSResult(call: call, result: .dictionary([:])), save: pluginCall?.isSaved ?? false)
self?.toJs(result: JSResult(call: call, result: .dictionary([:])), save: pluginCall?.keepAlive ?? false)
}
}, error: {(error: CAPPluginCallError?) -> Void in
if let error = error {
Expand All @@ -424,8 +436,10 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {

if let pluginCall = pluginCall {
plugin.perform(selector, with: pluginCall)
if pluginCall.isSaved {
self?.savePluginCall(pluginCall)
if pluginCall.keepAlive {
self?.saveCall(pluginCall)
} else {
self?.releaseCall(pluginCall)
}
}

Expand Down