Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
fix queue prioritization + fix requiresMainQueueSetup issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrower95 committed Jul 25, 2023
1 parent 67f783a commit 7bd0104
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 32 deletions.
4 changes: 4 additions & 0 deletions ios/react-native/MPCKeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class MPCKeyService: NSObject {

return false;
}

@objc static func requiresMainQueueSetup() -> Bool {
return true
}

/**
Initializes the MPCKeyService with the given parameters.
Expand Down
4 changes: 4 additions & 0 deletions ios/react-native/MPCSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class MPCSdk: NSObject {
return false
}

@objc static func requiresMainQueueSetup() -> Bool {
return true
}

/**
Initializes the MPCSdk with the given parameters.
Resolves with the string "success" on success; rejects with an error otherwise.
Expand Down
4 changes: 4 additions & 0 deletions ios/react-native/MPCWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class MPCWalletService: NSObject {
reject(walletsErr, error.localizedDescription, nil)
}
}

@objc static func requiresMainQueueSetup() -> Bool {
return true
}

/**
Creates an MPCWallet with the given parameters. Resolves with the response on success; rejects with an error
Expand Down
4 changes: 4 additions & 0 deletions ios/react-native/PoolService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class PoolService: NSObject {
reject(poolsErr, error.localizedDescription, nil)
}
}

@objc static func requiresMainQueueSetup() -> Bool {
return true
}

/**
Creates a Pool with the given parameters. Resolves with the created Pool object on success; rejects with an error
Expand Down
26 changes: 26 additions & 0 deletions ios/swift/Job.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation

/**
This class manages different .qos options for the dispatch queues Waas uses, to make it easier to
support user-configurable QOS in the future.
*/
class Job {
/**
Return a dispatch queue for running on a background thread.
*/
class func background() -> DispatchQueue {
return DispatchQueue.global(qos: .background)
}

class func backgroundHighPri() -> DispatchQueue {
return DispatchQueue.global(qos: .userInteractive)
}

/**
Return a dispatch queue for running on the main/UI thread.
*/
class func main() -> DispatchQueue {
DispatchQueue.main
}
}

36 changes: 18 additions & 18 deletions ios/swift/WaasSdk/MPCKeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MPCKeyService: NSObject {
*/
public func registerDevice() -> Future<V1Device, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let device = try self.keyClient?.registerDevice()
promise(Result.success(device!))
Expand All @@ -60,7 +60,7 @@ public class MPCKeyService: NSObject {
*/
public func pollForPendingDeviceGroup(_ deviceGroup: NSString, pollInterval: NSNumber) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pendingDeviceGroupData = try self.keyClient?.pollPendingDeviceGroup(
deviceGroup as String,
Expand All @@ -82,7 +82,7 @@ public class MPCKeyService: NSObject {
*/
public func stopPollingForPendingDeviceGroup() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.stopPollingPendingDeviceBackups(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -94,7 +94,7 @@ public class MPCKeyService: NSObject {
*/
public func createSignatureFromTx(_ parent: NSString, transaction: NSDictionary) -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let serializedTx = try JSONSerialization.data(withJSONObject: transaction)
self.keyClient?.createTxSignature(parent as String, tx: serializedTx, receiver: goReturnsString(promise: promise, wrapAsError: self.wrapError))
Expand All @@ -113,7 +113,7 @@ public class MPCKeyService: NSObject {
*/
public func pollForPendingSignatures(_ deviceGroup: NSString, pollInterval: NSNumber) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pendingSignaturesData = try self.keyClient?.pollPendingSignatures(
deviceGroup as String,
Expand All @@ -135,7 +135,7 @@ public class MPCKeyService: NSObject {
*/
public func stopPollingForPendingSignatures() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.stopPollingPendingSignatures(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -147,7 +147,7 @@ public class MPCKeyService: NSObject {
*/
public func waitPendingSignature(_ operation: NSString) -> Future<V1Signature, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
var signature: V1Signature?
do {
signature = try self.keyClient?.waitPendingSignature(operation as String)
Expand All @@ -164,7 +164,7 @@ public class MPCKeyService: NSObject {
*/
public func getSignedTransaction(_ transaction: NSDictionary, signature: NSDictionary) -> Future<V1SignedTransaction, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let serializedTx = try JSONSerialization.data(withJSONObject: transaction)
let goSignature = V1Signature()
Expand All @@ -188,7 +188,7 @@ public class MPCKeyService: NSObject {
*/
public func getDeviceGroup(_ name: NSString) -> Future<V1DeviceGroup, WaasError>{
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let deviceGroupRes = try self.keyClient?.getDeviceGroup(name as String)
promise(Result.success(deviceGroupRes!))
Expand All @@ -205,7 +205,7 @@ public class MPCKeyService: NSObject {
*/
public func prepareDeviceArchive(_ deviceGroup: NSString, device: NSString) -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.prepareDeviceArchive(
deviceGroup as String, device: device as String, receiver: goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
Expand All @@ -220,7 +220,7 @@ public class MPCKeyService: NSObject {
*/
public func pollForPendingDeviceArchives(_ deviceGroup: NSString, pollInterval: NSNumber) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pendingDeviceArchiveData = try self.keyClient?.pollPendingDeviceArchives(
deviceGroup as String,
Expand All @@ -241,7 +241,7 @@ public class MPCKeyService: NSObject {
*/
public func stopPollingForPendingDeviceArchives() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.stopPollingPendingDeviceArchives(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -253,7 +253,7 @@ public class MPCKeyService: NSObject {
*/
public func prepareDeviceBackup(_ deviceGroup: NSString, device: NSString) -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.prepareDeviceBackup(
deviceGroup as String, device: device as String, receiver: goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
Expand All @@ -268,7 +268,7 @@ public class MPCKeyService: NSObject {
*/
public func pollForPendingDeviceBackups(_ deviceGroup: NSString, pollInterval: NSNumber) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pendingDeviceBackupData = try self.keyClient?.pollPendingDeviceBackups(
deviceGroup as String,
Expand All @@ -291,7 +291,7 @@ public class MPCKeyService: NSObject {
*/
public func stopPollingForPendingDeviceBackups() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.stopPollingPendingDeviceBackups(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -303,7 +303,7 @@ public class MPCKeyService: NSObject {
*/
public func addDevice(_ deviceGroup: NSString, device: NSString) -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.addDevice(
deviceGroup as String, device: device as String, receiver: goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
Expand All @@ -318,7 +318,7 @@ public class MPCKeyService: NSObject {
*/
public func pollForPendingDevices(_ deviceGroup: NSString, pollInterval: NSNumber) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pendingDeviceData = try self.keyClient?.pollPendingDevices(
deviceGroup as String,
Expand All @@ -341,7 +341,7 @@ public class MPCKeyService: NSObject {
*/
public func stopPollingForPendingDevices() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
self.keyClient?.stopPollingPendingDevices(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand Down
18 changes: 9 additions & 9 deletions ios/swift/WaasSdk/MPCSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MPCSdk: NSObject {
*/
public func bootstrapDevice(_ passcode: NSString) -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.main().async(execute: {
self.sdk.bootstrapDevice(passcode as String, receiver: goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -58,7 +58,7 @@ public class MPCSdk: NSObject {
*/
public func resetPasscode(_ newPasscode: NSString) -> Future<Void, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.main().async(execute: {
do {
try self.sdk.resetPasscode(newPasscode as String)
promise(Result.success(()))
Expand All @@ -75,7 +75,7 @@ public class MPCSdk: NSObject {
*/
public func getRegistrationData() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.main().async(execute: {
self.sdk.getRegistrationData(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand All @@ -88,7 +88,7 @@ public class MPCSdk: NSObject {
*/
public func computeMPCOperation(_ mpcData: NSString) -> Future<Void, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
try self.sdk.computeMPCOperation(mpcData as String)
promise(Result.success(()))
Expand All @@ -105,7 +105,7 @@ public class MPCSdk: NSObject {
*/
public func computePrepareDeviceArchiveMPCOperation(_ mpcData: NSString, passcode: NSString) -> Future<Void, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
try self.sdk.computePrepareDeviceArchiveMPCOperation(mpcData as String, passcode: passcode as String)
promise(Result.success(()))
Expand All @@ -122,7 +122,7 @@ public class MPCSdk: NSObject {
*/
public func computePrepareDeviceBackupMPCOperation(_ mpcData: NSString, passcode: NSString) -> Future<Void, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
try self.sdk.computePrepareDeviceBackupMPCOperation(mpcData as String, passcode: passcode as String)
promise(Result.success(()))
Expand All @@ -139,7 +139,7 @@ public class MPCSdk: NSObject {
*/
public func computeAddDeviceMPCOperation(_ mpcData: NSString, passcode: NSString, deviceBackup: NSString) -> Future<Void, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
try self.sdk.computeAddDeviceMPCOperation(
mpcData as String,
Expand All @@ -161,7 +161,7 @@ public class MPCSdk: NSObject {
*/
public func exportPrivateKeys(_ mpcKeyExportMetadata: NSString, passcode: NSString) -> Future<NSArray, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
let response = try self.sdk.exportPrivateKeys(
mpcKeyExportMetadata as String,
Expand All @@ -183,7 +183,7 @@ public class MPCSdk: NSObject {
*/
public func exportDeviceBackup() -> Future<String, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
self.sdk.exportDeviceBackup(goReturnsString(promise: promise, wrapAsError: self.wrapError))
})
}
Expand Down
8 changes: 4 additions & 4 deletions ios/swift/WaasSdk/MPCWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MPCWalletService: NSObject {
*/
public func createMPCWallet(parent: NSString, device: NSString) -> Future<V1CreateMPCWalletResponse, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
let response = try self.walletsClient.createMPCWallet(parent as String, device: device as String)
promise(Result.success(response))
Expand All @@ -58,7 +58,7 @@ public class MPCWalletService: NSObject {
*/
public func waitPendingMPCWallet(operation: NSString) -> Future<V1MPCWallet, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let mpcWallet = try self.walletsClient.waitPendingMPCWallet(operation as String)
promise(Result.success(mpcWallet))
Expand All @@ -75,7 +75,7 @@ public class MPCWalletService: NSObject {
*/
public func generateAddress(_ mpcWallet: NSString, network: NSString) -> Future<NSDictionary, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.backgroundHighPri().async(execute: {
do {
let addressData = try self.walletsClient.generateAddress(mpcWallet as String, network: network as String)
let res = try JSONSerialization.jsonObject(with: addressData) as? NSDictionary
Expand All @@ -92,7 +92,7 @@ public class MPCWalletService: NSObject {
*/
public func getAddress(name: NSString) -> Future<NSDictionary, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let addressData = try self.walletsClient.getAddress(name as String)
let res = try JSONSerialization.jsonObject(with: addressData) as? NSDictionary
Expand Down
2 changes: 1 addition & 1 deletion ios/swift/WaasSdk/PoolService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PoolService: NSObject {
*/
public func createPool(displayName: NSString, poolID: NSString) -> Future<V1Pool, WaasError> {
return Future() { promise in
DispatchQueue.main.async(execute: {
Job.background().async(execute: {
do {
let pool = try self.poolsClient.createPool(displayName as String, poolID: poolID as String)
promise(Result.success(pool))
Expand Down

0 comments on commit 7bd0104

Please sign in to comment.