Skip to content
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

Fix: Stream All Streams All #249

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,23 +409,30 @@ public actor Conversations {
}
}
}

public func streamAll() -> AsyncThrowingStream<Conversation, Error> {
AsyncThrowingStream<Conversation, Error> { continuation in
Task {
do {
for try await conversation in streamGroupConversations() {
continuation.yield(conversation)
}
for try await conversation in stream() {
continuation.yield(conversation)
}
} catch {
continuation.finish(throwing: error)
}
}
}
}

public func streamAll() -> AsyncThrowingStream<Conversation, Error> {
AsyncThrowingStream<Conversation, Error> { continuation in
@Sendable func forwardStreamToMerged(stream: AsyncThrowingStream<Conversation, Error>) async {
do {
var iterator = stream.makeAsyncIterator()
while let element = try await iterator.next() {
continuation.yield(element)
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}

Task {
await forwardStreamToMerged(stream: stream())
}
Task {
await forwardStreamToMerged(stream: streamGroupConversations())
}
}
}

private func makeConversation(from sealedInvitation: SealedInvitation) throws -> ConversationV2 {
let unsealed = try sealedInvitation.v1.getInvitation(viewer: client.keys)
let conversation = try ConversationV2.create(client: client, invitation: unsealed, header: sealedInvitation.v1.header)
Expand Down
Loading