Skip to content

Commit

Permalink
abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Sep 21, 2023
1 parent 5b18587 commit f1e4c9d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,11 @@ public class XMTPModule: Module {
}

AsyncFunction("unsubscribeFromConversations") { (clientAddress: String) in
await subscriptionsManager.getSubscription(key: "conversations:\(clientAddress)")?.cancel()
await subscriptionsManager.getSubscription(key: getConversationsKey(key: clientAddress))?.cancel()
}

AsyncFunction("unsubscribeFromAllMessages") { (clientAddress: String) in
await subscriptionsManager.getSubscription(key: "messages:\(clientAddress)")?.cancel()
await subscriptionsManager.getSubscription(key: getMessagesKey(key: clientAddress))?.cancel()
}

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

await subscriptionsManager.getSubscription(key: "conversations:\(clientAddress)")?.cancel()
await subscriptionsManager.updateSubscription(key: "conversations:\(clientAddress)", task: Task {
await subscriptionsManager.getSubscription(key: getConversationsKey(key: clientAddress))?.cancel()
await subscriptionsManager.updateSubscription(key: getConversationsKey(key: clientAddress), task: Task {
do {
for try await conversation in await client.conversations.stream() {
try sendEvent("conversation", [
Expand All @@ -581,7 +581,7 @@ public class XMTPModule: Module {
}
} catch {
print("Error in conversations subscription: \(error)")
await subscriptionsManager.getSubscription(key: "conversations:\(clientAddress)")?.cancel()
await subscriptionsManager.getSubscription(key: getConversationsKey(key: clientAddress))?.cancel()
}
})
}
Expand All @@ -591,8 +591,8 @@ public class XMTPModule: Module {
return
}

await subscriptionsManager.getSubscription(key: "messages:\(clientAddress)")?.cancel()
await subscriptionsManager.updateSubscription(key: "messages:\(clientAddress)", task: Task {
await subscriptionsManager.getSubscription(key: getMessagesKey(key: clientAddress))?.cancel()
await subscriptionsManager.updateSubscription(key: getMessagesKey(key: clientAddress), task: Task {
do {
for try await message in try await client.conversations.streamAllMessages() {
do {
Expand All @@ -606,7 +606,7 @@ public class XMTPModule: Module {
}
} catch {
print("Error in all messages subscription: \(error)")
await subscriptionsManager.getSubscription(key: "messages:\(clientAddress)")?.cancel()
await subscriptionsManager.getSubscription(key: getMessagesKey(key: clientAddress))?.cancel()
}
})
}
Expand Down Expand Up @@ -643,4 +643,12 @@ public class XMTPModule: Module {

await subscriptionsManager.getSubscription(key: conversation.cacheKey(clientAddress))?.cancel()
}

func getMessagesKey(clientAddress: String) -> String {
return "messages:\(clientAddress)"
}

func getConversationsKey(clientAddress: String) -> String {
return "conversations:\(clientAddress)"
}
}

0 comments on commit f1e4c9d

Please sign in to comment.