Skip to content

Commit

Permalink
add the RN side of the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Feb 21, 2024
1 parent 666a78f commit 5668230
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,15 @@ export function subscribeToGroups(clientAddress: string) {
return XMTPModule.subscribeToGroups(clientAddress)
}

export function subscribeToAllMessages(clientAddress: string) {
return XMTPModule.subscribeToAllMessages(clientAddress)
export function subscribeToAllMessages(
clientAddress: string,
includeGroups: boolean
) {
return XMTPModule.subscribeToAllMessages(clientAddress, includeGroups)
}

export function subscribeToAllGroupMessages(clientAddress: string) {
return XMTPModule.subscribeToAllGroupMessages(clientAddress)
}

export async function subscribeToMessages(
Expand All @@ -529,6 +536,10 @@ export function unsubscribeFromAllMessages(clientAddress: string) {
return XMTPModule.unsubscribeFromAllMessages(clientAddress)
}

export function unsubscribeFromAllGroupMessages(clientAddress: string) {
return XMTPModule.unsubscribeFromAllGroupMessages(clientAddress)
}

export async function unsubscribeFromMessages(
clientAddress: string,
topic: string
Expand Down
44 changes: 43 additions & 1 deletion src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,48 @@ export default class Conversations<
* Listen for new messages in all conversations.
*
* This method subscribes to all conversations in real-time and listens for incoming and outgoing messages.
* @param {boolean} includeGroups - Whether or not to include group messages in the stream.
* @param {Function} callback - A callback function that will be invoked when a message is sent or received.
* @returns {Promise<void>} A Promise that resolves when the stream is set up.
*/
async streamAllMessages(
includeGroups: boolean = false,
callback: (message: DecodedMessage<ContentTypes>) => Promise<void>
): Promise<void> {
XMTPModule.subscribeToAllMessages(this.client.address)
XMTPModule.subscribeToAllMessages(this.client.address, includeGroups)
XMTPModule.emitter.addListener(
'message',
async ({
clientAddress,
message,
}: {
clientAddress: string
message: DecodedMessage
}) => {
if (clientAddress !== this.client.address) {
return
}
if (this.known[message.id]) {
return
}

this.known[message.id] = true
await callback(DecodedMessage.fromObject(message, this.client))
}
)
}

/**
* Listen for new messages in all groups.
*
* This method subscribes to all groups in real-time and listens for incoming and outgoing messages.
* @param {Function} callback - A callback function that will be invoked when a message is sent or received.
* @returns {Promise<void>} A Promise that resolves when the stream is set up.
*/
async streamAllGroupMessages(
callback: (message: DecodedMessage<ContentTypes>) => Promise<void>
): Promise<void> {
XMTPModule.subscribeToAllGroupMessages(this.client.address)
XMTPModule.emitter.addListener(
'message',
async ({
Expand Down Expand Up @@ -291,4 +326,11 @@ export default class Conversations<
cancelStreamAllMessages() {
XMTPModule.unsubscribeFromAllMessages(this.client.address)
}

/**
* Cancels the stream for new messages in all groups.
*/
cancelStreamAllGroupMessages() {
XMTPModule.unsubscribeFromAllGroupMessages(this.client.address)
}
}

0 comments on commit 5668230

Please sign in to comment.