Skip to content

Commit

Permalink
Merge pull request #285 from xmtp/ar/event-names
Browse files Browse the repository at this point in the history
Change to dedicated event names per event
  • Loading branch information
alexrisch authored Feb 26, 2024
2 parents ea461fb + a59ad07 commit 0099b84
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,22 @@ class XMTPModule : Module() {
override fun definition() = ModuleDefinition {
Name("XMTP")
Events(
// Auth
"sign",
"authed",
"preCreateIdentityCallback",
"preEnableIdentityCallback",
// Conversations
"conversation",
"conversationContainer",
"group",
"conversationContainer",
"message",
"preEnableIdentityCallback",
"preCreateIdentityCallback"
"allGroupMessage",
// Conversation
"conversationMessage",
// Group
"groupMessage"

)

Function("address") { clientAddress: String ->
Expand Down Expand Up @@ -1038,7 +1046,7 @@ class XMTPModule : Module() {
try {
client.conversations.streamAllGroupDecryptedMessages().collect { message ->
sendEvent(
"message",
"allGroupMessage",
mapOf(
"clientAddress" to clientAddress,
"message" to DecodedMessageWrapper.encodeMap(message),
Expand All @@ -1064,7 +1072,7 @@ class XMTPModule : Module() {
try {
conversation.streamDecryptedMessages().collect { message ->
sendEvent(
"message",
"conversationMessage",
mapOf(
"clientAddress" to clientAddress,
"message" to DecodedMessageWrapper.encodeMap(message),
Expand All @@ -1090,7 +1098,7 @@ class XMTPModule : Module() {
try {
group.streamDecryptedMessages().collect { message ->
sendEvent(
"message",
"groupMessage",
mapOf(
"clientAddress" to clientAddress,
"message" to DecodedMessageWrapper.encodeMap(message),
Expand Down
24 changes: 20 additions & 4 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,23 @@ public class XMTPModule: Module {
public func definition() -> ModuleDefinition {
Name("XMTP")

Events("sign", "authed", "conversation", "message", "group", "conversationContainer", "preEnableIdentityCallback", "preCreateIdentityCallback")
Events(
// Auth
"sign",
"authed",
"preCreateIdentityCallback",
"preEnableIdentityCallback",
// Conversations
"conversation",
"group",
"conversationContainer",
"message",
"allGroupMessage",
// Conversation
"conversationMessage",
// Group
"groupMessage"
)

AsyncFunction("address") { (clientAddress: String) -> String in
if let client = await clientsManager.getClient(key: clientAddress) {
Expand Down Expand Up @@ -963,7 +979,7 @@ public class XMTPModule: Module {
do {
for try await message in await client.conversations.streamAllGroupDecryptedMessages() {
do {
try sendEvent("message", [
try sendEvent("allGroupMessage", [
"clientAddress": clientAddress,
"message": DecodedMessageWrapper.encodeToObj(message, client: client),
])
Expand Down Expand Up @@ -992,7 +1008,7 @@ public class XMTPModule: Module {
do {
for try await message in conversation.streamDecryptedMessages() {
do {
try sendEvent("message", [
try sendEvent("conversationMessage", [
"clientAddress": clientAddress,
"message": DecodedMessageWrapper.encodeToObj(message, client: client),
])
Expand Down Expand Up @@ -1062,7 +1078,7 @@ public class XMTPModule: Module {
do {
for try await message in group.streamDecryptedMessages() {
do {
try sendEvent("message", [
try sendEvent("groupMessage", [
"clientAddress": clientAddress,
"message": DecodedMessageWrapper.encodeToObj(message, client: client),
])
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from './ConversationContainer'
import { ConversationSendPayload } from './types/ConversationCodecs'
import { DefaultContentTypes } from './types/DefaultContentType'
import { EventTypes } from './types/EventTypes'
import { SendOptions } from './types/SendOptions'
import * as XMTP from '../index'
import {
Expand Down Expand Up @@ -274,7 +275,7 @@ export class Conversation<ContentTypes extends DefaultContentTypes>
XMTP.subscribeToMessages(this.client.address, this.topic)
const hasSeen = {}
const messageSubscription = XMTP.emitter.addListener(
'message',
EventTypes.ConversationMessage,
async ({
clientAddress,
message,
Expand Down
11 changes: 6 additions & 5 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from './ConversationContainer'
import { DecodedMessage } from './DecodedMessage'
import { Group } from './Group'
import { EventTypes } from './types/EventTypes'
import { ConversationContext } from '../XMTP.types'
import * as XMTPModule from '../index'
import { ContentCodec } from '../index'
Expand Down Expand Up @@ -110,7 +111,7 @@ export default class Conversations<
): Promise<() => void> {
XMTPModule.subscribeToGroups(this.client.address)
const groupsSubscription = XMTPModule.emitter.addListener(
'group',
EventTypes.Group,
async ({
clientAddress,
group,
Expand Down Expand Up @@ -168,7 +169,7 @@ export default class Conversations<
) {
XMTPModule.subscribeToConversations(this.client.address)
XMTPModule.emitter.addListener(
'conversation',
EventTypes.Conversation,
async ({
clientAddress,
conversation,
Expand Down Expand Up @@ -205,7 +206,7 @@ export default class Conversations<
) {
XMTPModule.subscribeToAll(this.client.address)
const subscription = XMTPModule.emitter.addListener(
'conversationContainer',
EventTypes.ConversationContainer,
async ({
clientAddress,
conversationContainer,
Expand Down Expand Up @@ -252,7 +253,7 @@ export default class Conversations<
): Promise<void> {
XMTPModule.subscribeToAllMessages(this.client.address, includeGroups)
XMTPModule.emitter.addListener(
'message',
EventTypes.Message,
async ({
clientAddress,
message,
Expand Down Expand Up @@ -285,7 +286,7 @@ export default class Conversations<
): Promise<void> {
XMTPModule.subscribeToAllGroupMessages(this.client.address)
XMTPModule.emitter.addListener(
'message',
EventTypes.AllGroupMessage,
async ({
clientAddress,
message,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { DecodedMessage } from './DecodedMessage'
import { ConversationSendPayload } from './types/ConversationCodecs'
import { DefaultContentTypes } from './types/DefaultContentType'
import { EventTypes } from './types/EventTypes'
import { SendOptions } from './types/SendOptions'
import * as XMTP from '../index'

Expand Down Expand Up @@ -128,7 +129,7 @@ export class Group<
XMTP.subscribeToGroupMessages(this.client.address, this.id)
const hasSeen = {}
const messageSubscription = XMTP.emitter.addListener(
'message',
EventTypes.GroupMessage,
async ({
clientAddress,
message,
Expand Down
40 changes: 40 additions & 0 deletions src/lib/types/EventTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export enum EventTypes {
// Auth
Sign = 'sign',
Authed = 'authed',
PreCreateIdentityCallback = 'preCreateIdentityCallback',
PreEnableIdentityCallback = 'preEnableIdentityCallback',
// Conversations Events
/**
* Current user is in a newly created conversation
*/
Conversation = 'conversation',
/**
* Current user is in a newly created group
*/
Group = 'group',
/**
* Current user is in a newly created group or conversation
*/
ConversationContainer = 'conversationContainer',
/**
* Current user receives a new message in any conversation
*/
Message = 'message',
/**
* Current user receives a new message in any group
* Current limitation, only groups created before listener is added will be received
*/
AllGroupMessage = 'allGroupMessage',

// Conversation Events
/**
* A new message is sent to a specific conversation
*/
ConversationMessage = 'conversationMessage',
// Group Events
/**
* A new message is sent to a specific group
*/
GroupMessage = 'groupMessage',
}

0 comments on commit 0099b84

Please sign in to comment.