Skip to content

Commit

Permalink
add a bunch of awaits
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Sep 19, 2023
1 parent 01b5b06 commit aa18df5
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public class XMTPModule: Module {

Events("sign", "authed", "conversation", "message")

Function("address") { (clientAddress: String) -> String in
if let client = clients[clientAddress] {
AsyncFunction("address") { (clientAddress: String) -> String in
if let client = await clients[clientAddress] {
return client.address
} else {
return "No Client."
Expand All @@ -100,7 +100,7 @@ public class XMTPModule: Module {
let signer = ReactNativeSigner(module: self, address: address)
self.signer = signer
let options = createClientConfig(env: environment, appVersion: appVersion)
self.clients[address] = try await XMTP.Client.create(account: signer, options: options)
await self.clients[address] = try await XMTP.Client.create(account: signer, options: options)
self.signer = nil
sendEvent("authed")
}
Expand All @@ -115,7 +115,7 @@ public class XMTPModule: Module {
let options = createClientConfig(env: environment, appVersion: appVersion)
let client = try await Client.create(account: privateKey, options: options)

self.clients[client.address] = try await client
await self.clients[client.address] = client
return client.address
}

Expand All @@ -129,7 +129,7 @@ public class XMTPModule: Module {

let options = createClientConfig(env: environment, appVersion: appVersion)
let client = try await Client.from(bundle: bundle, options: options)
self.clients[client.address] = try await client
await self.clients[client.address] = client
return client.address
} catch {
print("ERRO! Failed to create client: \(error)")
Expand All @@ -139,7 +139,7 @@ public class XMTPModule: Module {

// Export the client's serialized key bundle.
AsyncFunction("exportKeyBundle") { (clientAddress: String) -> String in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}
let bundle = try client.privateKeyBundle.serializedData().base64EncodedString()
Expand All @@ -156,29 +156,29 @@ public class XMTPModule: Module {

// Import a conversation from its serialized topic data.
AsyncFunction("importConversationTopicData") { (clientAddress: String, topicData: String) -> String in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}
let data = try Xmtp_KeystoreApi_V1_TopicMap.TopicData(
serializedData: Data(base64Encoded: Data(topicData.utf8))!
)
let conversation = client.conversations.importTopicData(data: data)
conversations[conversation.cacheKey(clientAddress)] = try await conversation
await conversations[conversation.cacheKey(clientAddress)] = conversation
return try ConversationWrapper.encode(conversation, client: client)
}

//
// Client API
AsyncFunction("canMessage") { (clientAddress: String, peerAddress: String) -> Bool in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}

return try await client.canMessage(peerAddress)
}

AsyncFunction("encryptAttachment") { (clientAddress: String, fileJson: String) -> String in
if clients[clientAddress] == nil {
if await clients[clientAddress] == nil {
throw Error.noClient
}
let file = try DecryptedLocalAttachment.fromJson(fileJson)
Expand All @@ -204,7 +204,7 @@ public class XMTPModule: Module {
}

AsyncFunction("decryptAttachment") { (clientAddress: String, encryptedFileJson: String) -> String in
if clients[clientAddress] == nil {
if await clients[clientAddress] == nil {
throw Error.noClient
}
let encryptedFile = try EncryptedLocalAttachment.fromJson(encryptedFileJson)
Expand All @@ -229,14 +229,14 @@ public class XMTPModule: Module {
}

AsyncFunction("listConversations") { (clientAddress: String) -> [String] in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}

let conversations = try await client.conversations.list()

return try conversations.map { conversation in
self.conversations[conversation.cacheKey(clientAddress)] = try await conversation
await self.conversations[conversation.cacheKey(clientAddress)] = conversation

return try ConversationWrapper.encode(conversation, client: client)
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public class XMTPModule: Module {
}

AsyncFunction("loadBatchMessages") { (clientAddress: String, topics: [String]) -> [String] in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}

Expand Down Expand Up @@ -363,7 +363,7 @@ public class XMTPModule: Module {
}

AsyncFunction("sendPreparedMessage") { (clientAddress: String, preparedLocalMessageJson: String) -> String in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}
guard let local = try? PreparedLocalMessage.fromJson(preparedLocalMessageJson) else {
Expand All @@ -386,7 +386,7 @@ public class XMTPModule: Module {
}

AsyncFunction("createConversation") { (clientAddress: String, peerAddress: String, contextJson: String) -> String in
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}

Expand Down Expand Up @@ -417,12 +417,12 @@ public class XMTPModule: Module {
try await subscribeToMessages(clientAddress: clientAddress, topic: topic)
}

Function("unsubscribeFromConversations") {
subscriptions["conversations"]?.cancel()
AsyncFunction("unsubscribeFromConversations") {
await subscriptions["conversations"]?.cancel()
}

Function("unsubscribeFromAllMessages") {
subscriptions["messages"]?.cancel()
AsyncFunction("unsubscribeFromAllMessages") {
await subscriptions["messages"]?.cancel()
}

AsyncFunction("unsubscribeFromMessages") { (clientAddress: String, topic: String) in
Expand Down Expand Up @@ -494,28 +494,28 @@ public class XMTPModule: Module {
}

func findConversation(clientAddress: String, topic: String) async throws -> Conversation? {
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
throw Error.noClient
}

let cacheKey = Conversation.cacheKeyForTopic(clientAddress: clientAddress, topic: topic)
if let conversation = conversations[cacheKey] {
if let conversation = await conversations[cacheKey] {
return conversation
} else if let conversation = try await client.conversations.list().first(where: { $0.topic == topic }) {
conversations[cacheKey] = try await conversation
await conversations[cacheKey] = conversation
return conversation
}

return nil
}

func subscribeToConversations(clientAddress: String) {
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
return
}

subscriptions["conversations"]?.cancel()
subscriptions["conversations"] = Task {
await subscriptions["conversations"]?.cancel()
await subscriptions["conversations"] = Task {
do {
for try await conversation in client.conversations.stream() {
sendEvent("conversation", [
Expand All @@ -525,18 +525,18 @@ public class XMTPModule: Module {
}
} catch {
print("Error in conversations subscription: \(error)")
subscriptions["conversations"]?.cancel()
await subscriptions["conversations"]?.cancel()
}
}
}

func subscribeToAllMessages(clientAddress: String) {
guard let client = clients[clientAddress] else {
guard let client = await clients[clientAddress] else {
return
}

subscriptions["messages"]?.cancel()
subscriptions["messages"] = Task {
await subscriptions["messages"]?.cancel()
await subscriptions["messages"] = Task {
do {
for try await message in try await client.conversations.streamAllMessages() {
do {
Expand All @@ -550,7 +550,7 @@ public class XMTPModule: Module {
}
} catch {
print("Error in all messages subscription: \(error)")
subscriptions["messages"]?.cancel()
await subscriptions["messages"]?.cancel()
}
}
}
Expand All @@ -560,8 +560,8 @@ public class XMTPModule: Module {
return
}

subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
subscriptions[conversation.cacheKey(clientAddress)] = Task {
await subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
await subscriptions[conversation.cacheKey(clientAddress)] = Task {
do {
for try await message in conversation.streamMessages() {
do {
Expand All @@ -575,7 +575,7 @@ public class XMTPModule: Module {
}
} catch {
print("Error in messages subscription: \(error)")
subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
await subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
}
}
}
Expand All @@ -585,6 +585,6 @@ public class XMTPModule: Module {
return
}

subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
await subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
}
}

0 comments on commit aa18df5

Please sign in to comment.