From 2bb5bbb4c796b523340de735cee7bd92d16c49cb Mon Sep 17 00:00:00 2001 From: Homayoun Date: Sat, 12 Aug 2023 23:36:09 +0330 Subject: [PATCH] some fixes --- ios/Fula.mm | 4 ++-- ios/Fula.swift | 37 ++++++++++++++++++++++------------ ios/UserDataHelper.swift | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/ios/Fula.mm b/ios/Fula.mm index fd636ab..d257e61 100644 --- a/ios/Fula.mm +++ b/ios/Fula.mm @@ -2,7 +2,8 @@ @interface RCT_EXTERN_MODULE(FulaModule, NSObject) -RCT_EXTERN_METHOD(checkConnection:(RCTPromiseResolveBlock)resolve +RCT_EXTERN_METHOD(checkConnection: (NSNumber *) timeout + withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(newClient:(NSString *)IdentityString @@ -44,7 +45,6 @@ @interface RCT_EXTERN_MODULE(FulaModule, NSObject) withRejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(writeFile:(NSString *)fulaTargetFilename - withFulaTargetFilename:(NSString *) fulaTargetFilename withLocalFilename: (NSString *) localFilename withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) diff --git a/ios/Fula.swift b/ios/Fula.swift index bc7aa76..24fea57 100644 --- a/ios/Fula.swift +++ b/ios/Fula.swift @@ -1,4 +1,5 @@ import Foundation +import Foundation.NSDate // for TimeInterval import CommonCrypto import Wnfs import Fula @@ -112,22 +113,27 @@ class FulaModule: NSObject { return convertIntToByte(keyInt) } - @objc(checkConnection:withRejecter:) - func checkConnection(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { - + @objc(checkConnection:withResolver:withRejecter:) + func checkConnection(timeout: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) { print("ReactNative", "checkConnection started") - if (fula != nil) { do { - try checkConnectionInternal() - resolve(true) + // FIXME: run with timeout + // Task { + // try await withTimeout(seconds: TimeInterval(timeout)) { [weak self] in + // guard let weakSelf = self else { return false } + // try weakSelf.checkConnectionInternal() + // callback(BOOL(true)) + // } + // } + resolve(try self.checkConnectionInternal()) } catch let error { print("ReactNative", "checkConnection failed with Error: ", error.localizedDescription) + // callback(BOOL(false)) resolve(false) } } - } @objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:) @@ -886,13 +892,9 @@ class FulaModule: NSObject { } @objc(shutdown:withRejecter:) - func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { + func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) { do { - if(fula != nil) { - try fula?.shutdown() - fula = nil - client = nil - } + try shutdownInternal() resolve(true) } catch let error { print("ReactNative", "shutdown", error.localizedDescription) @@ -901,6 +903,15 @@ class FulaModule: NSObject { } + func shutdownInternal() throws { + if(fula != nil) { + try fula?.shutdown() + fula = nil + client = nil + wnfs = nil + } + } + /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// diff --git a/ios/UserDataHelper.swift b/ios/UserDataHelper.swift index 0107968..08959cb 100644 --- a/ios/UserDataHelper.swift +++ b/ios/UserDataHelper.swift @@ -1,4 +1,39 @@ import Foundation +import CommonCrypto +import Foundation.NSDate // for TimeInterval + +struct TimedOutError: Error, Equatable {} + +public func withTimeout( + seconds: TimeInterval, + operation: @escaping @Sendable () async throws -> R +) async throws -> R { + return try await withThrowingTaskGroup(of: R.self) { group in + defer { + group.cancelAll() + } + + // Start actual work. + group.addTask { + let result = try await operation() + try Task.checkCancellation() + return result + } + // Start timeout child task. + group.addTask { + if seconds > 0 { + try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000)) + } + try Task.checkCancellation() + // We’ve reached the timeout. + throw TimedOutError() + } + // First finished child task wins, cancel the other task. + let result = try await group.next()! + return result + } +} + public class UserDataHelper: NSObject { var defaults: UserDefaults @@ -92,4 +127,12 @@ public extension Data { // Finally, decode. return Data(base64Encoded: self) } + + func sha256() -> Data { + var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) + self.withUnsafeBytes { + _ = CC_SHA256($0.baseAddress, CC_LONG(self.count), &hash) + } + return Data(hash) + } }