FieldRuntimeService.swift (3365B)
1 import Foundation 2 3 public final class FieldRuntimeService: @unchecked Sendable { 4 private let runtime: RadrootsRuntime 5 private let queue = DispatchQueue(label: "org.radroots.field_ios.runtime", qos: .userInitiated) 6 7 public init(runtime: RadrootsRuntime) { 8 self.runtime = runtime 9 } 10 11 func run<T>(_ work: @escaping @Sendable (RadrootsRuntime) throws -> T) async throws -> T { 12 try await withCheckedThrowingContinuation { continuation in 13 queue.async { 14 do { 15 continuation.resume(returning: try work(self.runtime)) 16 } catch { 17 continuation.resume(throwing: error) 18 } 19 } 20 } 21 } 22 23 func runValue<T>(_ work: @escaping @Sendable (RadrootsRuntime) -> T) async -> T { 24 await withCheckedContinuation { continuation in 25 queue.async { 26 continuation.resume(returning: work(self.runtime)) 27 } 28 } 29 } 30 31 public func infoJson() async -> String { 32 await runValue { $0.infoJson() } 33 } 34 35 public func nostrSetDefaultRelays(_ relays: [String]) async throws { 36 try await run { try $0.nostrSetDefaultRelays(relays: relays) } 37 } 38 39 public func nostrConnectIfKeyPresent() async throws { 40 try await run { try $0.nostrConnectIfKeyPresent() } 41 } 42 43 public func nostrConnectionStatus() async -> NostrConnectionStatus { 44 await runValue { $0.nostrConnectionStatus() } 45 } 46 47 public func nostrIdentitySnapshot() async throws -> NostrIdentitySnapshot { 48 try await run { try $0.nostrIdentitySnapshot() } 49 } 50 51 public func nostrIdentityList() async throws -> [NostrIdentityRecord] { 52 try await run { try $0.nostrIdentityList() } 53 } 54 55 public func nostrIdentityValidateHostCustodySecret(secretKey: String) async throws -> NostrHostCustodyIdentity { 56 try await run { try $0.nostrIdentityValidateHostCustodySecret(secretKey: secretKey) } 57 } 58 59 public func nostrIdentityRestoreHostCustodySecret( 60 secretKey: String, 61 label: String?, 62 makeSelected: Bool 63 ) async throws -> NostrIdentityRecord { 64 try await run { 65 try $0.nostrIdentityRestoreHostCustodySecret( 66 secretKey: secretKey, 67 label: label, 68 makeSelected: makeSelected 69 ) 70 } 71 } 72 73 public func nostrIdentityRemove(identityId: String) async throws { 74 try await run { try $0.nostrIdentityRemove(identityId: identityId) } 75 } 76 77 public func nostrIdentityLockHostCustodyRuntime() async throws { 78 try await run { try $0.nostrIdentityLockHostCustodyRuntime() } 79 } 80 81 public func nostrIdentityResetHostCustodyRuntime() async throws { 82 try await run { try $0.nostrIdentityResetHostCustodyRuntime() } 83 } 84 85 public func nostrProfileForSelf() async throws -> NostrProfileEventMetadata? { 86 try await run { try $0.nostrProfileForSelf() } 87 } 88 89 public func nostrFetchTextNotes( 90 limit: UInt16, 91 sinceUnix: UInt64? 92 ) async throws -> [NostrPostEventMetadata] { 93 try await run { try $0.nostrFetchTextNotes(limit: limit, sinceUnix: sinceUnix) } 94 } 95 96 public func nostrNextPostStreamEvent() async throws -> NostrPostEventMetadata? { 97 try await run { try $0.nostrNextPostEvent() } 98 } 99 }