Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Homayoun authored and Homayoun committed Aug 12, 2023
1 parent 787dc81 commit 2bb5bbb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
4 changes: 2 additions & 2 deletions ios/Fula.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 24 additions & 13 deletions ios/Fula.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Foundation.NSDate // for TimeInterval
import CommonCrypto
import Wnfs
import Fula
Expand Down Expand Up @@ -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:)
Expand Down Expand Up @@ -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)
Expand All @@ -901,6 +903,15 @@ class FulaModule: NSObject {

}

func shutdownInternal() throws {
if(fula != nil) {
try fula?.shutdown()
fula = nil
client = nil
wnfs = nil
}
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
Expand Down
43 changes: 43 additions & 0 deletions ios/UserDataHelper.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
import Foundation
import CommonCrypto
import Foundation.NSDate // for TimeInterval

struct TimedOutError: Error, Equatable {}

public func withTimeout<R>(
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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2bb5bbb

Please sign in to comment.