Skip to content

Commit

Permalink
Merge pull request #24 from xmtp/np/fix-example-stream
Browse files Browse the repository at this point in the history
Fix example streaming
  • Loading branch information
nplasterer authored May 1, 2023
2 parents 5cfbc67 + 9bed441 commit b9b725a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class XMTPModule : Module() {
findConversation(topic = topic, conversationId = conversationId) ?: return
subscriptions[conversation.cacheKey] = CoroutineScope(Dispatchers.IO).launch {
try {
conversation.streamMessages().collect() { message ->
conversation.streamMessages().collect { message ->
sendEvent(
"message",
mapOf(
Expand Down
12 changes: 2 additions & 10 deletions example/src/ConversationView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,11 @@ export default function ConversationView({ route }: Props): JSX.Element {

useEffect(() => {
const unsubscribe = conversation.streamMessages(async (message) => {
const uniqueMessages = [
...new Map(
[message, ...messages].map((item: DecodedMessage) => [item.id, item])
).values(),
].sort((a, b) => {
return a.sent > b.sent ? 1 : -1;
});

setMessages(uniqueMessages);
await loadMessages();
});

return unsubscribe;
});
}, []);

return (
<>
Expand Down
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ export function address(): string {
return XMTPModule.address();
}

export async function auth(address: string, environment: 'local' | 'dev' | 'production') {
export async function auth(
address: string,
environment: "local" | "dev" | "production"
) {
return await XMTPModule.auth(address, environment);
}

export async function receiveSignature(requestID: string, signature: string) {
return await XMTPModule.receiveSignature(requestID, signature);
}

export async function createRandom(environment: 'local' | 'dev' | 'production'): Promise<string> {
export async function createRandom(
environment: "local" | "dev" | "production"
): Promise<string> {
return await XMTPModule.createRandom(environment);
}

Expand All @@ -33,8 +38,15 @@ export async function listMessages(
before?: Date | undefined,
after?: Date | undefined
): Promise<DecodedMessage[]> {
return (await XMTPModule.loadMessages(conversationTopic, conversationID, limit, before?.getTime, after?.getTime)).map(
(json: string) => {
return (
await XMTPModule.loadMessages(
conversationTopic,
conversationID,
limit,
before?.getTime,
after?.getTime
)
).map((json: string) => {
return JSON.parse(json);
}
);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export class Client {
address: string;
conversations: Conversations;

static async create(signer: Signer, environment: 'local' | 'dev' | 'production'): Promise<Client> {
static async create(
signer: Signer,
environment: "local" | "dev" | "production"
): Promise<Client> {
return new Promise<Client>((resolve, reject) => {
(async () => {
XMTPModule.emitter.addListener(
Expand Down Expand Up @@ -40,7 +43,9 @@ export class Client {
});
}

static async createRandom(environment: 'local' | 'dev' | 'production'): Promise<Client> {
static async createRandom(
environment: "local" | "dev" | "production"
): Promise<Client> {
const address = await XMTPModule.createRandom(environment);
return new Client(address);
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export class Conversation {
}

// TODO: Support pagination and conversation ID here
async messages(limit?: number | undefined, before?: Date | undefined, after?: Date | undefined): Promise<DecodedMessage[]> {
async messages(
limit?: number | undefined,
before?: Date | undefined,
after?: Date | undefined
): Promise<DecodedMessage[]> {
try {
return await XMTP.listMessages(this.topic, this.conversationID, limit, before, after);
} catch (e) {
Expand Down

0 comments on commit b9b725a

Please sign in to comment.