Skip to content

Commit

Permalink
ios: wait for pre-event callback completion
Browse files Browse the repository at this point in the history
  • Loading branch information
kele-leanes committed Dec 27, 2023
1 parent 08649d7 commit d5f2557
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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?

actor ClientsManager {
private var clients: [String: XMTP.Client] = [:]
Expand Down Expand Up @@ -67,6 +69,12 @@ public class XMTPModule: Module {
AsyncFunction("auth") { (address: String, environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) in
let signer = ReactNativeSigner(module: self, address: address)
self.signer = signer
if(hasCreateIdentityCallback ?? false) {
preCreateIdentityCallbackDeferred = DispatchGroup()
}
if(hasEnableIdentityCallback ?? false) {
preEnableIdentityCallbackDeferred = DispatchGroup()
}
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil
let options = createClientConfig(env: environment, appVersion: appVersion, preEnableIdentityCallback: preEnableIdentityCallback, preCreateIdentityCallback: preCreateIdentityCallback)
Expand All @@ -82,6 +90,12 @@ public class XMTPModule: Module {
// Generate a random wallet and set the client to that
AsyncFunction("createRandom") { (environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) -> String in
let privateKey = try PrivateKey.generate()
if(hasCreateIdentityCallback ?? false) {
preCreateIdentityCallbackDeferred = DispatchGroup()
}
if(hasEnableIdentityCallback ?? false) {
preEnableIdentityCallbackDeferred = DispatchGroup()
}
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil

Expand Down Expand Up @@ -534,6 +548,14 @@ public class XMTPModule: Module {
try ConsentWrapper.encode(entry.value)
}
}

Function("preEnableIdentityCallbackCompleted") {
preEnableIdentityCallbackDeferred?.leave()
}

Function("preCreateIdentityCallbackCompleted") {
preCreateIdentityCallbackDeferred?.leave()
}
}

//
Expand Down Expand Up @@ -673,9 +695,15 @@ public class XMTPModule: Module {

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

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

0 comments on commit d5f2557

Please sign in to comment.