From 2a72b5a996de2638af14022b946f459e7365c88a Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Fri, 1 Dec 2023 01:25:22 +0100 Subject: [PATCH 1/6] Ios refactoring --- .github/workflows/gradle-publish.yml | 20 ++++++ ios/src/Plugin.swift | 36 ++++------ ios/src/actions/BaseAction.swift | 37 ++++++----- ios/src/actions/CallContext.swift | 70 ++++++++++---------- ios/src/actions/Mappers.swift | 66 +++++++++--------- ios/src/actions/PluginCallbackInternal.swift | 2 - ios/src/helpers/CallbacksHelpers.swift | 3 + ios/src/helpers/DictionaryHelpers.swift | 40 +++++++++++ ios/src/json/Helpers.swift | 27 +++++--- ios/src/json/JsonArray.swift | 50 +++++++------- ios/src/json/JsonObject.swift | 54 +++++++-------- ios/src/json/MutableJsonArray.swift | 44 ++++++------ ios/src/json/MutableJsonObject.swift | 44 ++++++------ package.json | 2 +- 14 files changed, 282 insertions(+), 213 deletions(-) create mode 100644 .github/workflows/gradle-publish.yml create mode 100644 ios/src/helpers/CallbacksHelpers.swift create mode 100644 ios/src/helpers/DictionaryHelpers.swift diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml new file mode 100644 index 0000000..cc0cd4b --- /dev/null +++ b/.github/workflows/gradle-publish.yml @@ -0,0 +1,20 @@ +name: Publish package +on: + push +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + # Setup .npmrc file to publish to npm + - uses: actions/setup-node@v3 + with: + node-version: '20.x' + registry-url: 'https://registry.npmjs.org' + - run: yarn && yarn build + - run: yarn publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/ios/src/Plugin.swift b/ios/src/Plugin.swift index 57f66c8..54d28d3 100644 --- a/ios/src/Plugin.swift +++ b/ios/src/Plugin.swift @@ -9,34 +9,34 @@ open class CorePlugin: IPluginL private struct Session { let wrapperDelegate: CapacitorPluginDelegate } - + private let delegate: TDelegate private let mappers: TMappers - + public init(delegate: TDelegate, mappers: TMappers) { self.delegate = delegate self.mappers = mappers } - + private var session_: Session? = nil private var session: Session { get { return session_! } } public var wrapperDelegate: CapacitorPluginDelegate { get { return session.wrapperDelegate } } - + public func initialize(wrapperDelegate: CapacitorPluginDelegate) { session_ = Session(wrapperDelegate: wrapperDelegate) } - + public func sendEvent(_ event: ICoreEvent) { - if (event is Context) { - (event as! Context).initialize(callback: self, delegate: delegate, mappers: mappers) + if let event = event as? Context { + event.initialize(callback: self, delegate: delegate, mappers: mappers) } wrapperDelegate.sendEvent(event.name, event.getData()) } - + public func sendLog(_ action: String?, _ tag: String?, _ level: LogLevel, _ message: String, _ params: LogParams) { sendEvent(LogEvent(action: action, tag: tag, level: level, message: message, params: params)) } - + public func call(_ actionType: CoreBaseAction.Type, _ call: CAPPluginCall) { let context = CallContext(call: call, mappers: mappers) do { @@ -44,30 +44,22 @@ open class CorePlugin: IPluginL action.initialize(callback: self, delegate: delegate, mappers: mappers, call: context) try action.onExecute() } catch { - reportError(error, call: context, finish: true) + mappers.reportError(error, call: context, finish: true) } } - - public func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) { - mappers.reportSuccess(data, call: call, finish: finish) - } - - public func reportError(_ error: Error?, call: CallContext, finish: Bool) { - mappers.reportError(error, call: call, finish: finish) - } - + public func logger(tag: String?) -> ILogger { return Logger(action: nil, tag: tag, params: nil, pluginLogger: self) } - + public func logger() -> ILogger { return logger(tag: nil) } - + public func loggerLegacy(tag: String?) -> ILoggerLegacy { return LoggerLegacy(action: nil, tag: tag, params: nil, pluginLogger: self) } - + public func loggerLegacy() -> ILoggerLegacy { return loggerLegacy(tag: nil) } diff --git a/ios/src/actions/BaseAction.swift b/ios/src/actions/BaseAction.swift index a403988..572f155 100644 --- a/ios/src/actions/BaseAction.swift +++ b/ios/src/actions/BaseAction.swift @@ -5,9 +5,9 @@ open class CoreBaseAction: ContextWithCall Void) { DispatchQueue.main.async { do { @@ -17,7 +17,7 @@ open class CoreBaseAction: ContextWithCall Void) { Task.init { do { @@ -27,35 +27,38 @@ open class CoreBaseAction: ContextWithCall ILogger { return Logger(action: getClassName(), tag: tag, params: nil, pluginLogger: callback) } - + public func logger() -> ILogger { return logger(tag: nil) } - + private func getClassName() -> String { let fullName = String(describing: self) let parts = fullName.split(separator: ".") - return parts.last!.description + return parts.last?.description ?? "Unknown class name" } - + public func sendEvent(_ event: ICoreEvent) { callback.sendEvent(event) } diff --git a/ios/src/actions/CallContext.swift b/ios/src/actions/CallContext.swift index 3ab76b6..7d3c0cb 100644 --- a/ios/src/actions/CallContext.swift +++ b/ios/src/actions/CallContext.swift @@ -3,130 +3,130 @@ import Capacitor public class CallContext { private let call: CAPPluginCall private let mappers: CoreMappers - + init(call: CAPPluginCall, mappers: CoreMappers) { self.call = call self.mappers = mappers } - + public func asObject() -> CallContextAsJsonObject { return CallContextAsJsonObject(call: call) } - - public func success(_ data: PluginCallResultData?, finish: Bool) { + + public func success(_ data: JsonObject?, finish: Bool) { call.keepAlive = !finish - if (data == nil) { + if let data = data { + call.resolve(data.toRaw() as PluginCallResultData) + } else { call.resolve() - return } - - call.resolve(data!) } - + public func error(_ error: Error?, finish: Bool) { call.keepAlive = !finish - + let errorData = prepareErrorData(error) - + let message = error?.localizedDescription ?? "Unknown error" - if (errorData == nil) { + if let errorData = errorData { + call.reject(message, nil, nil, errorData.toRaw() as PluginCallResultData) + } else { call.reject(message) - return } - - call.reject(message, nil, nil, errorData!) } - - private func prepareErrorData(_ error: Error?) -> PluginCallResultData? { - if (error == nil) { return nil } - let pluginError = mappers.errorMapper.map(error!) - return mappers.errorMapper.mapToJson(pluginError)?.toRaw() + + private func prepareErrorData(_ error: Error?) -> JsonObject? { + guard let error = error else { + return nil + } + let pluginError = mappers.errorMapper.map(error) + return mappers.errorMapper.mapToJson(pluginError) } } public class CallContextAsJsonObject: IJsonObjectProperties { private let call: CAPPluginCall - + init(call: CAPPluginCall) { self.call = call } - + public func opt(_ name: String) -> JsonValue? { return nil } - + public func optString(_ name: String) -> String? { return call.getString(name) } - + public func optInt(_ name: String) -> Int? { return call.getInt(name) } - + public func optDouble(_ name: String) -> Double? { return call.getDouble(name) } - + public func optBool(_ name: String) -> Bool? { return call.getBool(name) } - + public func optObject(_ name: String) -> JsonObject? { guard let raw = call.getObject(name) else { return nil } return JsonObject.fromRaw(raw) } - + public func optArray(_ name: String) -> JsonArray? { guard let raw = call.getArray(name) else { return nil } return JsonArray.fromRaw(raw) } - + public func get(_ name: String) throws -> JsonValue { guard let result = opt(name) else { throw PluginError(message: "Value for required '\(name)' is nil") } return result } - + public func getString(_ name: String) throws -> String { guard let result = optString(name) else { throw PluginError(message: "Value for required string '\(name)' is nil") } return result } - + public func getInt(_ name: String) throws -> Int { guard let result = optInt(name) else { throw PluginError(message: "Value for required int '\(name)' is nil") } return result } - + public func getDouble(_ name: String) throws -> Double { guard let result = optDouble(name) else { throw PluginError(message: "Value for required double '\(name)' is nil") } return result } - + public func getBool(_ name: String) throws -> Bool { guard let result = optBool(name) else { throw PluginError(message: "Value for required bool '\(name)' is nil") } return result } - + public func getObject(_ name: String) throws -> JsonObject { guard let result = optObject(name) else { throw PluginError(message: "Value for required object '\(name)' is nil") } return result } - + public func getArray(_ name: String) throws -> JsonArray { guard let result = optArray(name) else { throw PluginError(message: "Value for required array '\(name)' is nil") diff --git a/ios/src/actions/Mappers.swift b/ios/src/actions/Mappers.swift index a34adbf..896fa88 100644 --- a/ios/src/actions/Mappers.swift +++ b/ios/src/actions/Mappers.swift @@ -2,27 +2,27 @@ import Capacitor open class CoreMappers { public init() {} - - func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) { + + func reportSuccess(_ data: JsonObject?, call: CallContext, finish: Bool) { call.success(data, finish: finish) } - + func reportError(_ error: Error?, call: CallContext, finish: Bool) { errorMapper.reportError(error, call: call, finish: finish) } - + open func createErrorMapper() -> CoreErrorMapper { return CoreErrorMapper() } - + lazy var errorMapper: CoreErrorMapper = { createErrorMapper() }() - + open func createLogMapper() -> CoreLogMapper { return CoreLogMapper() } - + lazy var logMapper: CoreLogMapper = { createLogMapper() }() @@ -30,33 +30,32 @@ open class CoreMappers { open class CoreErrorMapper { public init() {} - + open func map(_ error: Error) -> PluginError { - if (error is PluginError) { - return error as! PluginError + if let error = error as? PluginError { + return error } return PluginError(message: error.localizedDescription, cause: error) } - - open func mapToJson(_ error_: Error) -> JsonObject? { - if (!(error_ is PluginError)) { + + open func mapToJson(_ error: Error) -> JsonObject? { + guard let error = error as? PluginError else { return nil } - let error = error_ as! PluginError let data = mutableJsonObject() - + if let errorMessage = error.message { data.put("message", errorMessage) } - + if let errorCode = error.code { data.put("code", errorCode) } - + return data } - + func reportError(_ error: Error?, call: CallContext, finish: Bool) { call.error(error, finish: finish) } @@ -66,17 +65,20 @@ public typealias LogMapperObjectFormatter = (_ obj: T) throws -> JsonObject open class CoreLogMapper { private var formatters: Array<(type: Any.Type, formatter: LogMapperObjectFormatter)> = [] - + public init() { } - + public func register(type: T.Type, formatter: @escaping LogMapperObjectFormatter) { let f: LogMapperObjectFormatter = { c in - return try formatter(c as! T) + guard let c = c as? T else { + throw PluginError(message: "Object formatting error, cannot cast to required type") + } + return try formatter(c) } formatters.append((type, f)) } - + private func mapLogObject(_ obj: Any) -> JsonObject? { let type = type(of: obj) if let formatter = formatters.first(where: { it in it.type == type })?.formatter { @@ -85,7 +87,7 @@ open class CoreLogMapper { return nil } } - + internal func getLogTypeValue(_ type: LogLevel) -> String { switch(type) { case .Warning: @@ -100,7 +102,7 @@ open class CoreLogMapper { return "Trace" } } - + internal func formatParams(_ params: LogParams) -> JsonObject { let result = mutableJsonObject() for param in params { @@ -112,11 +114,11 @@ open class CoreLogMapper { } return result } - + private func formatValue(_ value: Any?) -> JsonValue? { if let value = value { - if (value is Array) { - return formatArray(value as! Array) + if let value = value as? Array { + return formatArray(value) } else if ( value is String || value is Int || @@ -128,10 +130,10 @@ open class CoreLogMapper { return formatObject(value) } } - + return nil } - + private func formatArray(_ array: Array) -> JsonArray { let result = mutableJsonArray() array.forEach { it in @@ -143,13 +145,13 @@ open class CoreLogMapper { } return result } - + private func formatObject(_ obj: Any) -> JsonValue? { if let formatted = mapLogObject(obj) { return formatted } - if (obj is AnyClass) { - return (obj as! AnyClass).description() + if let obj = obj as? AnyClass { + return obj.description() } return nil } diff --git a/ios/src/actions/PluginCallbackInternal.swift b/ios/src/actions/PluginCallbackInternal.swift index 07bd76a..79e0684 100644 --- a/ios/src/actions/PluginCallbackInternal.swift +++ b/ios/src/actions/PluginCallbackInternal.swift @@ -1,6 +1,4 @@ import Capacitor public protocol PluginCallbackInternal: IPluginLogger, IEventSender { - func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) - func reportError(_ error: Error?, call: CallContext, finish: Bool) } diff --git a/ios/src/helpers/CallbacksHelpers.swift b/ios/src/helpers/CallbacksHelpers.swift new file mode 100644 index 0000000..c4abe58 --- /dev/null +++ b/ios/src/helpers/CallbacksHelpers.swift @@ -0,0 +1,3 @@ +public typealias OnSuccess = () -> Void +public typealias OnSuccess1 = (_ value: T) -> Void +public typealias OnError = (_ cause: Error) -> Void diff --git a/ios/src/helpers/DictionaryHelpers.swift b/ios/src/helpers/DictionaryHelpers.swift new file mode 100644 index 0000000..a965ab7 --- /dev/null +++ b/ios/src/helpers/DictionaryHelpers.swift @@ -0,0 +1,40 @@ +public typealias HashableDictionary = Dictionary + +public func cannotGet(_ name: String, type: String, from: String) -> Error { + return PluginError(message: "Cannot get \"\(name)\" \(type) from \(from)") +} + +public func getDictionaryFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> HashableDictionary { + guard let result = dict[name] as? HashableDictionary else { + throw cannotGet(name, type: "dictionary", from: "dictionary") + } + return result +} + +public func optStringFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> String? { + guard let result = dict[name] as? String? else { + throw cannotGet(name, type: "optional string", from: "dictionary") + } + return result +} + +public func getStringFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> String { + guard let result = dict[name] as? String else { + throw cannotGet(name, type: "string", from: "dictionary") + } + return result +} + +public func getDoubleFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> Double { + guard let result = dict[name] as? Double else { + throw cannotGet(name, type: "double", from: "dictionary") + } + return result +} + +public func getIntFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> Int { + guard let result = dict[name] as? Int else { + throw cannotGet(name, type: "int", from: "dictionary") + } + return result +} diff --git a/ios/src/json/Helpers.swift b/ios/src/json/Helpers.swift index 224e860..0e07c80 100644 --- a/ios/src/json/Helpers.swift +++ b/ios/src/json/Helpers.swift @@ -1,9 +1,20 @@ -public func mutableJsonObject() -> MutableJsonObject { - return MutableJsonObject() +public typealias MutableJsonObjectInit = (_ obj: MutableJsonObject) -> Void +public typealias MutableJsonArrayInit = (_ arr: MutableJsonArray) -> Void + +public func mutableJsonObject(initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject { + let obj = MutableJsonObject() + if let initFunc = initFunc { + initFunc(obj) + } + return obj } -public func mutableJsonArray() -> MutableJsonArray { - return MutableJsonArray() +public func mutableJsonArray(initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray { + let arr = MutableJsonArray() + if let initFunc = initFunc { + initFunc(arr) + } + return arr } func convertFromRawJsonElement(raw: JsonValueRaw?) -> JsonValue? { @@ -16,10 +27,10 @@ func convertFromRawJsonElement(raw: JsonValueRaw?) -> JsonValue? { ) { return raw } - else if (raw is Array) { - return JsonArray(raw as! Array) - } else if (raw is Dictionary) { - return JsonObject(raw as! Dictionary) + else if let raw = raw as? Array { + return JsonArray(raw) + } else if let raw = raw as? Dictionary { + return JsonObject(raw) } } return nil diff --git a/ios/src/json/JsonArray.swift b/ios/src/json/JsonArray.swift index 02fc15f..2d2421c 100644 --- a/ios/src/json/JsonArray.swift +++ b/ios/src/json/JsonArray.swift @@ -1,80 +1,80 @@ public class JsonArray: JsonElement, IJsonArray { internal var arr: Array - + internal init(_ arr: Array) { self.arr = arr } - + public static func fromRaw(_ arr: JsonArrayRaw) -> JsonArray { return JsonArray(arr) } - + public var size: Int { get { return arr.count } } - + public func opt(_ index: Int) -> JsonValue? { return convertFromRawJsonElement(raw: arr[index] as JsonValueRaw?) } - + public func optString(_ index: Int) -> String? { - return opt(index) as! String? + return opt(index) as? String? ?? nil } - + public func optInt(_ index: Int) -> Int? { - return opt(index) as! Int? + return opt(index) as? Int? ?? nil } - + public func optDouble(_ index: Int) -> Double? { - return opt(index) as! Double? + return opt(index) as? Double? ?? nil } - + public func optBool(_ index: Int) -> Bool? { - return opt(index) as! Bool? + return opt(index) as? Bool? ?? nil } - + public func optObject(_ index: Int) -> JsonObject? { - return opt(index) as! JsonObject? + return opt(index) as? JsonObject? ?? nil } - + public func optArray(_ index: Int) -> JsonArray? { - return opt(index) as! JsonArray? + return opt(index) as? JsonArray? ?? nil } - + public func get(_ index: Int) throws -> JsonValue { return try require(index, opt) } - + public func getString(_ index: Int) throws -> String { return try require(index, optString) } - + public func getInt(_ index: Int) throws -> Int { return try require(index, optInt) } - + public func getDouble(_ index: Int) throws -> Double { return try require(index, optDouble) } - + public func getBool(_ index: Int) throws -> Bool { return try require(index, optBool) } - + public func getObject(_ index: Int) throws -> JsonObject { return try require(index, optObject) } - + public func getArray(_ index: Int) throws -> JsonArray { return try require(index, optArray) } - + public func mutate() -> MutableJsonArray { return MutableJsonArray(arr) } - + public func toRaw() -> JsonArrayRaw { return arr } diff --git a/ios/src/json/JsonObject.swift b/ios/src/json/JsonObject.swift index 40e7c7b..ee6689b 100644 --- a/ios/src/json/JsonObject.swift +++ b/ios/src/json/JsonObject.swift @@ -1,83 +1,83 @@ public class JsonObject: JsonElement, IJsonObject { internal var dict: Dictionary - + internal init(_ dict: Dictionary) { self.dict = dict } - + public static func fromRaw(_ dict: JsonObjectRaw) -> JsonObject { return JsonObject(dict) } - + public static func fromObject(_ obj: T) throws -> JsonObject where T: Encodable { let data = try JSONEncoder().encode(obj) let result = try JSONSerialization.jsonObject(with: data) - if (result is Dictionary) { - return JsonObject(result as! Dictionary) + if let result = result as? Dictionary { + return JsonObject(result) } throw PluginError(message: "Cannot create json object") } - + public func opt(_ name: String) -> JsonValue? { return convertFromRawJsonElement(raw: dict[name] as JsonValueRaw?) } - + public func optString(_ name: String) -> String? { - return opt(name) as! String? + return opt(name) as? String? ?? nil } - + public func optInt(_ name: String) -> Int? { - return opt(name) as! Int? + return opt(name) as? Int? ?? nil } - + public func optDouble(_ name: String) -> Double? { - return opt(name) as! Double? + return opt(name) as? Double? ?? nil } - + public func optBool(_ name: String) -> Bool? { - return opt(name) as! Bool? + return opt(name) as? Bool? ?? nil } - + public func optObject(_ name: String) -> JsonObject? { - return opt(name) as! JsonObject? + return opt(name) as? JsonObject? ?? nil } - + public func optArray(_ name: String) -> JsonArray? { - return opt(name) as! JsonArray? + return opt(name) as? JsonArray? ?? nil } - + public func get(_ name: String) throws -> JsonValue { return try require(name, opt) } - + public func getString(_ name: String) throws -> String { return try require(name, optString) } - + public func getInt(_ name: String) throws -> Int { return try require(name, optInt) } - + public func getDouble(_ name: String) throws -> Double { return try require(name, optDouble) } - + public func getBool(_ name: String) throws -> Bool { return try require(name, optBool) } - + public func getObject(_ name: String) throws -> JsonObject { return try require(name, optObject) } - + public func getArray(_ name: String) throws -> JsonArray { return try require(name, optArray) } - + public func mutate() -> MutableJsonObject { return MutableJsonObject(dict) } - + public func toRaw() -> JsonObjectRaw { return dict } diff --git a/ios/src/json/MutableJsonArray.swift b/ios/src/json/MutableJsonArray.swift index c14ac71..83f2ba5 100644 --- a/ios/src/json/MutableJsonArray.swift +++ b/ios/src/json/MutableJsonArray.swift @@ -2,55 +2,55 @@ public class MutableJsonArray: JsonArray, IMutableJsonArray { override init(_ arr: Array) { super.init(arr) } - + internal convenience init() { self.init([]) } - + public func put(_ value: String, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Int, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Double, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Bool, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: JsonObject, index: Int? = nil) { putInternal(value.dict, index) } - + public func put(_ value: JsonArray, index: Int? = nil) { putInternal(value.arr, index) } - + public func putNull(index: Int? = nil) { putInternal(nil, index) } - + public func put(_ value: JsonValue, index: Int? = nil) { - if (value is String) { - put(value as! String, index: index) - } else if (value is Int) { - put(value as! Int, index: index) - } else if (value is Double) { - put(value as! Double, index: index) - } else if (value is Bool) { - put(value as! Bool, index: index) - } else if (value is JsonObject) { - put(value as! JsonObject, index: index) - } else if (value is JsonArray) { - put(value as! JsonArray, index: index) + if let value = value as? String { + put(value, index: index) + } else if let value = value as? Int { + put(value, index: index) + } else if let value = value as? Double { + put(value, index: index) + } else if let value = value as? Bool { + put(value, index: index) + } else if let value = value as? JsonObject { + put(value, index: index) + } else if let value = value as? JsonArray { + put(value, index: index) } } - + private func putInternal(_ value: JsonValueRaw?, _ index: Int?) { arr.append(value) } diff --git a/ios/src/json/MutableJsonObject.swift b/ios/src/json/MutableJsonObject.swift index 6eb84c2..012c126 100644 --- a/ios/src/json/MutableJsonObject.swift +++ b/ios/src/json/MutableJsonObject.swift @@ -2,55 +2,55 @@ public class MutableJsonObject: JsonObject, IMutableJsonObject { override init(_ dict: Dictionary) { super.init(dict) } - + internal convenience init() { self.init([:]) } - + public func put(_ name: String, _ value: String) { putInternal(name, value) } - + public func put(_ name: String, _ value: Int) { putInternal(name, value) } - + public func put(_ name: String, _ value: Double) { putInternal(name, value) } - + public func put(_ name: String, _ value: Bool) { putInternal(name, value) } - + public func put(_ name: String, _ value: JsonObject) { putInternal(name, value.dict) } - + public func put(_ name: String, _ value: JsonArray) { putInternal(name, value.arr) } - + public func putNull(_ name: String) { putInternal(name, nil) } - + public func put(_ name: String, _ value: JsonValue) { - if (value is String) { - put(name, value as! String) - } else if (value is Int) { - put(name, value as! Int) - } else if (value is Double) { - put(name, value as! Double) - } else if (value is Bool) { - put(name, value as! Bool) - } else if (value is JsonObject) { - put(name, value as! JsonObject) - } else if (value is JsonArray) { - put(name, value as! JsonArray) + if let value = value as? String { + put(name, value) + } else if let value = value as? Int { + put(name, value) + } else if let value = value as? Double { + put(name, value) + } else if let value = value as? Bool { + put(name, value) + } else if let value = value as? JsonObject { + put(name, value) + } else if let value = value as? JsonArray { + put(name, value) } } - + private func putInternal(_ name: String, _ value: JsonValueRaw?) { dict[name] = value } diff --git a/package.json b/package.json index be3079f..38f009e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.5.6-alpha.0", + "version": "5.6.0-alpha.0", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", From 8035e1a74b53463fae5ace9137e7dad1431347ba Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Sat, 2 Dec 2023 01:52:36 +0100 Subject: [PATCH 2/6] Ios refactoring --- ios/src/helpers/CallbacksHelpers.swift | 28 ++++++++++++++++++++ ios/src/helpers/ThreadingHelpers.swift | 9 +++++++ ios/src/helpers/UiControllerProvider.swift | 30 ++++++++++++++++++++++ ios/src/json/Helpers.swift | 4 +-- ios/src/json/JsonArray.swift | 6 ++++- ios/src/json/utils/JsonArrayIterator.swift | 16 ++++++++++++ package.json | 2 +- 7 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 ios/src/helpers/ThreadingHelpers.swift create mode 100644 ios/src/helpers/UiControllerProvider.swift create mode 100644 ios/src/json/utils/JsonArrayIterator.swift diff --git a/ios/src/helpers/CallbacksHelpers.swift b/ios/src/helpers/CallbacksHelpers.swift index c4abe58..c726b97 100644 --- a/ios/src/helpers/CallbacksHelpers.swift +++ b/ios/src/helpers/CallbacksHelpers.swift @@ -1,3 +1,31 @@ public typealias OnSuccess = () -> Void public typealias OnSuccess1 = (_ value: T) -> Void public typealias OnError = (_ cause: Error) -> Void + +public class ContinuationCallback { + private var continuation: CheckedContinuation? = nil + + public init() { + } + + public func setContinuation(_ continuation: CheckedContinuation) { + if let continuation = self.continuation { + continuation.resume(throwing: PluginError(message: "Cancelled because another callback was set")) + } + self.continuation = continuation + } + + public func onSuccess(_ result: TResult) { + if let continuation = continuation { + continuation.resume(returning: result) + } + continuation = nil + } + + public func onError(_ error: Error) { + if let continuation = continuation { + continuation.resume(throwing: error) + } + continuation = nil + } +} diff --git a/ios/src/helpers/ThreadingHelpers.swift b/ios/src/helpers/ThreadingHelpers.swift new file mode 100644 index 0000000..b48eda0 --- /dev/null +++ b/ios/src/helpers/ThreadingHelpers.swift @@ -0,0 +1,9 @@ +public func runInMainThreadAsync(_ action: @escaping () async throws -> T) async throws -> T { + return try await withCheckedThrowingContinuation { c in + DispatchQueue.main.async { + Task { + c.resume(returning: try await action()) + } + } + } +} diff --git a/ios/src/helpers/UiControllerProvider.swift b/ios/src/helpers/UiControllerProvider.swift new file mode 100644 index 0000000..c2214f2 --- /dev/null +++ b/ios/src/helpers/UiControllerProvider.swift @@ -0,0 +1,30 @@ +import Capacitor + +public protocol WithUiViewControllerProvider { + func setUiViewControllerProvider(viewControllerProvider: IUiViewControllerProvider) +} + +public extension IUiViewControllerProvider? { + func requireViewController() throws -> UIViewController { + guard let viewController = self?.getViewController() else { + throw PluginError(message: "viewControllerProvider is nil") + } + return viewController + } +} + +public protocol IUiViewControllerProvider { + func getViewController() -> UIViewController? +} + +public class UiViewControllerProvider: IUiViewControllerProvider { + private let plugin: CAPPlugin + + public init(plugin: CAPPlugin) { + self.plugin = plugin + } + + public func getViewController() -> UIViewController? { + return plugin.bridge?.viewController + } +} diff --git a/ios/src/json/Helpers.swift b/ios/src/json/Helpers.swift index 0e07c80..4c0938b 100644 --- a/ios/src/json/Helpers.swift +++ b/ios/src/json/Helpers.swift @@ -1,7 +1,7 @@ public typealias MutableJsonObjectInit = (_ obj: MutableJsonObject) -> Void public typealias MutableJsonArrayInit = (_ arr: MutableJsonArray) -> Void -public func mutableJsonObject(initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject { +public func mutableJsonObject(_ initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject { let obj = MutableJsonObject() if let initFunc = initFunc { initFunc(obj) @@ -9,7 +9,7 @@ public func mutableJsonObject(initFunc: MutableJsonObjectInit? = nil) -> Mutable return obj } -public func mutableJsonArray(initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray { +public func mutableJsonArray(_ initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray { let arr = MutableJsonArray() if let initFunc = initFunc { initFunc(arr) diff --git a/ios/src/json/JsonArray.swift b/ios/src/json/JsonArray.swift index 2d2421c..08ff437 100644 --- a/ios/src/json/JsonArray.swift +++ b/ios/src/json/JsonArray.swift @@ -1,4 +1,4 @@ -public class JsonArray: JsonElement, IJsonArray { +public class JsonArray: JsonElement, IJsonArray, Sequence { internal var arr: Array internal init(_ arr: Array) { @@ -78,4 +78,8 @@ public class JsonArray: JsonElement, IJsonArray { public func toRaw() -> JsonArrayRaw { return arr } + + public func makeIterator() -> JsonArrayIterator { + return JsonArrayIterator(self) + } } diff --git a/ios/src/json/utils/JsonArrayIterator.swift b/ios/src/json/utils/JsonArrayIterator.swift new file mode 100644 index 0000000..6140d25 --- /dev/null +++ b/ios/src/json/utils/JsonArrayIterator.swift @@ -0,0 +1,16 @@ +public class JsonArrayIterator: IteratorProtocol { + public typealias Element = JsonValue + + private let jsonArray: JsonArray + + private var index = -1 + + init(_ jsonArray: JsonArray) { + self.jsonArray = jsonArray + } + + public func next() -> JsonValue? { + index += 1; + return jsonArray.opt(index) + } +} diff --git a/package.json b/package.json index 38f009e..1cf69bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.6.0-alpha.0", + "version": "5.7.0-alpha.0", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", From 185cf62e172ab1d8316748423f111506b5d4d2dc Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Wed, 21 Feb 2024 23:52:41 +0100 Subject: [PATCH 3/6] Add log filter --- .github/workflows/gradle-publish.yml | 5 +- .nvmrc | 2 +- .../android/capacitor/core/CapacitorPlugin.kt | 32 ++++++++-- package.json | 4 +- src/definitions.ts | 10 ++- src/mappers.ts | 6 ++ src/plugin.ts | 26 ++++++-- yarn.lock | 62 +++++++++---------- 8 files changed, 101 insertions(+), 46 deletions(-) diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml index cc0cd4b..9238ebf 100644 --- a/.github/workflows/gradle-publish.yml +++ b/.github/workflows/gradle-publish.yml @@ -9,10 +9,9 @@ jobs: packages: write steps: - uses: actions/checkout@v4 - # Setup .npmrc file to publish to npm - - uses: actions/setup-node@v3 + - uses: actions/setup-node@v4 with: - node-version: '20.x' + node-version: '21.x' registry-url: 'https://registry.npmjs.org' - run: yarn && yarn build - run: yarn publish diff --git a/.nvmrc b/.nvmrc index 209e3ef..aabe6ec 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -20 +21 diff --git a/android/src/main/java/com/ionic/plugin/android/capacitor/core/CapacitorPlugin.kt b/android/src/main/java/com/ionic/plugin/android/capacitor/core/CapacitorPlugin.kt index bea3aeb..46bd2bf 100644 --- a/android/src/main/java/com/ionic/plugin/android/capacitor/core/CapacitorPlugin.kt +++ b/android/src/main/java/com/ionic/plugin/android/capacitor/core/CapacitorPlugin.kt @@ -3,15 +3,19 @@ package com.ionic.plugin.android.capacitor.core import android.app.Activity import android.content.Intent import com.getcapacitor.PluginCall +import com.getcapacitor.PluginMethod import com.ionic.plugin.android.capacitor.core.actions.CallContext import com.ionic.plugin.android.core.utils.ActivityResultObserver import com.ionic.plugin.android.core.utils.IActivityResultObserver +import com.ionic.plugin.core.actions.BaseAction import com.ionic.plugin.core.actions.Delegate +import com.ionic.plugin.core.actions.IActionCreator import com.ionic.plugin.core.actions.Mappers +import com.ionic.plugin.core.actions.SetLogLevelsAction import com.spryrocks.kson.JsonObject -import com.spryrocks.kson.mutableJsonObject -abstract class CapacitorPlugin, TMappers: Mappers> : com.getcapacitor.Plugin() { +abstract class CapacitorPlugin, TMappers : Mappers> : + com.getcapacitor.Plugin() { private val plugin: com.ionic.plugin.core.Plugin private val wrapperDelegate = WrapperDelegateImpl(this) private var activityResultObserver_ = ActivityResultObserver() @@ -29,14 +33,34 @@ abstract class CapacitorPlugin, TMapp } protected fun call(action: TActionKey, call: PluginCall) { - plugin.call(action, CallContext(call, wrapperDelegate, plugin.mappers)) + plugin.call(action, call.toContext()) + } + + protected fun callAction( + actionCreator: (call: CallContext) -> BaseAction, + call: PluginCall + ) { + val context = call.toContext() + val baseAction = actionCreator(context) + plugin.callAction(baseAction, context) + } + + private fun PluginCall.toContext(): CallContext { + return CallContext(this, wrapperDelegate, plugin.mappers) } override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { activityResultObserver_.onActivityResult(requestCode, resultCode, data) } - class WrapperDelegateImpl, TMappers: Mappers>(private val wrapper: CapacitorPlugin) : + @PluginMethod + fun setLogLevels(call: PluginCall) { + callAction({ SetLogLevelsAction(it.asObject()) }, call) + } + + class WrapperDelegateImpl, TMappers : Mappers>( + private val wrapper: CapacitorPlugin + ) : WrapperDelegate { override val activity: Activity get() = wrapper.activity diff --git a/package.json b/package.json index 1cf69bc..90f5221 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.7.0-alpha.0", + "version": "5.8.0-alpha.9", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", @@ -26,7 +26,7 @@ "compile": "tsc --noEmit" }, "dependencies": { - "@spryrocks/logger-plugin": "^0.1.15-alpha.1" + "@spryrocks/logger-plugin": "^0.1.17-alpha.2" }, "peerDependencies": { "@capacitor/core": "^5.4.0" diff --git a/src/definitions.ts b/src/definitions.ts index e03c11e..4f2ad8a 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -1,8 +1,16 @@ type IAction = {options: TOptions; result: TResult}; +type SetLogLevelsOptions = { + logLevels: string[] | undefined; +}; + +export type DefaultActions = { + setLogLevels: IAction; +}; + export type IDefinitions = { [name: string]: IAction; -}; +} & DefaultActions; export type CallbackId = string; type PluginCallback = ( diff --git a/src/mappers.ts b/src/mappers.ts index 9c5bddf..87e3210 100644 --- a/src/mappers.ts +++ b/src/mappers.ts @@ -1,3 +1,4 @@ +import {LogLevel} from '@spryrocks/logger-plugin'; import {PluginError} from './error'; type RawError = {message: unknown; code: unknown; data: unknown}; @@ -55,4 +56,9 @@ export abstract class Mappers return undefined; } } + + mapLogLevels(logLevels: LogLevel[] | undefined): string[] | undefined { + if (!logLevels) return undefined; + return logLevels.map((l) => l as string); + } } diff --git a/src/plugin.ts b/src/plugin.ts index 747edb2..52d7e43 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -9,6 +9,7 @@ import { createLoggerFactory, GlobalData, ILoggerObserver, + LogData, LoggerObserver, prepareLogData, } from './logger'; @@ -32,6 +33,7 @@ type LogEvent = { export interface ICapacitorPlugin { get logObserver(): ILoggerObserver; + setLogLevels(logLevels: LogLevel[] | undefined): Promise; } export type PluginOptions = { @@ -49,13 +51,15 @@ export abstract class CapacitorPlugin< private readonly _logObserver = new LoggerObserver(); - private readonly _logNotifiers = new MultipleNotifiers([ - this._logObserver, - CapacitorPlugin._logObserver, - ]); + private readonly _logNotifiers = new MultipleNotifiers({ + notifiers: [this._logObserver, CapacitorPlugin._logObserver], + filter: this.testLog.bind(this), + }); private readonly _loggerFactory: ILoggerFactory; + private logLevels: LogLevel[] | undefined; + protected abstract readonly mappers: TMappers; // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected @@ -136,6 +140,7 @@ export abstract class CapacitorPlugin< ) { try { this.proxy.addListener(name, (event) => { + // eslint-disable-next-line no-console console.log('CapacitorPlugin', 'event received', name, event); listener(event as TListener); }); @@ -208,4 +213,17 @@ export abstract class CapacitorPlugin< public get proxy() { return this.options.proxy; } + + public async setLogLevels(logLevels: LogLevel[] | undefined) { + this.logLevels = logLevels; + await this.call('setLogLevels', { + logLevels: this.mappers.mapLogLevels(logLevels), + }).catch(() => {}); + } + + private testLog(data: LogData) { + const logLevels = this.logLevels; + if (!logLevels) return true; + return logLevels.includes(data.level); + } } diff --git a/yarn.lock b/yarn.lock index 049c8ca..5df9be6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -129,47 +129,47 @@ eslint-plugin-import "^2.28.1" eslint-plugin-prettier "^5.0.0" -"@spryrocks/logger-core@^0.1.15-alpha.0": - version "0.1.15-alpha.0" - resolved "https://registry.yarnpkg.com/@spryrocks/logger-core/-/logger-core-0.1.15-alpha.0.tgz#c8c8f8f802ace92683e930b2918587bd30bb61ba" - integrity sha512-B+MfnTVMg8XsShJOLnXhJvmSc5/26tHRDyUvzfuw4MsAS4ME8vZuU/+tcyG/m+H8PbRDv5CE9Qmg0dwUFlk4EQ== +"@spryrocks/logger-core@^0.1.17-alpha.0": + version "0.1.17-alpha.0" + resolved "https://registry.yarnpkg.com/@spryrocks/logger-core/-/logger-core-0.1.17-alpha.0.tgz#574b1231b633586bdd829f6bb0b5880b2a317167" + integrity sha512-m2vY5EhOv8rz8vyIh0agioKm5W/ETMvq0gmAEM+Jg2V8ciK+UTyT7dAkXnZelscuaB+tLDWqS/J2cnaNhQc2lA== -"@spryrocks/logger-observer@^0.1.15-alpha.0": - version "0.1.15-alpha.0" - resolved "https://registry.yarnpkg.com/@spryrocks/logger-observer/-/logger-observer-0.1.15-alpha.0.tgz#9b406ef3e187a728fa867ffb770c39f32316c3d8" - integrity sha512-fz6Ls4KZRL2ZasrnTY8223R7KIjZ7pyy8W9HduwdY1jprgg13+qvGe52GFHDionA6pYeaYc8J1/rZ5wjYrvvUA== +"@spryrocks/logger-observer@^0.1.17-alpha.2": + version "0.1.17-alpha.2" + resolved "https://registry.yarnpkg.com/@spryrocks/logger-observer/-/logger-observer-0.1.17-alpha.2.tgz#7089773ffb422a2e95d1a5edd6ce1e1ab1eb3a01" + integrity sha512-NlrVcCTOhTXaheye3U6dyhlqoN3SkZVQycBZJ5r4oqiL3ziiTAjTJ/DRS4PiwixE1yugaUfKZW02o5cQ41RqNQ== dependencies: - "@spryrocks/logger-core" "^0.1.15-alpha.0" + "@spryrocks/logger-core" "^0.1.17-alpha.0" -"@spryrocks/logger-plugin-core@^0.1.15-alpha.0": - version "0.1.15-alpha.0" - resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin-core/-/logger-plugin-core-0.1.15-alpha.0.tgz#e42f202dd57bb4d1627bfed514da8d6aa69244d5" - integrity sha512-iRmip/fa+V/IWxr2H3yHcg4YJmoRUOgqUv0Qvol7dcDK21pF9NasFJVJrMRXoGqhv3S6CI/AHRIhNx1idQ9lSw== +"@spryrocks/logger-plugin-core@^0.1.17-alpha.0": + version "0.1.17-alpha.0" + resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin-core/-/logger-plugin-core-0.1.17-alpha.0.tgz#84fb20f978a00219f732c7615109a01ee98522e8" + integrity sha512-bgQcfWn+eYpff1Gsof/Pm8aQchZhBhm4ctKh9c4otkwpOVvW6ob3guyUBkV/xAeEzdPdeYTtqSBiOB2xzvVRsw== dependencies: - "@spryrocks/logger-core" "^0.1.15-alpha.0" + "@spryrocks/logger-core" "^0.1.17-alpha.0" -"@spryrocks/logger-plugin-observer@^0.1.15-alpha.0": - version "0.1.15-alpha.0" - resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin-observer/-/logger-plugin-observer-0.1.15-alpha.0.tgz#06ffa53a7f2b2660cae018970abdd87f8dcd395c" - integrity sha512-H5qaNFbSWHL8Lpkf57wGisYlpQ+Wj548vG/Evb47xK9xASVlqctc5PLmCQu1iCoonmzsxMPUv80CX/KcrMciKw== +"@spryrocks/logger-plugin-observer@^0.1.17-alpha.2": + version "0.1.17-alpha.2" + resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin-observer/-/logger-plugin-observer-0.1.17-alpha.2.tgz#64d2f59a449e988a4fe70ef402ed458c2675e0c8" + integrity sha512-veZXAEQy0txmMCMOAr0nB4E3fN1Qa9waGcM5VUW9Rzq2/3NLQXPGCu1DTjVRSktbCbf7BgQOlqHWXlBRrd9qCw== dependencies: - "@spryrocks/logger-observer" "^0.1.15-alpha.0" - "@spryrocks/logger-plugin-core" "^0.1.15-alpha.0" + "@spryrocks/logger-observer" "^0.1.17-alpha.2" + "@spryrocks/logger-plugin-core" "^0.1.17-alpha.0" -"@spryrocks/logger-plugin@^0.1.15-alpha.1": - version "0.1.15-alpha.1" - resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin/-/logger-plugin-0.1.15-alpha.1.tgz#dd03b5b85642e24b6f85ed55e0b6b984ab384a39" - integrity sha512-hUzBeJel8zLmN4q7wldq7Wlj804ljpA98juop53EhyYn99c756CAIfdim7nSyXMBlGGltb7wf8p+S413QYkSQg== +"@spryrocks/logger-plugin@^0.1.17-alpha.2": + version "0.1.17-alpha.2" + resolved "https://registry.yarnpkg.com/@spryrocks/logger-plugin/-/logger-plugin-0.1.17-alpha.2.tgz#12662d43e9f2ffb5d0ad87e351408bdfbed997e7" + integrity sha512-meq63XwkpMv9LdIsoOxIby1bi8BciSd3dAo60sD0dLB9FeB7GDP38FAYsSi09sN7+FxZq5uqn8Ug7TYk+Fdxdg== dependencies: - "@spryrocks/logger" "^0.1.15-alpha.1" - "@spryrocks/logger-plugin-observer" "^0.1.15-alpha.0" + "@spryrocks/logger" "^0.1.17-alpha.2" + "@spryrocks/logger-plugin-observer" "^0.1.17-alpha.2" -"@spryrocks/logger@^0.1.15-alpha.1": - version "0.1.15-alpha.1" - resolved "https://registry.yarnpkg.com/@spryrocks/logger/-/logger-0.1.15-alpha.1.tgz#66e80ee4283fcf96aca7285cb23ac26881bf739d" - integrity sha512-2iWn8cX3P7veezkkaro/IDUKYf/q3KRp/nBdnkpcR8y+VXxLsNRhptq2BY1LVXJhpoXbEE1kRbTy/5dJ0Uq7jA== +"@spryrocks/logger@^0.1.17-alpha.2": + version "0.1.17-alpha.2" + resolved "https://registry.yarnpkg.com/@spryrocks/logger/-/logger-0.1.17-alpha.2.tgz#c02ecf89eb57f1f7680852110055181f5cebd32b" + integrity sha512-HRVz+JDqAQ0N8D9rp93tV3gGyhwN6ZdI2vBWKftgXPCd0cle/GkSL3Wjj8P3F6kNcky2/xv0dyROGWIcLQoxSQ== dependencies: - "@spryrocks/logger-observer" "^0.1.15-alpha.0" + "@spryrocks/logger-observer" "^0.1.17-alpha.2" "@spryrocks/prettier-config@^0.3.0-alpha.0": version "0.3.0-alpha.0" From dc99e6e6e8dbd64195e7a30afdab8acb0320ce2f Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Fri, 1 Mar 2024 14:08:17 +0100 Subject: [PATCH 4/6] Remove threading helpers --- ios/src/helpers/ThreadingHelpers.swift | 9 --------- package.json | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 ios/src/helpers/ThreadingHelpers.swift diff --git a/ios/src/helpers/ThreadingHelpers.swift b/ios/src/helpers/ThreadingHelpers.swift deleted file mode 100644 index b48eda0..0000000 --- a/ios/src/helpers/ThreadingHelpers.swift +++ /dev/null @@ -1,9 +0,0 @@ -public func runInMainThreadAsync(_ action: @escaping () async throws -> T) async throws -> T { - return try await withCheckedThrowingContinuation { c in - DispatchQueue.main.async { - Task { - c.resume(returning: try await action()) - } - } - } -} diff --git a/package.json b/package.json index 90f5221..49b9b63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.8.0-alpha.9", + "version": "5.8.1-alpha.0", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", From fece42bc21289f09272df306995309f72bd21944 Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Fri, 1 Mar 2024 19:24:20 +0100 Subject: [PATCH 5/6] Refactoring --- ios/src/helpers/ContinuationHelpers.swift | 33 +++++++++++++++++++++++ package.json | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 ios/src/helpers/ContinuationHelpers.swift diff --git a/ios/src/helpers/ContinuationHelpers.swift b/ios/src/helpers/ContinuationHelpers.swift new file mode 100644 index 0000000..2f67f5e --- /dev/null +++ b/ios/src/helpers/ContinuationHelpers.swift @@ -0,0 +1,33 @@ +@inlinable public func withCheckedThrowingContinuationSafe(_ body: (CheckedContinuationSafe) -> Void) async throws -> T { + return try await withCheckedThrowingContinuation { c in + let continuation = CheckedContinuationSafe(continuation: c) + body(continuation) + } +} + +public final class CheckedContinuationSafe : Sendable where E : Error { + private let continuation: CheckedContinuation + private var isResumed = false + + public init(continuation: CheckedContinuation) { + self.continuation = continuation + } + + public func resume(returning value: T) { + if (isResumed) { return } + self.isResumed = true + continuation.resume(returning: value) + } + + public func resume(throwing error: E) { + if (isResumed) { return } + self.isResumed = true + continuation.resume(throwing: error) + } + + public func resume() where T == () { + if (isResumed) { return } + self.isResumed = true + continuation.resume() + } +} diff --git a/package.json b/package.json index 49b9b63..f50b3b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.8.1-alpha.0", + "version": "5.8.1-alpha.1", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", From 8483ba03a7da359423d05dd728a1aff2fa128379 Mon Sep 17 00:00:00 2001 From: "maksim.zhemerenko" Date: Wed, 6 Mar 2024 23:50:45 +0100 Subject: [PATCH 6/6] Refactoring --- package.json | 2 +- src/plugin.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f50b3b1..2fb656a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "5.8.1-alpha.1", + "version": "5.8.2-alpha.0", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", diff --git a/src/plugin.ts b/src/plugin.ts index 52d7e43..3616a67 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -20,6 +20,7 @@ import { LogParams, MultipleNotifiers, } from '@spryrocks/logger-plugin'; +import {Capacitor} from '@capacitor/core'; import {Mappers} from './mappers'; import {PluginError} from './error'; @@ -152,7 +153,7 @@ export abstract class CapacitorPlugin< protected isEventsEnabled() { // if (!this.isPluginAvailableInCapacitor()) return false; - // if (this.isWeb()) return false; + if (this.isWeb()) return false; return true; // remove this method after implementation on web } @@ -192,9 +193,9 @@ export abstract class CapacitorPlugin< // return Capacitor.isPluginAvailable(this.options.pluginName); // } - // private isWeb() { - // return Capacitor.getPlatform() !== 'web'; - // } + private isWeb() { + return Capacitor.getPlatform() === 'web'; + } private logPluginError(method: string | number | symbol, error: PluginError) { const logger = this.createLogger(undefined, method.toString());