-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subscribe2 #226
Subscribe2 #226
Changes from all commits
2ce9bcb
66ce4f5
96d521a
42c21c5
c13cd56
e183f35
7b4eba8
082f238
62669a4
9b1c320
2d1addc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import Foundation | |
import LibXMTP | ||
|
||
public enum ConversationError: Error, CustomStringConvertible { | ||
case recipientNotOnNetwork, recipientIsSender, v1NotSupported(String) | ||
case recipientNotOnNetwork, recipientIsSender, v1NotSupported(String), streamingIssue(String) | ||
|
||
public var description: String { | ||
switch self { | ||
|
@@ -13,6 +13,9 @@ public enum ConversationError: Error, CustomStringConvertible { | |
case .v1NotSupported(let str): | ||
return "ConversationError.v1NotSupported: V1 does not support: \(str)" | ||
} | ||
case .streamingIssue(let str): | ||
return "ConversationError.streamingIssue: \(str)" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -242,33 +245,33 @@ public actor Conversations { | |
func streamAllV2Messages() async throws -> AsyncThrowingStream<DecodedMessage, Error> { | ||
return AsyncThrowingStream { continuation in | ||
Task { | ||
while true { | ||
var topics: [String] = [ | ||
Topic.userInvite(client.address).description, | ||
Topic.userIntro(client.address).description, | ||
] | ||
var topics: [String] = [ | ||
Topic.userInvite(client.address).description, | ||
Topic.userIntro(client.address).description, | ||
] | ||
|
||
for conversation in try await list() { | ||
topics.append(conversation.topic) | ||
} | ||
for conversation in try await list() { | ||
topics.append(conversation.topic) | ||
} | ||
|
||
while(true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where this while loop should actually live. 🤔 |
||
do { | ||
for try await envelope in client.subscribe(topics: topics) { | ||
for try await (envelope, subscription) in client.subscribe(topics: topics) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried a scenario where we returned the subscription independent of this for loop but the await function never completed. It may be my lack of knowledge of iOS threads/streams. But you can see that work in the previous commit history. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zombieobject I've got some iOS threading questions for you next week 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nplasterer Thanks for looping me in, I'll have a look. 👨🏼🚒 |
||
if let conversation = conversationsByTopic[envelope.contentTopic] { | ||
let decoded = try conversation.decode(envelope) | ||
continuation.yield(decoded) | ||
} else if envelope.contentTopic.hasPrefix("/xmtp/0/invite-") { | ||
let conversation = try fromInvite(envelope: envelope) | ||
conversationsByTopic[conversation.topic] = conversation | ||
break // Break so we can resubscribe with the new conversation | ||
topics.append(conversation.topic) | ||
try await subscription.update(req: client.makeSubscribeRequest(topics: topics).toFFI) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this update method want the entire list of topics or just the new topics? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is also blocking from the for loop continuing.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It takes the entire list of topics |
||
} else if envelope.contentTopic.hasPrefix("/xmtp/0/intro-") { | ||
let conversation = try fromIntro(envelope: envelope) | ||
conversationsByTopic[conversation.topic] = conversation | ||
let decoded = try conversation.decode(envelope) | ||
continuation.yield(decoded) | ||
break // Break so we can resubscribe with the new conversation | ||
} else { | ||
print("huh \(envelope)") | ||
topics.append(conversation.topic) | ||
try await subscription.update(req: client.makeSubscribeRequest(topics: topics).toFFI) | ||
} | ||
} | ||
} catch { | ||
|
@@ -373,33 +376,33 @@ public actor Conversations { | |
func streamAllV2DecryptedMessages() async throws -> AsyncThrowingStream<DecryptedMessage, Error> { | ||
return AsyncThrowingStream { continuation in | ||
Task { | ||
var topics: [String] = [ | ||
Topic.userInvite(client.address).description, | ||
Topic.userIntro(client.address).description, | ||
] | ||
|
||
for conversation in try await list() { | ||
topics.append(conversation.topic) | ||
} | ||
|
||
while true { | ||
var topics: [String] = [ | ||
Topic.userInvite(client.address).description, | ||
Topic.userIntro(client.address).description, | ||
] | ||
|
||
for conversation in try await list() { | ||
topics.append(conversation.topic) | ||
} | ||
|
||
do { | ||
for try await envelope in client.subscribe(topics: topics) { | ||
for try await (envelope, subscription) in client.subscribe(topics: topics) { | ||
if let conversation = conversationsByTopic[envelope.contentTopic] { | ||
let decoded = try conversation.decrypt(envelope) | ||
continuation.yield(decoded) | ||
} else if envelope.contentTopic.hasPrefix("/xmtp/0/invite-") { | ||
let conversation = try fromInvite(envelope: envelope) | ||
conversationsByTopic[conversation.topic] = conversation | ||
break // Break so we can resubscribe with the new conversation | ||
topics.append(conversation.topic) | ||
try await subscription.update(req: client.makeSubscribeRequest(topics: topics).toFFI) | ||
} else if envelope.contentTopic.hasPrefix("/xmtp/0/intro-") { | ||
let conversation = try fromIntro(envelope: envelope) | ||
conversationsByTopic[conversation.topic] = conversation | ||
let decoded = try conversation.decrypt(envelope) | ||
continuation.yield(decoded) | ||
break // Break so we can resubscribe with the new conversation | ||
} else { | ||
print("huh \(envelope)") | ||
topics.append(conversation.topic) | ||
try await subscription.update(req: client.makeSubscribeRequest(topics: topics).toFFI) | ||
} | ||
} | ||
} catch { | ||
|
@@ -474,7 +477,7 @@ public actor Conversations { | |
Task { | ||
var streamedConversationTopics: Set<String> = [] | ||
|
||
for try await envelope in client.subscribe(topics: [.userIntro(client.address), .userInvite(client.address)]) { | ||
for try await (envelope, subscription) in client.subscribe(topics: [.userIntro(client.address), .userInvite(client.address)]) { | ||
if envelope.contentTopic == Topic.userIntro(client.address).description { | ||
let conversationV1 = try fromIntro(envelope: envelope) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Touple probably isn't the best idea to solve this. But the simplest for now.