Skip to content

Commit

Permalink
Update DispatchGroup to DispatchSemaphore in XMTPModule.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
kele-leanes committed Jan 3, 2024
1 parent d5f2557 commit 8560716
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class XMTPModule: Module {
let clientsManager = ClientsManager()
let conversationsManager = IsolatedManager<Conversation>()
let subscriptionsManager = IsolatedManager<Task<Void, Never>>()
var preEnableIdentityCallbackDeferred: DispatchGroup?
var preCreateIdentityCallbackDeferred: DispatchGroup?
private var preEnableIdentityCallbackDeferred: DispatchSemaphore?
private var preCreateIdentityCallbackDeferred: DispatchSemaphore?

actor ClientsManager {
private var clients: [String: XMTP.Client] = [:]
Expand Down Expand Up @@ -70,10 +70,10 @@ public class XMTPModule: Module {
let signer = ReactNativeSigner(module: self, address: address)
self.signer = signer
if(hasCreateIdentityCallback ?? false) {
preCreateIdentityCallbackDeferred = DispatchGroup()
preCreateIdentityCallbackDeferred = DispatchSemaphore(value: 0)
}
if(hasEnableIdentityCallback ?? false) {
preEnableIdentityCallbackDeferred = DispatchGroup()
preEnableIdentityCallbackDeferred = DispatchSemaphore(value: 0)
}
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil
Expand All @@ -91,10 +91,10 @@ public class XMTPModule: Module {
AsyncFunction("createRandom") { (environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) -> String in
let privateKey = try PrivateKey.generate()
if(hasCreateIdentityCallback ?? false) {
preCreateIdentityCallbackDeferred = DispatchGroup()
preCreateIdentityCallbackDeferred = DispatchSemaphore(value: 0)
}
if(hasEnableIdentityCallback ?? false) {
preEnableIdentityCallbackDeferred = DispatchGroup()
preEnableIdentityCallbackDeferred = DispatchSemaphore(value: 0)
}
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil
Expand Down Expand Up @@ -550,11 +550,15 @@ public class XMTPModule: Module {
}

Function("preEnableIdentityCallbackCompleted") {
preEnableIdentityCallbackDeferred?.leave()
DispatchQueue.global().async {
self.preEnableIdentityCallbackDeferred?.signal()
}
}

Function("preCreateIdentityCallbackCompleted") {
preCreateIdentityCallbackDeferred?.leave()
DispatchQueue.global().async {
self.preCreateIdentityCallbackDeferred?.signal()
}
}
}

Expand Down Expand Up @@ -695,15 +699,13 @@ public class XMTPModule: Module {

func preEnableIdentityCallback() {
sendEvent("preEnableIdentityCallback")
preEnableIdentityCallbackDeferred?.enter()
preEnableIdentityCallbackDeferred?.wait()
preCreateIdentityCallbackDeferred = nil
self.preEnableIdentityCallbackDeferred?.wait()
self.preCreateIdentityCallbackDeferred = nil
}

func preCreateIdentityCallback() {
sendEvent("preCreateIdentityCallback")
preCreateIdentityCallbackDeferred?.enter()
preCreateIdentityCallbackDeferred?.wait()
preEnableIdentityCallbackDeferred = nil
self.preCreateIdentityCallbackDeferred?.wait()
self.preEnableIdentityCallbackDeferred = nil
}
}

0 comments on commit 8560716

Please sign in to comment.